diff options
12 files changed, 60 insertions, 690 deletions
diff --git a/src/quick/scenegraph/compressedtexture/qsgcompressedatlastexture.cpp b/src/quick/scenegraph/compressedtexture/qsgcompressedatlastexture.cpp index e868a4380e..796bc870de 100644 --- a/src/quick/scenegraph/compressedtexture/qsgcompressedatlastexture.cpp +++ b/src/quick/scenegraph/compressedtexture/qsgcompressedatlastexture.cpp @@ -56,7 +56,6 @@ #include <private/qquickprofiler_p.h> #include <private/qsgtexture_p.h> #include <private/qsgcompressedtexture_p.h> -#include <private/qsgpkmhandler_p.h> QT_BEGIN_NAMESPACE @@ -154,13 +153,12 @@ QSGTexture *Texture::removedFromAtlas() const } if (!m_data.isEmpty()) { - QSGCompressedTexture::DataPtr texData(QSGCompressedTexture::DataPtr::create()); - texData->data = m_data; - texData->size = m_size; - texData->format = static_cast<Atlas*>(m_atlas)->format(); - texData->hasAlpha = hasAlphaChannel(); - texData->dataLength = m_dataLength; - texData->dataOffset = m_dataOffset; + QTextureFileData texData; + texData.setData(m_data); + texData.setSize(m_size); + texData.setGLInternalFormat(static_cast<Atlas*>(m_atlas)->format()); + texData.setDataLength(m_dataLength); + texData.setDataOffset(m_dataOffset); m_nonatlas_texture = new QSGCompressedTexture(texData); m_nonatlas_texture->setMipmapFiltering(mipmapFiltering()); m_nonatlas_texture->setFiltering(filtering()); diff --git a/src/quick/scenegraph/compressedtexture/qsgcompressedtexture.cpp b/src/quick/scenegraph/compressedtexture/qsgcompressedtexture.cpp index 839c562989..d3310bc11c 100644 --- a/src/quick/scenegraph/compressedtexture/qsgcompressedtexture.cpp +++ b/src/quick/scenegraph/compressedtexture/qsgcompressedtexture.cpp @@ -49,50 +49,11 @@ QT_BEGIN_NAMESPACE Q_LOGGING_CATEGORY(QSG_LOG_TEXTUREIO, "qt.scenegraph.textureio"); -bool QSGCompressedTextureData::isValid() const -{ - if (data.isNull() || size.isEmpty() || !format) - return false; - if (dataLength < 0 || dataOffset < 0 || dataOffset >= data.length()) - return false; - if (dataLength > 0 && qint64(dataOffset) + qint64(dataLength) > qint64(data.length())) - return false; - - return true; -} - -int QSGCompressedTextureData::sizeInBytes() const -{ - if (!isValid()) - return 0; - return dataLength > 0 ? dataLength : data.length() - dataOffset; -} - -Q_QUICK_PRIVATE_EXPORT QDebug operator<<(QDebug dbg, const QSGCompressedTextureData *d) -{ - QDebugStateSaver saver(dbg); - - dbg.nospace() << "QSGCompressedTextureData("; - if (d) { - dbg << d->logName << ' '; - dbg << static_cast<QOpenGLTexture::TextureFormat>(d->format) - << "[0x" << hex << d->format << dec << "]"; - dbg.space() << (d->hasAlpha ? "with" : "no") << "alpha" << d->size - << "databuffer" << d->data.size() << "offset" << d->dataOffset << "length"; - dbg.nospace() << d->dataLength << ")"; - } else { - dbg << "null)"; - } - return dbg; -} - -QSGCompressedTexture::QSGCompressedTexture(const DataPtr& texData) +QSGCompressedTexture::QSGCompressedTexture(const QTextureFileData &texData) : m_textureData(texData) { - if (m_textureData) { - m_size = m_textureData->size; - m_hasAlpha = m_textureData->hasAlpha; - } + m_size = m_textureData.size(); + m_hasAlpha = !formatIsOpaque(m_textureData.glInternalFormat()); } QSGCompressedTexture::~QSGCompressedTexture() @@ -155,37 +116,40 @@ void QSGCompressedTexture::bind() if (m_uploaded) return; - QByteArray logName(m_textureData ? m_textureData->logName : QByteArrayLiteral("(unset)")); - - if (!m_textureData || !m_textureData->isValid()) { - qCDebug(QSG_LOG_TEXTUREIO, "Invalid texture data for %s", logName.constData()); + if (!m_textureData.isValid()) { + qCDebug(QSG_LOG_TEXTUREIO, "Invalid texture data for %s", m_textureData.logName().constData()); funcs->glBindTexture(GL_TEXTURE_2D, 0); return; } if (Q_UNLIKELY(QSG_LOG_TEXTUREIO().isDebugEnabled())) { - qCDebug(QSG_LOG_TEXTUREIO) << "Uploading texture" << m_textureData.data(); + qCDebug(QSG_LOG_TEXTUREIO) << "Uploading texture" << m_textureData; while (funcs->glGetError() != GL_NO_ERROR); } - funcs->glCompressedTexImage2D(GL_TEXTURE_2D, 0, m_textureData->format, - m_size.width(), m_size.height(), 0, m_textureData->sizeInBytes(), - m_textureData->data.constData() + m_textureData->dataOffset); + funcs->glCompressedTexImage2D(GL_TEXTURE_2D, 0, m_textureData.glInternalFormat(), + m_size.width(), m_size.height(), 0, m_textureData.dataLength(), + m_textureData.data().constData() + m_textureData.dataOffset()); if (Q_UNLIKELY(QSG_LOG_TEXTUREIO().isDebugEnabled())) { GLuint error = funcs->glGetError(); if (error != GL_NO_ERROR) { - qCDebug(QSG_LOG_TEXTUREIO, "glCompressedTexImage2D failed for %s, error 0x%x", logName.constData(), error); + qCDebug(QSG_LOG_TEXTUREIO, "glCompressedTexImage2D failed for %s, error 0x%x", m_textureData.logName().constData(), error); } } - m_textureData.clear(); // Release this memory, not needed anymore + m_textureData = QTextureFileData(); // Release this memory, not needed anymore updateBindOptions(true); m_uploaded = true; #endif // QT_CONFIG(opengl) } +QTextureFileData QSGCompressedTexture::textureData() const +{ + return m_textureData; +} + bool QSGCompressedTexture::formatIsOpaque(quint32 glTextureFormat) { switch (glTextureFormat) { @@ -211,14 +175,14 @@ bool QSGCompressedTexture::formatIsOpaque(quint32 glTextureFormat) } } -QSGCompressedTextureFactory::QSGCompressedTextureFactory(const QSGCompressedTexture::DataPtr &texData) +QSGCompressedTextureFactory::QSGCompressedTextureFactory(const QTextureFileData &texData) : m_textureData(texData) { } QSGTexture *QSGCompressedTextureFactory::createTexture(QQuickWindow *window) const { - if (!m_textureData || !m_textureData->isValid()) + if (!m_textureData.isValid()) return nullptr; // attempt to atlas the texture @@ -232,15 +196,12 @@ QSGTexture *QSGCompressedTextureFactory::createTexture(QQuickWindow *window) con int QSGCompressedTextureFactory::textureByteCount() const { - return m_textureData ? m_textureData->sizeInBytes() : 0; + return qMax(0, m_textureData.data().size() - m_textureData.dataOffset()); } - QSize QSGCompressedTextureFactory::textureSize() const { - if (m_textureData && m_textureData->isValid()) - return m_textureData->size; - return QSize(); + return m_textureData.size(); } QT_END_NAMESPACE diff --git a/src/quick/scenegraph/compressedtexture/qsgcompressedtexture_p.h b/src/quick/scenegraph/compressedtexture/qsgcompressedtexture_p.h index aa87316809..c3b58a2389 100644 --- a/src/quick/scenegraph/compressedtexture/qsgcompressedtexture_p.h +++ b/src/quick/scenegraph/compressedtexture/qsgcompressedtexture_p.h @@ -51,6 +51,7 @@ // We mean it. // +#include <private/qtexturefiledata_p.h> #include <QSGTexture> #include <QtQuick/private/qsgcontext_p.h> #include <QQuickTextureFactory> @@ -58,30 +59,11 @@ QT_BEGIN_NAMESPACE -struct Q_QUICK_PRIVATE_EXPORT QSGCompressedTextureData -{ - QByteArray logName; - QByteArray data; - QSize size; - uint format = 0; - int dataOffset = 0; - int dataLength = 0; - bool hasAlpha = false; - - bool isValid() const; - int sizeInBytes() const; -}; - -Q_QUICK_PRIVATE_EXPORT QDebug operator<<(QDebug, const QSGCompressedTextureData *); - - class Q_QUICK_PRIVATE_EXPORT QSGCompressedTexture : public QSGTexture { Q_OBJECT public: - typedef QSharedPointer<QSGCompressedTextureData> DataPtr; - - QSGCompressedTexture(const DataPtr& texData); + QSGCompressedTexture(const QTextureFileData& texData); virtual ~QSGCompressedTexture(); int textureId() const override; @@ -91,12 +73,12 @@ public: void bind() override; - const QSGCompressedTextureData *textureData(); + QTextureFileData textureData() const; static bool formatIsOpaque(quint32 glTextureFormat); protected: - DataPtr m_textureData; + QTextureFileData m_textureData; QSize m_size; mutable uint m_textureId = 0; bool m_hasAlpha = false; @@ -110,13 +92,14 @@ namespace QSGAtlasTexture { class Q_QUICK_PRIVATE_EXPORT QSGCompressedTextureFactory : public QQuickTextureFactory { public: - QSGCompressedTextureFactory(const QSGCompressedTexture::DataPtr& texData); + QSGCompressedTextureFactory(const QTextureFileData& texData); QSGTexture *createTexture(QQuickWindow *) const override; int textureByteCount() const override; QSize textureSize() const override; protected: - QSGCompressedTexture::DataPtr m_textureData; + QTextureFileData m_textureData; + private: friend class QSGAtlasTexture::Manager; }; diff --git a/src/quick/scenegraph/compressedtexture/qsgktxhandler.cpp b/src/quick/scenegraph/compressedtexture/qsgktxhandler.cpp deleted file mode 100644 index e3e4ca6824..0000000000 --- a/src/quick/scenegraph/compressedtexture/qsgktxhandler.cpp +++ /dev/null @@ -1,186 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2017 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtQuick module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#include "qsgktxhandler_p.h" -#include "qsgcompressedtexture_p.h" -#include <QOpenGLTexture> -#include <QtEndian> - -//#define KTX_DEBUG - -QT_BEGIN_NAMESPACE - -#define KTX_IDENTIFIER_LENGTH 12 -static const char ktxIdentifier[KTX_IDENTIFIER_LENGTH] = { '\xAB', 'K', 'T', 'X', ' ', '1', '1', '\xBB', '\r', '\n', '\x1A', '\n' }; -static const quint32 platformEndianIdentifier = 0x04030201; -static const quint32 inversePlatformEndianIdentifier = 0x01020304; - -struct KTXHeader { - quint8 identifier[KTX_IDENTIFIER_LENGTH]; //Must match ktxIdentifier - quint32 endianness; //Either platformEndianIdentifier or inversePlatformEndianIdentifier, other values not allowed. - quint32 glType; - quint32 glTypeSize; - quint32 glFormat; - quint32 glInternalFormat; - quint32 glBaseInternalFormat; - quint32 pixelWidth; - quint32 pixelHeight; - quint32 pixelDepth; - quint32 numberOfArrayElements; - quint32 numberOfFaces; - quint32 numberOfMipmapLevels; - quint32 bytesOfKeyValueData; -}; - -static const int headerSize = sizeof(KTXHeader); - -// Currently unused, declared for future reference -struct KTXKeyValuePairItem { - quint32 keyAndValueByteSize; - /* - quint8 keyAndValue[keyAndValueByteSize]; - quint8 valuePadding[3 - ((keyAndValueByteSize + 3) % 4)]; - */ -}; - -struct KTXMipmapLevel { - quint32 imageSize; - /* - for each array_element in numberOfArrayElements* - for each face in numberOfFaces - for each z_slice in pixelDepth* - for each row or row_of_blocks in pixelHeight* - for each pixel or block_of_pixels in pixelWidth - Byte data[format-specific-number-of-bytes]** - end - end - end - Byte cubePadding[0-3] - end - end - quint8 mipPadding[3 - ((imageSize + 3) % 4)] - */ -}; - -bool QSGKtxHandler::canRead(const QByteArray &suffix, const QByteArray &block) -{ - Q_UNUSED(suffix) - - return (qstrncmp(block.constData(), ktxIdentifier, KTX_IDENTIFIER_LENGTH) == 0); -} - -QQuickTextureFactory *QSGKtxHandler::read() -{ - if (!device()) - return nullptr; - - QByteArray buf = device()->readAll(); - if (buf.size() < headerSize || !canRead(QByteArray(), buf)) { - qCDebug(QSG_LOG_TEXTUREIO, "Invalid KTX file %s", logName().constData()); - return nullptr; - } - - const KTXHeader *header = reinterpret_cast<const KTXHeader *>(buf.constData()); - if (!checkHeader(*header)) { - qCDebug(QSG_LOG_TEXTUREIO, "Unsupported KTX file format in %s", logName().constData()); - return nullptr; - } - - QSGCompressedTexture::DataPtr texData(QSGCompressedTexture::DataPtr::create()); - - texData->size = QSize(decode(header->pixelWidth), decode(header->pixelHeight)); - texData->format = decode(header->glInternalFormat); - texData->hasAlpha = !QSGCompressedTexture::formatIsOpaque(texData->format); - - // For now, ignore any additional mipmap levels - int preambleSize = headerSize + decode(header->bytesOfKeyValueData); - if (buf.size() >= preambleSize + int(sizeof(KTXMipmapLevel))) { - texData->data = buf; - texData->dataOffset = preambleSize + sizeof(quint32); // for the imageSize - const KTXMipmapLevel *level = reinterpret_cast<const KTXMipmapLevel *>(buf.constData() + preambleSize); - texData->dataLength = decode(level->imageSize); - } - - if (!texData->isValid()) { - qCDebug(QSG_LOG_TEXTUREIO, "Invalid values in header of KTX file %s", logName().constData()); - return nullptr; - } - - texData->logName = logName(); -#ifdef KTX_DEBUG - qDebug() << "KTX file handler read" << texData.data(); -#endif - - return new QSGCompressedTextureFactory(texData); -} - -bool QSGKtxHandler::checkHeader(const KTXHeader &header) -{ - if (header.endianness != platformEndianIdentifier && header.endianness != inversePlatformEndianIdentifier) - return false; - inverseEndian = (header.endianness == inversePlatformEndianIdentifier); -#ifdef KTX_DEBUG - QMetaEnum tfme = QMetaEnum::fromType<QOpenGLTexture::TextureFormat>(); - QMetaEnum ptme = QMetaEnum::fromType<QOpenGLTexture::PixelType>(); - qDebug("Header of %s:", logName().constData()); - qDebug(" glType: 0x%x (%s)", decode(header.glType), ptme.valueToKey(decode(header.glType))); - qDebug(" glTypeSize: %u", decode(header.glTypeSize)); - qDebug(" glFormat: 0x%x (%s)", decode(header.glFormat), tfme.valueToKey(decode(header.glFormat))); - qDebug(" glInternalFormat: 0x%x (%s)", decode(header.glInternalFormat), tfme.valueToKey(decode(header.glInternalFormat))); - qDebug(" glBaseInternalFormat: 0x%x (%s)", decode(header.glBaseInternalFormat), tfme.valueToKey(decode(header.glBaseInternalFormat))); - qDebug(" pixelWidth: %u", decode(header.pixelWidth)); - qDebug(" pixelHeight: %u", decode(header.pixelHeight)); - qDebug(" pixelDepth: %u", decode(header.pixelDepth)); - qDebug(" numberOfArrayElements: %u", decode(header.numberOfArrayElements)); - qDebug(" numberOfFaces: %u", decode(header.numberOfFaces)); - qDebug(" numberOfMipmapLevels: %u", decode(header.numberOfMipmapLevels)); - qDebug(" bytesOfKeyValueData: %u", decode(header.bytesOfKeyValueData)); -#endif - return ((decode(header.glType) == 0) && - (decode(header.glFormat) == 0) && - (decode(header.pixelDepth) == 0) && - (decode(header.numberOfFaces) == 1)); -} - -quint32 QSGKtxHandler::decode(quint32 val) -{ - return inverseEndian ? qbswap<quint32>(val) : val; -} - -QT_END_NAMESPACE diff --git a/src/quick/scenegraph/compressedtexture/qsgktxhandler_p.h b/src/quick/scenegraph/compressedtexture/qsgktxhandler_p.h deleted file mode 100644 index 22f4db65b2..0000000000 --- a/src/quick/scenegraph/compressedtexture/qsgktxhandler_p.h +++ /dev/null @@ -1,78 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2017 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtQuick module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#ifndef QSGKTXHANDLER_H -#define QSGKTXHANDLER_H - -// -// W A R N I N G -// ------------- -// -// This file is not part of the Qt API. It exists purely as an -// implementation detail. This header file may change from version to -// version without notice, or even be removed. -// -// We mean it. -// - -#include "qsgtexturefilehandler_p.h" - -QT_BEGIN_NAMESPACE - -struct KTXHeader; - -class QSGKtxHandler : public QSGTextureFileHandler -{ -public: - using QSGTextureFileHandler::QSGTextureFileHandler; - - static bool canRead(const QByteArray &suffix, const QByteArray &block); - - QQuickTextureFactory *read() override; - -private: - bool checkHeader(const KTXHeader &header); - quint32 decode(quint32 val); - - bool inverseEndian = false; -}; - -QT_END_NAMESPACE - -#endif // QSGKTXHANDLER_H diff --git a/src/quick/scenegraph/compressedtexture/qsgpkmhandler.cpp b/src/quick/scenegraph/compressedtexture/qsgpkmhandler.cpp deleted file mode 100644 index 618c0db045..0000000000 --- a/src/quick/scenegraph/compressedtexture/qsgpkmhandler.cpp +++ /dev/null @@ -1,118 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2017 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtQuick module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#include "qsgpkmhandler_p.h" -#include "qsgcompressedtexture_p.h" - -#include <QFile> -#include <QDebug> -#include <qendian.h> -#include <qopenglfunctions.h> -#include <qqmlfile.h> -#include <QOpenGLTexture> - -//#define ETC_DEBUG - -QT_BEGIN_NAMESPACE - -static const int headerSize = 16; - -static unsigned int typeMap[5] = { - QOpenGLTexture::RGB8_ETC1, // GL_ETC1_RGB8_OES, - QOpenGLTexture::RGB8_ETC2, // GL_COMPRESSED_RGB8_ETC2, - 0, // unused (obsolete) - QOpenGLTexture::RGBA8_ETC2_EAC, // GL_COMPRESSED_RGBA8_ETC2_EAC, - QOpenGLTexture::RGB8_PunchThrough_Alpha1_ETC2 // GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2 -}; - -bool QSGPkmHandler::canRead(const QByteArray &suffix, const QByteArray &block) -{ - Q_UNUSED(suffix) - - return block.startsWith("PKM "); -} - -QQuickTextureFactory *QSGPkmHandler::read() -{ - if (!device()) - return nullptr; - - QSGCompressedTexture::DataPtr texData(QSGCompressedTexture::DataPtr::create()); - - texData->data = device()->readAll(); - if (texData->data.size() < headerSize || !canRead(QByteArray(), texData->data)) { - qCDebug(QSG_LOG_TEXTUREIO, "Invalid PKM file %s", logName().constData()); - return nullptr; - } - - const char *rawData = texData->data.constData(); - - // ignore version (rawData + 4 & 5) - - // texture type - quint16 type = qFromBigEndian<quint16>(rawData + 6); - if (type > sizeof(typeMap)/sizeof(typeMap[0])) { - qCDebug(QSG_LOG_TEXTUREIO, "Unknown compression format in PKM file %s", logName().constData()); - return nullptr; - } - texData->format = typeMap[type]; - texData->hasAlpha = !QSGCompressedTexture::formatIsOpaque(texData->format); - - // texture size - const int bpb = (texData->format == QOpenGLTexture::RGBA8_ETC2_EAC) ? 16 : 8; - QSize paddedSize(qFromBigEndian<quint16>(rawData + 8), qFromBigEndian<quint16>(rawData + 10)); - texData->dataLength = (paddedSize.width() / 4) * (paddedSize.height() / 4) * bpb; - QSize texSize(qFromBigEndian<quint16>(rawData + 12), qFromBigEndian<quint16>(rawData + 14)); - texData->size = texSize; - - texData->dataOffset = headerSize; - - if (!texData->isValid()) { - qCDebug(QSG_LOG_TEXTUREIO, "Invalid values in header of PKM file %s", logName().constData()); - return nullptr; - } - - texData->logName = logName(); -#ifdef ETC_DEBUG - qDebug() << "PKM file handler read" << texData.data(); -#endif - return new QSGCompressedTextureFactory(texData); -} - -QT_END_NAMESPACE diff --git a/src/quick/scenegraph/compressedtexture/qsgpkmhandler_p.h b/src/quick/scenegraph/compressedtexture/qsgpkmhandler_p.h deleted file mode 100644 index 6154c51b84..0000000000 --- a/src/quick/scenegraph/compressedtexture/qsgpkmhandler_p.h +++ /dev/null @@ -1,70 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2017 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtQuick module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#ifndef QSGPKMHANDLER_H -#define QSGPKMHANDLER_H - -// -// W A R N I N G -// ------------- -// -// This file is not part of the Qt API. It exists purely as an -// implementation detail. This header file may change from version to -// version without notice, or even be removed. -// -// We mean it. -// - -#include "qsgtexturefilehandler_p.h" - -QT_BEGIN_NAMESPACE - -class QSGPkmHandler : public QSGTextureFileHandler -{ -public: - using QSGTextureFileHandler::QSGTextureFileHandler; - - static bool canRead(const QByteArray &suffix, const QByteArray &block); - - QQuickTextureFactory *read() override; -}; - -QT_END_NAMESPACE - -#endif // QSGPKMHANDLER_H diff --git a/src/quick/scenegraph/compressedtexture/qsgtexturefilehandler_p.h b/src/quick/scenegraph/compressedtexture/qsgtexturefilehandler_p.h deleted file mode 100644 index 8b831aebb9..0000000000 --- a/src/quick/scenegraph/compressedtexture/qsgtexturefilehandler_p.h +++ /dev/null @@ -1,83 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2017 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtQuick module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#ifndef QSGTEXTUREFILEHANDLER_P_H -#define QSGTEXTUREFILEHANDLER_P_H - -// -// W A R N I N G -// ------------- -// -// This file is not part of the Qt API. It exists purely as an -// implementation detail. This header file may change from version to -// version without notice, or even be removed. -// -// We mean it. -// - -#include <QLoggingCategory> - -QT_BEGIN_NAMESPACE - -Q_DECLARE_LOGGING_CATEGORY(QSG_LOG_TEXTUREIO) - -class QQuickTextureFactory; - -class QSGTextureFileHandler -{ -public: - QSGTextureFileHandler(QIODevice *device, const QByteArray &logName = QByteArray()) - : m_device(device) - { - m_logName = !logName.isEmpty() ? logName : QByteArrayLiteral("(unknown)"); - } - virtual ~QSGTextureFileHandler() {} - - virtual QQuickTextureFactory *read() = 0; - QIODevice *device() const { return m_device; } - QByteArray logName() const { return m_logName; } - -private: - QIODevice *m_device = nullptr; - QByteArray m_logName; -}; - -QT_END_NAMESPACE - -#endif // QSGTEXTUREFILEHANDLER_P_H diff --git a/src/quick/scenegraph/scenegraph.pri b/src/quick/scenegraph/scenegraph.pri index 4f98638487..f08e8b7863 100644 --- a/src/quick/scenegraph/scenegraph.pri +++ b/src/quick/scenegraph/scenegraph.pri @@ -230,14 +230,9 @@ SOURCES += \ qtConfig(opengl(es1|es2)?) { HEADERS += \ $$PWD/compressedtexture/qsgcompressedatlastexture_p.h \ - $$PWD/compressedtexture/qsgcompressedtexture_p.h \ - $$PWD/compressedtexture/qsgtexturefilehandler_p.h \ - $$PWD/compressedtexture/qsgpkmhandler_p.h \ - $$PWD/compressedtexture/qsgktxhandler_p.h + $$PWD/compressedtexture/qsgcompressedtexture_p.h SOURCES += \ $$PWD/compressedtexture/qsgcompressedatlastexture.cpp \ - $$PWD/compressedtexture/qsgcompressedtexture.cpp \ - $$PWD/compressedtexture/qsgpkmhandler.cpp \ - $$PWD/compressedtexture/qsgktxhandler.cpp + $$PWD/compressedtexture/qsgcompressedtexture.cpp } diff --git a/src/quick/scenegraph/util/qsgatlastexture.cpp b/src/quick/scenegraph/util/qsgatlastexture.cpp index 97203db867..66c6d3a882 100644 --- a/src/quick/scenegraph/util/qsgatlastexture.cpp +++ b/src/quick/scenegraph/util/qsgatlastexture.cpp @@ -143,11 +143,11 @@ QSGTexture *Manager::create(const QImage &image, bool hasAlphaChannel) QSGTexture *Manager::create(const QSGCompressedTextureFactory *factory) { QSGTexture *t = nullptr; - if (!qsgEnableCompressedAtlas() || !factory->m_textureData || !factory->m_textureData->isValid()) + if (!qsgEnableCompressedAtlas() || !factory->m_textureData.isValid()) return t; // TODO: further abstract the atlas and remove this restriction - unsigned int format = factory->m_textureData->format; + unsigned int format = factory->m_textureData.glInternalFormat(); switch (format) { case QOpenGLTexture::RGB8_ETC1: case QOpenGLTexture::RGB8_ETC2: @@ -158,15 +158,15 @@ QSGTexture *Manager::create(const QSGCompressedTextureFactory *factory) return t; } - QSize size = factory->m_textureData->size; + QSize size = factory->m_textureData.size(); if (size.width() < m_atlas_size_limit && size.height() < m_atlas_size_limit) { QHash<unsigned int, QSGCompressedAtlasTexture::Atlas*>::iterator i = m_atlases.find(format); if (i == m_atlases.end()) i = m_atlases.insert(format, new QSGCompressedAtlasTexture::Atlas(m_atlas_size, format)); // must be multiple of 4 QSize paddedSize(((size.width() + 3) / 4) * 4, ((size.height() + 3) / 4) * 4); - QByteArray data = factory->m_textureData->data; - t = i.value()->create(data, factory->m_textureData->sizeInBytes(), factory->m_textureData->dataOffset, size, paddedSize); + QByteArray data = factory->m_textureData.data(); + t = i.value()->create(data, factory->m_textureData.dataLength(), factory->m_textureData.dataOffset(), size, paddedSize); } return t; } diff --git a/src/quick/scenegraph/util/qsgtexturereader.cpp b/src/quick/scenegraph/util/qsgtexturereader.cpp index 8e95f27120..27ba119f63 100644 --- a/src/quick/scenegraph/util/qsgtexturereader.cpp +++ b/src/quick/scenegraph/util/qsgtexturereader.cpp @@ -38,36 +38,37 @@ ****************************************************************************/ #include "qsgtexturereader_p.h" - -#include <private/qtquickglobal_p.h> - -#include <private/qsgtexturefilehandler_p.h> - -#if QT_CONFIG(opengl) -#include <private/qsgpkmhandler_p.h> -#include <private/qsgktxhandler_p.h> -#endif - -#include <QFileInfo> +#include <private/qsgcompressedtexture_p.h> +#include <private/qtexturefilereader_p.h> QT_BEGIN_NAMESPACE QSGTextureReader::QSGTextureReader(QIODevice *device, const QString &fileName) - : m_device(device), m_fileInfo(fileName) { +#if QT_CONFIG(opengl) + m_reader = new QTextureFileReader(device, fileName); +#else + Q_UNUSED(device); + Q_UNUSED(fileName); +#endif } QSGTextureReader::~QSGTextureReader() { - delete m_handler; + delete m_reader; } QQuickTextureFactory *QSGTextureReader::read() { #if QT_CONFIG(opengl) - if (!isTexture()) + if (!m_reader) + return nullptr; + + QTextureFileData texData = m_reader->read(); + if (!texData.isValid()) return nullptr; - return m_handler->read(); + + return new QSGCompressedTextureFactory(texData); #else return nullptr; #endif @@ -75,41 +76,12 @@ QQuickTextureFactory *QSGTextureReader::read() bool QSGTextureReader::isTexture() { -#if QT_CONFIG(opengl) - if (!checked) { - checked = true; - if (!init()) - return false; - - QByteArray headerBlock = m_device->peek(64); - QByteArray suffix = m_fileInfo.suffix().toLower().toLatin1(); - QByteArray logName = m_fileInfo.fileName().toUtf8(); - - // Currently the handlers are hardcoded; later maybe a list of plugins - if (QSGPkmHandler::canRead(suffix, headerBlock)) { - m_handler = new QSGPkmHandler(m_device, logName); - } else if (QSGKtxHandler::canRead(suffix, headerBlock)) { - m_handler = new QSGKtxHandler(m_device, logName); - } - // else if OtherHandler::canRead() ...etc. - } - return (m_handler != nullptr); -#else - return false; -#endif + return m_reader ? m_reader->canRead() : false; } QList<QByteArray> QSGTextureReader::supportedFileFormats() { - // Hardcoded for now - return {QByteArrayLiteral("pkm"), QByteArrayLiteral("ktx")}; -} - -bool QSGTextureReader::init() -{ - if (!m_device) - return false; - return m_device->isReadable(); + return QTextureFileReader::supportedFileFormats(); } QT_END_NAMESPACE diff --git a/src/quick/scenegraph/util/qsgtexturereader_p.h b/src/quick/scenegraph/util/qsgtexturereader_p.h index 19e33bf5c3..20c17fce50 100644 --- a/src/quick/scenegraph/util/qsgtexturereader_p.h +++ b/src/quick/scenegraph/util/qsgtexturereader_p.h @@ -58,7 +58,7 @@ QT_BEGIN_NAMESPACE class QIODevice; class QQuickTextureFactory; -class QSGTextureFileHandler; +class QTextureFileReader; class QSGTextureReader { @@ -75,11 +75,7 @@ public: static QList<QByteArray> supportedFileFormats(); private: - bool init(); - QIODevice *m_device = nullptr; - QFileInfo m_fileInfo; - QSGTextureFileHandler *m_handler = nullptr; - bool checked = false; + QTextureFileReader *m_reader = nullptr; }; QT_END_NAMESPACE |