1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the Declarative module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include "qetcprovider.h"
47 #include <qglfunctions.h>
53 unsigned short iBlank;
54 /* NB: Beware endianess issues here. */
55 unsigned char iPaddedWidthMSB;
56 unsigned char iPaddedWidthLSB;
57 unsigned char iPaddedHeightMSB;
58 unsigned char iPaddedHeightLSB;
59 unsigned char iWidthMSB;
60 unsigned char iWidthLSB;
61 unsigned char iHeightMSB;
62 unsigned char iHeightLSB;
66 unsigned short getWidth(ETCHeader *pHeader)
68 return (pHeader->iWidthMSB << 8) | pHeader->iWidthLSB;
71 unsigned short getHeight(ETCHeader *pHeader)
73 return (pHeader->iHeightMSB << 8) | pHeader->iHeightLSB;
76 unsigned short getPaddedWidth(ETCHeader *pHeader)
78 return (pHeader->iPaddedWidthMSB << 8) | pHeader->iPaddedWidthLSB;
81 unsigned short getPaddedHeight(ETCHeader *pHeader)
83 return (pHeader->iPaddedHeightMSB << 8) | pHeader->iPaddedHeightLSB;
86 enum {GL_ETC1_RGB8_OES=0x8d64};
88 EtcTexture::EtcTexture()
94 EtcTexture::~EtcTexture()
97 glDeleteTextures(1, &m_texture_id);
101 void EtcTexture::bind()
104 glBindTexture(GL_TEXTURE_2D, m_texture_id);
109 printf("EtcTextureProvider: about to update that texture...\n");
112 glGenTextures(1, &m_texture_id);
114 glBindTexture(GL_TEXTURE_2D, m_texture_id);
117 qDebug() << "glCompressedTexImage2D, width: " << m_size.width() << "height" << m_size.height() <<
118 "paddedWidth: " << m_paddedSize.width() << "paddedHeight: " << m_paddedSize.height();
121 const QGLContext *ctx = QGLContext::currentContext();
123 ctx->functions()->glCompressedTexImage2D(GL_TEXTURE_2D, 0, GL_ETC1_RGB8_OES,
124 m_size.width(), m_size.height(), 0,
125 (m_paddedSize.width() * m_paddedSize.height()) >> 1,
128 // Gracefully fail in case of an error...
129 GLuint error = glGetError();
130 if (error != GL_NO_ERROR) {
131 qDebug () << "glCompressedTexImage2D for compressed texture failed, error: " << error;
132 glBindTexture(GL_TEXTURE_2D, 0);
133 glDeleteTextures(1, &m_texture_id);
137 updateBindOptions(true);
140 QSize EtcTexture::textureSize() const
145 QSGTexture *QEtcProvider::requestTexture(const QString &id, QSize *size, const QSize &requestedSize)
147 Q_UNUSED(requestedSize);
155 qDebug() << "requestTexture opening file: " << id;
157 if (file.open(QIODevice::ReadOnly)) {
158 ret = new EtcTexture();
159 ret->m_data = file.readAll();
160 if (!ret->m_data.isEmpty()) {
161 ETCHeader *pETCHeader = NULL;
162 pETCHeader = (ETCHeader *)ret->m_data.data();
163 size->setHeight(getHeight(pETCHeader));
164 size->setWidth(getWidth(pETCHeader));
166 ret->m_paddedSize.setHeight(getPaddedHeight(pETCHeader));
167 ret->m_paddedSize.setWidth(getPaddedWidth(pETCHeader));
177 qDebug() << "requestTexture returning: " << ret->m_data.length() << ", bytes; width: " << size->width() << ", height: " << size->height();
179 qDebug () << "File not found.";