From f626f32c91781ee3fe3aaa7209114efb9d6e3e7c Mon Sep 17 00:00:00 2001 From: Michael Brasser Date: Fri, 1 Feb 2013 09:01:43 -0600 Subject: Add ETC1 texture provider example. This code was removed as a labs import in commit 732a86a11dbc20079e33c289af72762ce0125ced. Bring it back as an example. Change-Id: I057d721117dc75256c4a7229c4f8fad8dc9ac6b2 Reviewed-by: Gunnar Sletta --- examples/quick/textureprovider/Label.qml | 49 ++++++ examples/quick/textureprovider/etcprovider.cpp | 193 +++++++++++++++++++++ examples/quick/textureprovider/etcprovider.h | 80 +++++++++ examples/quick/textureprovider/images/qt-logo.jpg | Bin 0 -> 19365 bytes examples/quick/textureprovider/images/qt-logo.pkm | Bin 0 -> 32784 bytes examples/quick/textureprovider/main.cpp | 56 ++++++ examples/quick/textureprovider/textureprovider.pro | 13 ++ examples/quick/textureprovider/textureprovider.qml | 65 +++++++ examples/quick/textureprovider/textureprovider.qrc | 8 + 9 files changed, 464 insertions(+) create mode 100644 examples/quick/textureprovider/Label.qml create mode 100644 examples/quick/textureprovider/etcprovider.cpp create mode 100644 examples/quick/textureprovider/etcprovider.h create mode 100644 examples/quick/textureprovider/images/qt-logo.jpg create mode 100644 examples/quick/textureprovider/images/qt-logo.pkm create mode 100644 examples/quick/textureprovider/main.cpp create mode 100644 examples/quick/textureprovider/textureprovider.pro create mode 100644 examples/quick/textureprovider/textureprovider.qml create mode 100644 examples/quick/textureprovider/textureprovider.qrc (limited to 'examples/quick') diff --git a/examples/quick/textureprovider/Label.qml b/examples/quick/textureprovider/Label.qml new file mode 100644 index 0000000000..990ac247dc --- /dev/null +++ b/examples/quick/textureprovider/Label.qml @@ -0,0 +1,49 @@ +/**************************************************************************** +** +** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +import QtQuick 2.0 + +Text { + font.pixelSize: 26 + style: Text.Outline; styleColor: "white" + anchors.horizontalCenter: parent.horizontalCenter + anchors.bottom: parent.bottom + anchors.bottomMargin: 20 +} diff --git a/examples/quick/textureprovider/etcprovider.cpp b/examples/quick/textureprovider/etcprovider.cpp new file mode 100644 index 0000000000..0bb072ffaf --- /dev/null +++ b/examples/quick/textureprovider/etcprovider.cpp @@ -0,0 +1,193 @@ +/**************************************************************************** +** +** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the demonstration applications 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 Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/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 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "etcprovider.h" + +#include +#include +#include + +//#define ETC_DEBUG + +#ifndef GL_ETC1_RGB8_OES + #define GL_ETC1_RGB8_OES 0x8d64 +#endif + +typedef struct { + char aName[6]; + unsigned short iBlank; + /* NB: Beware endianness issues here. */ + unsigned char iPaddedWidthMSB; + unsigned char iPaddedWidthLSB; + unsigned char iPaddedHeightMSB; + unsigned char iPaddedHeightLSB; + unsigned char iWidthMSB; + unsigned char iWidthLSB; + unsigned char iHeightMSB; + unsigned char iHeightLSB; +} ETCHeader; + +unsigned short getWidth(ETCHeader *pHeader) +{ + return (pHeader->iWidthMSB << 8) | pHeader->iWidthLSB; +} + +unsigned short getHeight(ETCHeader *pHeader) +{ + return (pHeader->iHeightMSB << 8) | pHeader->iHeightLSB; +} + +unsigned short getPaddedWidth(ETCHeader *pHeader) +{ + return (pHeader->iPaddedWidthMSB << 8) | pHeader->iPaddedWidthLSB; +} + +unsigned short getPaddedHeight(ETCHeader *pHeader) +{ + return (pHeader->iPaddedHeightMSB << 8) | pHeader->iPaddedHeightLSB; +} + +EtcTexture::EtcTexture() + : m_texture_id(0) +{ + +} + +EtcTexture::~EtcTexture() +{ + if (m_texture_id) + glDeleteTextures(1, &m_texture_id); +} + +void EtcTexture::bind() +{ + if (m_texture_id) { + glBindTexture(GL_TEXTURE_2D, m_texture_id); + return; + } + + glGenTextures(1, &m_texture_id); + glBindTexture(GL_TEXTURE_2D, m_texture_id); + +#ifdef ETC_DEBUG + qDebug() << "glCompressedTexImage2D, width: " << m_size.width() << "height" << m_size.height() << + "paddedWidth: " << m_paddedSize.width() << "paddedHeight: " << m_paddedSize.height(); +#endif + + QOpenGLContext *ctx = QOpenGLContext::currentContext(); + Q_ASSERT(ctx != 0); + ctx->functions()->glCompressedTexImage2D(GL_TEXTURE_2D, 0, GL_ETC1_RGB8_OES, + m_size.width(), m_size.height(), 0, + (m_paddedSize.width() * m_paddedSize.height()) >> 1, + m_data.data() + 16); + + // Gracefully fail in case of an error... + GLuint error = glGetError(); + if (error != GL_NO_ERROR) { + qDebug () << "glCompressedTexImage2D for compressed texture failed, error: " << error; + glBindTexture(GL_TEXTURE_2D, 0); + glDeleteTextures(1, &m_texture_id); + m_texture_id = 0; + return; + } + + updateBindOptions(true); +} + +class QEtcTextureFactory : public QQuickTextureFactory +{ +public: + QByteArray m_data; + QSize m_size; + QSize m_paddedSize; + + QSize textureSize() const { return m_size; } + int textureByteCount() const { return m_data.size(); } + + QSGTexture *createTexture(QQuickWindow *) const { + EtcTexture *texture = new EtcTexture; + texture->m_data = m_data; + texture->m_size = m_size; + texture->m_paddedSize = m_paddedSize; + return texture; + } +}; + +QQuickTextureFactory *EtcProvider::requestTexture(const QString &id, QSize *size, const QSize &requestedSize) +{ + Q_UNUSED(requestedSize); + QEtcTextureFactory *ret = 0; + + size->setHeight(0); + size->setWidth(0); + + // resolve paths relative to qrc file + QFile file(QLatin1String(":/textureprovider/") + id); +#ifdef ETC_DEBUG + qDebug() << "requestTexture opening file: " << id; +#endif + if (file.open(QIODevice::ReadOnly)) { + ret = new QEtcTextureFactory; + ret->m_data = file.readAll(); + if (!ret->m_data.isEmpty()) { + ETCHeader *pETCHeader = NULL; + pETCHeader = (ETCHeader *)ret->m_data.data(); + size->setHeight(getHeight(pETCHeader)); + size->setWidth(getWidth(pETCHeader)); + ret->m_size = *size; + ret->m_paddedSize.setHeight(getPaddedHeight(pETCHeader)); + ret->m_paddedSize.setWidth(getPaddedWidth(pETCHeader)); + } + else { + delete ret; + ret = 0; + } + } + +#ifdef ETC_DEBUG + if (ret) + qDebug() << "requestTexture returning: " << ret->m_data.length() << ", bytes; width: " << size->width() << ", height: " << size->height(); + else + qDebug () << "File not found."; +#endif + + return ret; +} diff --git a/examples/quick/textureprovider/etcprovider.h b/examples/quick/textureprovider/etcprovider.h new file mode 100644 index 0000000000..4215ea297b --- /dev/null +++ b/examples/quick/textureprovider/etcprovider.h @@ -0,0 +1,80 @@ +/**************************************************************************** +** +** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the demonstration applications 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 Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/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 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef ETCPROVIDER_H +#define ETCPROVIDER_H + +#include +#include +#include + +class EtcProvider : public QQuickImageProvider +{ +public: + EtcProvider() + : QQuickImageProvider(QQuickImageProvider::Texture) + {} + + QQuickTextureFactory *requestTexture(const QString &id, QSize *size, const QSize &requestedSize); +}; + +class EtcTexture : public QSGTexture +{ + Q_OBJECT +public: + EtcTexture(); + ~EtcTexture(); + + void bind(); + + QSize textureSize() const { return m_size; } + int textureId() const { return m_texture_id; } + + bool hasAlphaChannel() const { return false; } + bool hasMipmaps() const { return false; } + + QByteArray m_data; + QSize m_size; + QSize m_paddedSize; + GLuint m_texture_id; +}; + +#endif // ETCPROVIDER_H diff --git a/examples/quick/textureprovider/images/qt-logo.jpg b/examples/quick/textureprovider/images/qt-logo.jpg new file mode 100644 index 0000000000..32d151c2c5 Binary files /dev/null and b/examples/quick/textureprovider/images/qt-logo.jpg differ diff --git a/examples/quick/textureprovider/images/qt-logo.pkm b/examples/quick/textureprovider/images/qt-logo.pkm new file mode 100644 index 0000000000..3329031ab2 Binary files /dev/null and b/examples/quick/textureprovider/images/qt-logo.pkm differ diff --git a/examples/quick/textureprovider/main.cpp b/examples/quick/textureprovider/main.cpp new file mode 100644 index 0000000000..d9d4fb0d38 --- /dev/null +++ b/examples/quick/textureprovider/main.cpp @@ -0,0 +1,56 @@ +/**************************************************************************** +** +** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include +#include + +#include "etcprovider.h" + +int main(int argc, char* argv[]) +{ + QGuiApplication app(argc,argv); + QQuickView view; + + view.engine()->addImageProvider("etc", new EtcProvider()); + + view.setSource(QUrl("qrc:///textureprovider/textureprovider.qml")); + view.show(); + return app.exec(); +} diff --git a/examples/quick/textureprovider/textureprovider.pro b/examples/quick/textureprovider/textureprovider.pro new file mode 100644 index 0000000000..5d9825f7ac --- /dev/null +++ b/examples/quick/textureprovider/textureprovider.pro @@ -0,0 +1,13 @@ +TEMPLATE = app + +QT += quick qml + +HEADERS += etcprovider.h + +SOURCES += main.cpp \ + etcprovider.cpp + +RESOURCES += textureprovider.qrc + +target.path = $$[QT_INSTALL_EXAMPLES]/quick/textureprovider +INSTALLS += target diff --git a/examples/quick/textureprovider/textureprovider.qml b/examples/quick/textureprovider/textureprovider.qml new file mode 100644 index 0000000000..9854bef229 --- /dev/null +++ b/examples/quick/textureprovider/textureprovider.qml @@ -0,0 +1,65 @@ +/**************************************************************************** +** +** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +import QtQuick 2.0 + +Row { + width: 512 + height: 256 + + Image { + source: "images/qt-logo.jpg" + + Label { text: "Original Image" } + } + + Image { + source: "image://etc/images/qt-logo.pkm" + + Label { id: clabel; text: "Compressed Texture" } + Text { + text: "(requires ETC1 support)" + anchors.horizontalCenter: clabel.horizontalCenter + anchors.top: clabel.bottom + font.pixelSize: 14 + color: "white" + } + } +} diff --git a/examples/quick/textureprovider/textureprovider.qrc b/examples/quick/textureprovider/textureprovider.qrc new file mode 100644 index 0000000000..77a568ba1d --- /dev/null +++ b/examples/quick/textureprovider/textureprovider.qrc @@ -0,0 +1,8 @@ + + + images/qt-logo.jpg + images/qt-logo.pkm + textureprovider.qml + Label.qml + + -- cgit v1.2.3