diff options
author | Eirik Aavitsland <eirik.aavitsland@qt.io> | 2017-11-27 19:03:16 +0100 |
---|---|---|
committer | Michael Brasser <michael.brasser@live.com> | 2018-01-27 02:42:16 +0000 |
commit | ae7c66b8f50103c212789805efccba208a55ea4b (patch) | |
tree | 8640661ae688829d03d1f3d77bda3a1fc0888865 /src/quick/scenegraph/util | |
parent | 0f8e0310b7ad874a1fbdb3def4cf8d7da7f943e5 (diff) |
Refactor compressed texture file reading
This commit expands on the basic support for compressed texture files
that was added in 432e27a. It prepares for adding support for other
file types than pkm by declaring a common QSGTextureFileHandler
interface, and by factoring out reusable code parts in a new
QSGCompressedTexture class.
It also adds some more systematic logging and checking for error
conditions.
Change-Id: Ie5070d0a6df757fee0753f53adbe23a33d51a634
Reviewed-by: Michael Brasser <michael.brasser@live.com>
Diffstat (limited to 'src/quick/scenegraph/util')
-rw-r--r-- | src/quick/scenegraph/util/qsgtexturereader.cpp | 49 | ||||
-rw-r--r-- | src/quick/scenegraph/util/qsgtexturereader_p.h | 18 |
2 files changed, 48 insertions, 19 deletions
diff --git a/src/quick/scenegraph/util/qsgtexturereader.cpp b/src/quick/scenegraph/util/qsgtexturereader.cpp index 61729ada18..cf4edf29b8 100644 --- a/src/quick/scenegraph/util/qsgtexturereader.cpp +++ b/src/quick/scenegraph/util/qsgtexturereader.cpp @@ -45,38 +45,55 @@ #include <private/qsgpkmhandler_p.h> #endif +#include <QFileInfo> + QT_BEGIN_NAMESPACE -QSGTextureReader::QSGTextureReader() +QSGTextureReader::QSGTextureReader(QIODevice *device, const QString &fileName) + : m_device(device), m_fileInfo(fileName) { - } -QQuickTextureFactory *QSGTextureReader::read(QIODevice *device, const QByteArray &format) +QQuickTextureFactory *QSGTextureReader::read() { #if QT_CONFIG(opengl) - if (format == QByteArrayLiteral("pkm")) { - QSGPkmHandler handler; - return handler.read(device); - } + if (!isTexture()) + return nullptr; + return m_handler->read(); #else - Q_UNUSED(device) - Q_UNUSED(format) -#endif return nullptr; +#endif } -bool QSGTextureReader::isTexture(QIODevice *device, const QByteArray &format) +bool QSGTextureReader::isTexture() { #if QT_CONFIG(opengl) - if (format == QByteArrayLiteral("pkm")) { - return device->peek(4) == QByteArrayLiteral("PKM "); + 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 OtherHandler::canRead() ...etc. } + return (m_handler != nullptr); #else - Q_UNUSED(device) - Q_UNUSED(format) -#endif return false; +#endif +} + +bool QSGTextureReader::init() +{ + if (!m_device) + return false; + return m_device->isReadable(); } QT_END_NAMESPACE diff --git a/src/quick/scenegraph/util/qsgtexturereader_p.h b/src/quick/scenegraph/util/qsgtexturereader_p.h index 7d2fc314a6..8251e4c696 100644 --- a/src/quick/scenegraph/util/qsgtexturereader_p.h +++ b/src/quick/scenegraph/util/qsgtexturereader_p.h @@ -52,19 +52,31 @@ // #include <QString> +#include <QFileInfo> QT_BEGIN_NAMESPACE class QIODevice; class QQuickTextureFactory; +class QSGTextureFileHandler; class QSGTextureReader { public: - QSGTextureReader(); + QSGTextureReader(QIODevice *device, const QString &fileName = QString()); - static QQuickTextureFactory *read(QIODevice *device, const QByteArray &format); - static bool isTexture(QIODevice *device, const QByteArray &format); + QQuickTextureFactory *read(); + bool isTexture(); + + // TBD access function to params + // TBD ask for identified fmt + +private: + bool init(); + QIODevice *m_device = nullptr; + QFileInfo m_fileInfo; + QSGTextureFileHandler *m_handler = nullptr; + bool checked = false; }; QT_END_NAMESPACE |