Qt 4.8
qgltexturepool.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtOpenVG module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include "qgltexturepool_p.h"
43 #include "qpixmapdata_gl_p.h"
44 #include "qgl_p.h"
45 
47 
49 
51 
53 {
54 public:
56 
59 };
60 
62  : d_ptr(new QGLTexturePoolPrivate())
63 {
64 }
65 
67 {
68 }
69 
71 {
72  if (!qt_gl_texture_pool)
73  qt_gl_texture_pool = new QGLTexturePool();
74  return qt_gl_texture_pool;
75 }
76 
78  GLint level,
79  GLint internalformat,
80  GLsizei width,
81  GLsizei height,
82  GLenum format,
83  GLenum type,
84  QGLTexture *texture)
85 {
86  GLuint tex;
87  glGenTextures(1, &tex);
88  glBindTexture(target, tex);
89  do {
90  glTexImage2D(target, level, internalformat, width, height, 0, format, type, 0);
91  GLenum error = glGetError();
92  if (error == GL_NO_ERROR) {
93  if (texture)
94  moveToHeadOfLRU(texture);
95  return tex;
96  } else if (error != GL_OUT_OF_MEMORY) {
97  qWarning("QGLTexturePool: cannot create temporary texture because of invalid params");
98  return 0;
99  }
100  } while (reclaimSpace(internalformat, width, height, format, type, texture));
101  qWarning("QGLTexturePool: cannot reclaim sufficient space for a %dx%d texture",
102  width, height);
103  return 0;
104 }
105 
107  GLenum target,
108  GLint level,
109  GLint internalformat,
110  GLsizei width,
111  GLsizei height,
112  GLenum format,
113  GLenum type,
114  const GLvoid *data)
115 {
116  glBindTexture(target, tex);
117  do {
118  glTexImage2D(target, level, internalformat, width, height, 0, format, type, data);
119 
120  GLenum error = glGetError();
121  if (error == GL_NO_ERROR) {
122  return true;
123  } else if (error != GL_OUT_OF_MEMORY) {
124  qWarning("QGLTexturePool: cannot create permanent texture because of invalid params");
125  return false;
126  }
127  } while (reclaimSpace(internalformat, width, height, format, type, 0));
128  qWarning("QGLTexturePool: cannot reclaim sufficient space for a %dx%d texture",
129  width, height);
130  return 0;
131 }
132 
134 {
135  moveToHeadOfLRU(texture);
136  texture->inTexturePool = true;
137 }
138 
140 {
141  removeFromLRU(texture);
142  texture->inTexturePool = false;
143 }
144 
146  GLsizei width,
147  GLsizei height,
148  GLenum format,
149  GLenum type,
150  QGLTexture *texture)
151 {
152  Q_UNUSED(internalformat); // For future use in picking the best texture to eject.
153  Q_UNUSED(width);
154  Q_UNUSED(height);
155  Q_UNUSED(format);
156  Q_UNUSED(type);
157 
158  bool succeeded = false;
159  bool wasInLRU = false;
160  if (texture) {
161  wasInLRU = texture->inLRU;
162  moveToHeadOfLRU(texture);
163  }
164 
165  QGLTexture *lrutexture = textureLRU();
166  if (lrutexture && lrutexture != texture) {
167  if (lrutexture->boundPixmap)
168  lrutexture->boundPixmap->reclaimTexture();
169  else
170  QGLTextureCache::instance()->remove(lrutexture->boundKey);
171  succeeded = true;
172  }
173 
174  if (texture && !wasInLRU)
175  removeFromLRU(texture);
176 
177  return succeeded;
178 }
179 
181 {
183  QGLTexture *texture = d->lruLast;
184  while (texture) {
185  QGLTexture *prevLRU = texture->prevLRU;
186  texture->inTexturePool = false;
187  texture->inLRU = false;
188  texture->nextLRU = 0;
189  texture->prevLRU = 0;
190  if (texture->boundPixmap)
191  texture->boundPixmap->hibernate();
192  else
193  QGLTextureCache::instance()->remove(texture->boundKey);
194  texture = prevLRU;
195  }
196  d->lruFirst = 0;
197  d->lruLast = 0;
198 }
199 
201 {
203  if (texture->inLRU) {
204  if (!texture->prevLRU)
205  return; // Already at the head of the list.
206  removeFromLRU(texture);
207  }
208  texture->inLRU = true;
209  texture->nextLRU = d->lruFirst;
210  texture->prevLRU = 0;
211  if (d->lruFirst)
212  d->lruFirst->prevLRU = texture;
213  else
214  d->lruLast = texture;
215  d->lruFirst = texture;
216 }
217 
219 {
221  if (!texture->inLRU)
222  return;
223  if (texture->nextLRU)
224  texture->nextLRU->prevLRU = texture->prevLRU;
225  else
226  d->lruLast = texture->prevLRU;
227  if (texture->prevLRU)
228  texture->prevLRU->nextLRU = texture->nextLRU;
229  else
230  d->lruFirst = texture->nextLRU;
231  texture->inLRU = false;
232 }
233 
235 {
237  return d->lruLast;
238 }
239 
double d
Definition: qnumeric_p.h:62
#define GL_OUT_OF_MEMORY
bool createPermanentTexture(GLuint texture, GLenum target, GLint level, GLint internalformat, GLsizei width, GLsizei height, GLenum format, GLenum type, const GLvoid *data)
int type
Definition: qmetatype.cpp:239
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
void moveToHeadOfLRU(QGLTexture *texture)
static QGLTextureCache * instance()
Definition: qgl.cpp:1995
#define Q_OPENGL_EXPORT
Definition: qglobal.h:1455
#define error(msg)
Q_OPENGL_EXPORT QGLWidget * qt_gl_share_widget()
void useTexture(QGLTexture *texture)
void removeFromLRU(QGLTexture *texture)
#define Q_D(Class)
Definition: qglobal.h:2482
void detachTexture(QGLTexture *texture)
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
Q_CORE_EXPORT void qWarning(const char *,...)
virtual ~QGLTexturePool()
static const char * data(const QByteArray &arr)
static QGLTexturePool * instance()
static QGLTexturePool * qt_gl_texture_pool
QPixmapData * boundPixmap
Definition: qgl_p.h:614
bool reclaimSpace(GLint internalformat, GLsizei width, GLsizei height, GLenum format, GLenum type, QGLTexture *data)
The QGLWidget class is a widget for rendering OpenGL graphics.
Definition: qgl.h:474
unsigned int GLenum
Definition: main.cpp:50
QGLTexture * textureLRU()
int GLsizei
Definition: main.cpp:49
typedef GLint
Definition: glfunctions.h:67
GLuint createTexture(GLenum target, GLint level, GLint internalformat, GLsizei width, GLsizei height, GLenum format, GLenum type, QGLTexture *texture)
void remove(qint64 key)
Definition: qgl.cpp:1929
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729
#define GL_NO_ERROR