Remove "All rights reserved" line from license headers.
[profile/ivi/qtdeclarative.git] / src / imports / etcprovider / qetcprovider.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the Declarative module of the Qt Toolkit.
7 **
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.
16 **
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.
20 **
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.
28 **
29 ** Other Usage
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.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "qetcprovider.h"
43
44 #include <QtDebug>
45 #include <QFile>
46
47 #include <qopenglfunctions.h>
48 #include <qopenglcontext.h>
49
50 QT_BEGIN_NAMESPACE
51
52 typedef struct {
53     char aName[6];
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;
64 } ETCHeader;
65
66
67 unsigned short getWidth(ETCHeader *pHeader)
68 {
69     return (pHeader->iWidthMSB << 8) | pHeader->iWidthLSB;
70 }
71
72 unsigned short getHeight(ETCHeader *pHeader)
73 {
74     return (pHeader->iHeightMSB << 8) | pHeader->iHeightLSB;
75 }
76
77 unsigned short getPaddedWidth(ETCHeader *pHeader)
78 {
79     return (pHeader->iPaddedWidthMSB << 8) | pHeader->iPaddedWidthLSB;
80 }
81
82 unsigned short getPaddedHeight(ETCHeader *pHeader)
83 {
84     return (pHeader->iPaddedHeightMSB << 8) | pHeader->iPaddedHeightLSB;
85 }
86
87 enum {GL_ETC1_RGB8_OES=0x8d64};
88
89 EtcTexture::EtcTexture()
90     : m_texture_id(0)
91 {
92
93 }
94
95 EtcTexture::~EtcTexture()
96 {
97     if (m_texture_id)
98         glDeleteTextures(1, &m_texture_id);
99 }
100
101
102 void EtcTexture::bind()
103 {
104     if (m_texture_id) {
105         glBindTexture(GL_TEXTURE_2D, m_texture_id);
106         return;
107     }
108
109 #ifdef ETC_DEBUG
110     printf("EtcTextureProvider: about to update that texture...\n");
111 #endif
112
113     glGenTextures(1, &m_texture_id);
114
115     glBindTexture(GL_TEXTURE_2D, m_texture_id);
116
117 #ifdef ETC_DEBUG
118     qDebug() << "glCompressedTexImage2D, width: " << m_size.width() << "height" << m_size.height() <<
119                 "paddedWidth: " << m_paddedSize.width() << "paddedHeight: " << m_paddedSize.height();
120 #endif
121
122     QOpenGLContext *ctx = QOpenGLContext::currentContext();
123     Q_ASSERT(ctx != 0);
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,
127                                              m_data.data() + 16);
128
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);
135         m_texture_id = 0;
136         return;
137     }
138     updateBindOptions(true);
139 }
140
141 QSize EtcTexture::textureSize() const
142 {
143     return m_size;
144 }
145
146
147 class QEtcTextureFactory : public QDeclarativeTextureFactory
148 {
149 public:
150     QByteArray m_data;
151     QSize m_size;
152     QSize m_paddedSize;
153
154     QSize textureSize() const {
155         return m_size;
156     }
157
158     int textureByteCount() const {
159         return m_data.size();
160     }
161
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;
167         return texture;
168     }
169
170 };
171
172
173 QDeclarativeTextureFactory *QEtcProvider::requestTexture(const QString &id, QSize *size, const QSize &requestedSize)
174 {
175     Q_UNUSED(requestedSize);
176     QEtcTextureFactory *ret = 0;
177
178     size->setHeight(0);
179     size->setWidth(0);
180
181     QFile file(id);
182 #ifdef ETC_DEBUG
183     qDebug() << "requestTexture opening file: " << id;
184 #endif
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));
193             ret->m_size = *size;
194             ret->m_paddedSize.setHeight(getPaddedHeight(pETCHeader));
195             ret->m_paddedSize.setWidth(getPaddedWidth(pETCHeader));
196         }
197         else {
198             delete ret;
199             ret = 0;
200         }
201     }
202
203 #ifdef ETC_DEBUG
204     if (ret)
205         qDebug() << "requestTexture returning: " << ret->m_data.length() << ", bytes; width: " << size->width() << ", height: " << size->height();
206     else
207         qDebug () << "File not found.";
208 #endif
209
210     return ret;
211 }
212
213 QT_END_NAMESPACE