Update licenseheader text in source files for qtdeclarative Qt module
[profile/ivi/qtdeclarative.git] / src / imports / etcprovider / qetcprovider.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the Declarative module of the Qt Toolkit.
8 **
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.
17 **
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.
21 **
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.
29 **
30 ** Other Usage
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.
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 <qglfunctions.h>
48
49 QT_BEGIN_NAMESPACE
50
51 typedef struct {
52     char aName[6];
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;
63 } ETCHeader;
64
65
66 unsigned short getWidth(ETCHeader *pHeader)
67 {
68     return (pHeader->iWidthMSB << 8) | pHeader->iWidthLSB;
69 }
70
71 unsigned short getHeight(ETCHeader *pHeader)
72 {
73     return (pHeader->iHeightMSB << 8) | pHeader->iHeightLSB;
74 }
75
76 unsigned short getPaddedWidth(ETCHeader *pHeader)
77 {
78     return (pHeader->iPaddedWidthMSB << 8) | pHeader->iPaddedWidthLSB;
79 }
80
81 unsigned short getPaddedHeight(ETCHeader *pHeader)
82 {
83     return (pHeader->iPaddedHeightMSB << 8) | pHeader->iPaddedHeightLSB;
84 }
85
86 enum {GL_ETC1_RGB8_OES=0x8d64};
87
88 EtcTexture::EtcTexture()
89     : m_texture_id(0)
90 {
91
92 }
93
94 EtcTexture::~EtcTexture()
95 {
96     if (m_texture_id)
97         glDeleteTextures(1, &m_texture_id);
98 }
99
100
101 void EtcTexture::bind()
102 {
103     if (m_texture_id) {
104         glBindTexture(GL_TEXTURE_2D, m_texture_id);
105         return;
106     }
107
108 #ifdef ETC_DEBUG
109     printf("EtcTextureProvider: about to update that texture...\n");
110 #endif
111
112     glGenTextures(1, &m_texture_id);
113
114     glBindTexture(GL_TEXTURE_2D, m_texture_id);
115
116 #ifdef ETC_DEBUG
117     qDebug() << "glCompressedTexImage2D, width: " << m_size.width() << "height" << m_size.height() <<
118                 "paddedWidth: " << m_paddedSize.width() << "paddedHeight: " << m_paddedSize.height();
119 #endif
120
121     const QGLContext *ctx = QGLContext::currentContext();
122     Q_ASSERT(ctx != 0);
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,
126                                              m_data.data() + 16);
127
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);
134         m_texture_id = 0;
135         return;
136     }
137     updateBindOptions(true);
138 }
139
140 QSize EtcTexture::textureSize() const
141 {
142     return m_size;
143 }
144
145 QSGTexture *QEtcProvider::requestTexture(const QString &id, QSize *size, const QSize &requestedSize)
146 {
147     Q_UNUSED(requestedSize);
148     EtcTexture *ret = 0;
149
150     size->setHeight(0);
151     size->setWidth(0);
152
153     QFile file(id);
154 #ifdef ETC_DEBUG
155     qDebug() << "requestTexture opening file: " << id;
156 #endif
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));
165             ret->m_size = *size;
166             ret->m_paddedSize.setHeight(getPaddedHeight(pETCHeader));
167             ret->m_paddedSize.setWidth(getPaddedWidth(pETCHeader));
168         }
169         else {
170             free (ret);
171             ret = 0;
172         }
173     }
174
175 #ifdef ETC_DEBUG
176     if (ret)
177         qDebug() << "requestTexture returning: " << ret->m_data.length() << ", bytes; width: " << size->width() << ", height: " << size->height();
178     else
179         qDebug () << "File not found.";
180 #endif
181
182     return ret;
183 }
184
185 QT_END_NAMESPACE