1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the Declarative module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include "qetcprovider.h"
47 #include <qopenglfunctions.h>
48 #include <qopenglcontext.h>
54 unsigned short iBlank;
55 /* NB: Beware endianess issues here. */
56 unsigned char iPaddedWidthMSB;
57 unsigned char iPaddedWidthLSB;
58 unsigned char iPaddedHeightMSB;
59 unsigned char iPaddedHeightLSB;
60 unsigned char iWidthMSB;
61 unsigned char iWidthLSB;
62 unsigned char iHeightMSB;
63 unsigned char iHeightLSB;
67 unsigned short getWidth(ETCHeader *pHeader)
69 return (pHeader->iWidthMSB << 8) | pHeader->iWidthLSB;
72 unsigned short getHeight(ETCHeader *pHeader)
74 return (pHeader->iHeightMSB << 8) | pHeader->iHeightLSB;
77 unsigned short getPaddedWidth(ETCHeader *pHeader)
79 return (pHeader->iPaddedWidthMSB << 8) | pHeader->iPaddedWidthLSB;
82 unsigned short getPaddedHeight(ETCHeader *pHeader)
84 return (pHeader->iPaddedHeightMSB << 8) | pHeader->iPaddedHeightLSB;
87 enum {GL_ETC1_RGB8_OES=0x8d64};
89 EtcTexture::EtcTexture()
95 EtcTexture::~EtcTexture()
98 glDeleteTextures(1, &m_texture_id);
102 void EtcTexture::bind()
105 glBindTexture(GL_TEXTURE_2D, m_texture_id);
110 printf("EtcTextureProvider: about to update that texture...\n");
113 glGenTextures(1, &m_texture_id);
115 glBindTexture(GL_TEXTURE_2D, m_texture_id);
118 qDebug() << "glCompressedTexImage2D, width: " << m_size.width() << "height" << m_size.height() <<
119 "paddedWidth: " << m_paddedSize.width() << "paddedHeight: " << m_paddedSize.height();
122 QOpenGLContext *ctx = QOpenGLContext::currentContext();
124 ctx->functions()->glCompressedTexImage2D(GL_TEXTURE_2D, 0, GL_ETC1_RGB8_OES,
125 m_size.width(), m_size.height(), 0,
126 (m_paddedSize.width() * m_paddedSize.height()) >> 1,
129 // Gracefully fail in case of an error...
130 GLuint error = glGetError();
131 if (error != GL_NO_ERROR) {
132 qDebug () << "glCompressedTexImage2D for compressed texture failed, error: " << error;
133 glBindTexture(GL_TEXTURE_2D, 0);
134 glDeleteTextures(1, &m_texture_id);
138 updateBindOptions(true);
141 QSize EtcTexture::textureSize() const
147 class QEtcTextureFactory : public QDeclarativeTextureFactory
154 QSize textureSize() const {
158 int textureByteCount() const {
159 return m_data.size();
162 QSGTexture *createTexture() const {
163 EtcTexture *texture = new EtcTexture;
164 texture->m_data = m_data;
165 texture->m_size = m_size;
166 texture->m_paddedSize = m_paddedSize;
173 QDeclarativeTextureFactory *QEtcProvider::requestTexture(const QString &id, QSize *size, const QSize &requestedSize)
175 Q_UNUSED(requestedSize);
176 QEtcTextureFactory *ret = 0;
183 qDebug() << "requestTexture opening file: " << id;
185 if (file.open(QIODevice::ReadOnly)) {
186 ret = new QEtcTextureFactory;
187 ret->m_data = file.readAll();
188 if (!ret->m_data.isEmpty()) {
189 ETCHeader *pETCHeader = NULL;
190 pETCHeader = (ETCHeader *)ret->m_data.data();
191 size->setHeight(getHeight(pETCHeader));
192 size->setWidth(getWidth(pETCHeader));
194 ret->m_paddedSize.setHeight(getPaddedHeight(pETCHeader));
195 ret->m_paddedSize.setWidth(getPaddedWidth(pETCHeader));
205 qDebug() << "requestTexture returning: " << ret->m_data.length() << ", bytes; width: " << size->width() << ", height: " << size->height();
207 qDebug () << "File not found.";