Qt 4.8
Public Functions | Public Variables | List of all members
QGLTexture Class Reference

#include <qgl_p.h>

Public Functions

 QGLTexture (QGLContext *ctx=0, GLuint tx_id=0, GLenum tx_target=GL_TEXTURE_2D, QGLContext::BindOptions opt=QGLContext::DefaultBindOption)
 
 ~QGLTexture ()
 
bool canBindCompressedTexture (const char *buf, int len, const char *format, bool *hasAlpha)
 
QSize bindCompressedTexture (const QString &fileName, const char *format=0)
 
QSize bindCompressedTexture (const char *buf, int len, const char *format=0)
 
QSize bindCompressedTextureDDS (const char *buf, int len)
 
QSize bindCompressedTexturePVR (const char *buf, int len)
 

Public Variables

QPixmapDataboundPixmap
 
QGLContextcontext
 
GLuint id
 
QGLContext::BindOptions options
 
GLenum target
 

Detailed Description

Definition at line 572 of file qgl_p.h.

Constructors and Destructors

◆ QGLTexture()

QGLTexture::QGLTexture ( QGLContext ctx = 0,
GLuint  tx_id = 0,
GLenum  tx_target = GL_TEXTURE_2D,
QGLContext::BindOptions  opt = QGLContext::DefaultBindOption 
)
inline

Definition at line 574 of file qgl_p.h.

576  : context(ctx),
577  id(tx_id),
578  target(tx_target),
579  options(opt)
580 #if defined(Q_WS_X11)
581  , boundPixmap(0)
582 #elif defined(Q_OS_SYMBIAN)
583  , boundPixmap(0)
584  , boundKey(0)
585  , nextLRU(0)
586  , prevLRU(0)
587  , inLRU(false)
588  , failedToAlloc(false)
589  , inTexturePool(false)
590 #endif
591  {}
GLenum target
Definition: qgl_p.h:609
QGLContext::BindOptions options
Definition: qgl_p.h:611
GLuint id
Definition: qgl_p.h:608
QPixmapData * boundPixmap
Definition: qgl_p.h:614
QGLContext * context
Definition: qgl_p.h:607

◆ ~QGLTexture()

QGLTexture::~QGLTexture ( )
inline

Definition at line 593 of file qgl_p.h.

593  {
594 #ifdef Q_OS_SYMBIAN
595  freeTexture();
596 #else
598  Q_ASSERT(context);
599 #if !defined(Q_WS_X11)
601 #endif
603  }
604 #endif
605  }
QScopedPointer< QGLContextPrivate > d_ptr
Definition: qgl.h:430
void emitFreeTexture(QGLContext *context, QPixmapData *boundPixmap, GLuint id)
Definition: qgl_p.h:529
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
QGLContext::BindOptions options
Definition: qgl_p.h:611
QPixmapData * boundPixmap
Definition: qgl_p.h:614
QGLContext * context
Definition: qgl_p.h:607
QGLTextureDestroyer * texture_destroyer
Definition: qgl_p.h:460

Functions

◆ bindCompressedTexture() [1/2]

QSize QGLTexture::bindCompressedTexture ( const QString fileName,
const char *  format = 0 
)

Definition at line 5996 of file qgl.cpp.

Referenced by QGLPixmapData::fromData(), and QGLPixmapData::fromFile().

5997 {
5998  QFile file(fileName);
5999  if (!file.open(QIODevice::ReadOnly))
6000  return QSize();
6001  QByteArray contents = file.readAll();
6002  file.close();
6003  return bindCompressedTexture
6004  (contents.constData(), contents.size(), format);
6005 }
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
const char * constData() const
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:433
The QFile class provides an interface for reading from and writing to files.
Definition: qfile.h:65
int size() const
Returns the number of bytes in this byte array.
Definition: qbytearray.h:402
The QSize class defines the size of a two-dimensional object using integer point precision.
Definition: qsize.h:53
QSize bindCompressedTexture(const QString &fileName, const char *format=0)
Definition: qgl.cpp:5996

◆ bindCompressedTexture() [2/2]

QSize QGLTexture::bindCompressedTexture ( const char *  buf,
int  len,
const char *  format = 0 
)

Definition at line 6097 of file qgl.cpp.

6098 {
6099  if (QSysInfo::ByteOrder != QSysInfo::LittleEndian) {
6100  // Compressed texture loading only supported on little-endian
6101  // systems such as x86 and ARM at the moment.
6102  return QSize();
6103  }
6104 #if !defined(QT_OPENGL_ES)
6105  if (!glCompressedTexImage2D) {
6107  qWarning("QGLContext::bindTexture(): The GL implementation does "
6108  "not support texture compression extensions.");
6109  return QSize();
6110  }
6111  glCompressedTexImage2D = (_glCompressedTexImage2DARB) ctx->getProcAddress(QLatin1String("glCompressedTexImage2DARB"));
6112  if (!glCompressedTexImage2D) {
6113  qWarning("QGLContext::bindTexture(): could not resolve "
6114  "glCompressedTexImage2DARB.");
6115  return QSize();
6116  }
6117  }
6118 #endif
6119  if (!format) {
6120  // Auto-detect the format from the header.
6121  if (len >= 4 && !qstrncmp(buf, "DDS ", 4))
6122  return bindCompressedTextureDDS(buf, len);
6123  else if (len >= 52 && !qstrncmp(buf + 44, "PVR!", 4))
6124  return bindCompressedTexturePVR(buf, len);
6125  } else {
6126  // Validate the format against the header.
6127  if (!qstricmp(format, "DDS")) {
6128  if (len >= 4 && !qstrncmp(buf, "DDS ", 4))
6129  return bindCompressedTextureDDS(buf, len);
6130  } else if (!qstricmp(format, "PVR") || !qstricmp(format, "ETC1")) {
6131  if (len >= 52 && !qstrncmp(buf + 44, "PVR!", 4))
6132  return bindCompressedTexturePVR(buf, len);
6133  }
6134  }
6135  return QSize();
6136 }
#define glCompressedTexImage2D
QSize bindCompressedTextureDDS(const char *buf, int len)
Definition: qgl.cpp:6138
Q_CORE_EXPORT void qWarning(const char *,...)
The QLatin1String class provides a thin wrapper around an US-ASCII/Latin-1 encoded string literal...
Definition: qstring.h:654
QSize bindCompressedTexturePVR(const char *buf, int len)
Definition: qgl.cpp:6213
#define ctx
Definition: qgl.cpp:6094
int qstrncmp(const char *str1, const char *str2, uint len)
Definition: qbytearray.h:101
static Extensions glExtensions()
Definition: qgl.cpp:5781
The QSize class defines the size of a two-dimensional object using integer point precision.
Definition: qsize.h:53
Q_CORE_EXPORT int qstricmp(const char *, const char *)
void(APIENTRY * _glCompressedTexImage2DARB)(GLenum, GLint, GLenum, GLsizei, GLsizei, GLint, GLsizei, const GLvoid *)

◆ bindCompressedTextureDDS()

QSize QGLTexture::bindCompressedTextureDDS ( const char *  buf,
int  len 
)

Definition at line 6138 of file qgl.cpp.

6139 {
6140  // We only support 2D texture loading at present.
6141  if (target != GL_TEXTURE_2D)
6142  return QSize();
6143 
6144  // Bail out if the necessary extension is not present.
6146  qWarning("QGLContext::bindTexture(): DDS texture compression is not supported.");
6147  return QSize();
6148  }
6149 
6150  const DDSFormat *ddsHeader = reinterpret_cast<const DDSFormat *>(buf + 4);
6151  if (!ddsHeader->dwLinearSize) {
6152  qWarning("QGLContext::bindTexture(): DDS image size is not valid.");
6153  return QSize();
6154  }
6155 
6156  int blockSize = 16;
6157  GLenum format;
6158 
6159  switch(ddsHeader->ddsPixelFormat.dwFourCC) {
6160  case FOURCC_DXT1:
6162  blockSize = 8;
6163  break;
6164  case FOURCC_DXT3:
6166  break;
6167  case FOURCC_DXT5:
6169  break;
6170  default:
6171  qWarning("QGLContext::bindTexture(): DDS image format not supported.");
6172  return QSize();
6173  }
6174 
6175  const GLubyte *pixels =
6176  reinterpret_cast<const GLubyte *>(buf + ddsHeader->dwSize + 4);
6177 
6178  glGenTextures(1, &id);
6179  glBindTexture(GL_TEXTURE_2D, id);
6180  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
6181  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
6182 
6183  int size;
6184  int offset = 0;
6185  int available = len - int(ddsHeader->dwSize + 4);
6186  int w = ddsHeader->dwWidth;
6187  int h = ddsHeader->dwHeight;
6188 
6189  // load mip-maps
6190  for(int i = 0; i < (int) ddsHeader->dwMipMapCount; ++i) {
6191  if (w == 0) w = 1;
6192  if (h == 0) h = 1;
6193 
6194  size = ((w+3)/4) * ((h+3)/4) * blockSize;
6195  if (size > available)
6196  break;
6197  glCompressedTexImage2D(GL_TEXTURE_2D, i, format, w, h, 0,
6198  size, pixels + offset);
6199  offset += size;
6200  available -= size;
6201 
6202  // half size for each mip-map level
6203  w = w/2;
6204  h = h/2;
6205  }
6206 
6207  // DDS images are not inverted.
6208  options &= ~QGLContext::InvertedYBindOption;
6209 
6210  return QSize(ddsHeader->dwWidth, ddsHeader->dwHeight);
6211 }
struct DDSFormat::@333 ddsPixelFormat
const int blockSize
#define GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
Definition: qgl.cpp:2028
quint32 dwHeight
Definition: qgl.cpp:2004
#define GL_TEXTURE_MIN_FILTER
GLenum target
Definition: qgl_p.h:609
quint32 dwMipMapCount
Definition: qgl.cpp:2008
#define glCompressedTexImage2D
#define GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
Definition: qgl.cpp:2026
QGLContext::BindOptions options
Definition: qgl_p.h:611
#define GL_TEXTURE_2D
#define FOURCC_DXT3
Definition: qgl.cpp:2020
#define FOURCC_DXT5
Definition: qgl.cpp:2022
The QGLContext class encapsulates an OpenGL rendering context.
Definition: qgl.h:310
quint32 dwFourCC
Definition: qgl.cpp:2012
quint32 dwLinearSize
Definition: qgl.cpp:2006
Q_CORE_EXPORT void qWarning(const char *,...)
quint32 dwWidth
Definition: qgl.cpp:2005
#define GL_LINEAR
#define GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
Definition: qgl.cpp:2027
quint32 dwSize
Definition: qgl.cpp:2002
unsigned int GLenum
Definition: main.cpp:50
#define FOURCC_DXT1
Definition: qgl.cpp:2018
static Extensions glExtensions()
Definition: qgl.cpp:5781
The QSize class defines the size of a two-dimensional object using integer point precision.
Definition: qsize.h:53
#define GL_TEXTURE_MAG_FILTER

◆ bindCompressedTexturePVR()

QSize QGLTexture::bindCompressedTexturePVR ( const char *  buf,
int  len 
)

Definition at line 6213 of file qgl.cpp.

6214 {
6215  // We only support 2D texture loading at present. Cube maps later.
6216  if (target != GL_TEXTURE_2D)
6217  return QSize();
6218 
6219  // Determine which texture format we will be loading.
6220  const PvrHeader *pvrHeader = reinterpret_cast<const PvrHeader *>(buf);
6221  GLenum textureFormat;
6222  quint32 minWidth, minHeight;
6223  switch (pvrHeader->flags & PVR_FORMAT_MASK) {
6224  case PVR_FORMAT_PVRTC2:
6225  if (pvrHeader->alphaMask)
6226  textureFormat = GL_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG;
6227  else
6228  textureFormat = GL_COMPRESSED_RGB_PVRTC_2BPPV1_IMG;
6229  minWidth = 16;
6230  minHeight = 8;
6231  break;
6232 
6233  case PVR_FORMAT_PVRTC4:
6234  if (pvrHeader->alphaMask)
6235  textureFormat = GL_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG;
6236  else
6237  textureFormat = GL_COMPRESSED_RGB_PVRTC_4BPPV1_IMG;
6238  minWidth = 8;
6239  minHeight = 8;
6240  break;
6241 
6242  case PVR_FORMAT_ETC1:
6243  textureFormat = GL_ETC1_RGB8_OES;
6244  minWidth = 4;
6245  minHeight = 4;
6246  break;
6247 
6248  default:
6249  qWarning("QGLContext::bindTexture(): PVR image format 0x%x not supported.", int(pvrHeader->flags & PVR_FORMAT_MASK));
6250  return QSize();
6251  }
6252 
6253  // Bail out if the necessary extension is not present.
6254  if (textureFormat == GL_ETC1_RGB8_OES) {
6255  if (!(QGLExtensions::glExtensions() &
6257  qWarning("QGLContext::bindTexture(): ETC1 texture compression is not supported.");
6258  return QSize();
6259  }
6260  } else {
6261  if (!(QGLExtensions::glExtensions() &
6263  qWarning("QGLContext::bindTexture(): PVRTC texture compression is not supported.");
6264  return QSize();
6265  }
6266  }
6267 
6268  // Boundary check on the buffer size.
6269  quint32 bufferSize = pvrHeader->headerSize + pvrHeader->dataSize;
6270  if (bufferSize > quint32(len)) {
6271  qWarning("QGLContext::bindTexture(): PVR image size is not valid.");
6272  return QSize();
6273  }
6274 
6275  // Create the texture.
6276  glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
6277  glGenTextures(1, &id);
6278  glBindTexture(GL_TEXTURE_2D, id);
6279  if (pvrHeader->mipMapCount) {
6281  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
6283  } else {
6284  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
6286  }
6287  } else if ((options & QGLContext::LinearFilteringBindOption) != 0) {
6288  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
6289  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
6290  } else {
6291  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
6292  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
6293  }
6294 
6295  // Load the compressed mipmap levels.
6296  const GLubyte *buffer =
6297  reinterpret_cast<const GLubyte *>(buf + pvrHeader->headerSize);
6298  bufferSize = pvrHeader->dataSize;
6299  quint32 level = 0;
6300  quint32 width = pvrHeader->width;
6301  quint32 height = pvrHeader->height;
6302  while (bufferSize > 0 && level <= pvrHeader->mipMapCount) {
6303  quint32 size =
6304  (qMax(width, minWidth) * qMax(height, minHeight) *
6305  pvrHeader->bitsPerPixel) / 8;
6306  if (size > bufferSize)
6307  break;
6308  glCompressedTexImage2D(GL_TEXTURE_2D, GLint(level), textureFormat,
6309  GLsizei(width), GLsizei(height), 0,
6310  GLsizei(size), buffer);
6311  width /= 2;
6312  height /= 2;
6313  buffer += size;
6314  ++level;
6315  }
6316 
6317  // Restore the default pixel alignment for later texture uploads.
6318  glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
6319 
6320  // Set the invert flag for the texture. The "vertical flip"
6321  // flag in PVR is the opposite sense to our sense of inversion.
6322  if ((pvrHeader->flags & PVR_VERTICAL_FLIP) != 0)
6323  options &= ~QGLContext::InvertedYBindOption;
6324  else
6326 
6327  return QSize(pvrHeader->width, pvrHeader->height);
6328 }
#define PVR_FORMAT_PVRTC2
Definition: qgl.cpp:6031
quint32 width
Definition: qgl.cpp:6015
#define GL_TEXTURE_MIN_FILTER
GLenum target
Definition: qgl_p.h:609
quint32 height
Definition: qgl.cpp:6014
#define glCompressedTexImage2D
#define PVR_FORMAT_ETC1
Definition: qgl.cpp:6033
QGLContext::BindOptions options
Definition: qgl_p.h:611
#define GL_TEXTURE_2D
Q_DECL_CONSTEXPR const T & qMax(const T &a, const T &b)
Definition: qglobal.h:1217
#define PVR_FORMAT_PVRTC4
Definition: qgl.cpp:6032
The QGLContext class encapsulates an OpenGL rendering context.
Definition: qgl.h:310
Q_CORE_EXPORT void qWarning(const char *,...)
quint32 bitsPerPixel
Definition: qgl.cpp:6019
quint32 mipMapCount
Definition: qgl.cpp:6016
#define GL_NEAREST_MIPMAP_NEAREST
#define GL_ETC1_RGB8_OES
Definition: qgl.cpp:6053
#define GL_UNPACK_ALIGNMENT
#define GL_LINEAR
#define GL_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG
Definition: qgl.cpp:6048
unsigned int GLenum
Definition: main.cpp:50
quint32 alphaMask
Definition: qgl.cpp:6023
unsigned int quint32
Definition: qglobal.h:938
#define GL_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG
Definition: qgl.cpp:6049
static Extensions glExtensions()
Definition: qgl.cpp:5781
int GLsizei
Definition: main.cpp:49
typedef GLint
Definition: glfunctions.h:67
#define GL_COMPRESSED_RGB_PVRTC_2BPPV1_IMG
Definition: qgl.cpp:6047
The QSize class defines the size of a two-dimensional object using integer point precision.
Definition: qsize.h:53
#define GL_LINEAR_MIPMAP_LINEAR
#define GL_TEXTURE_MAG_FILTER
quint32 headerSize
Definition: qgl.cpp:6013
quint32 dataSize
Definition: qgl.cpp:6018
#define PVR_VERTICAL_FLIP
Definition: qgl.cpp:6043
#define GL_COMPRESSED_RGB_PVRTC_4BPPV1_IMG
Definition: qgl.cpp:6046
#define GL_NEAREST
quint32 flags
Definition: qgl.cpp:6017
#define PVR_FORMAT_MASK
Definition: qgl.cpp:6030

◆ canBindCompressedTexture()

bool QGLTexture::canBindCompressedTexture ( const char *  buf,
int  len,
const char *  format,
bool *  hasAlpha 
)

Definition at line 6057 of file qgl.cpp.

Referenced by QGLPixmapData::fromData(), and QGLPixmapData::fromFile().

6058 {
6059  if (QSysInfo::ByteOrder != QSysInfo::LittleEndian) {
6060  // Compressed texture loading only supported on little-endian
6061  // systems such as x86 and ARM at the moment.
6062  return false;
6063  }
6064  if (!format) {
6065  // Auto-detect the format from the header.
6066  if (len >= 4 && !qstrncmp(buf, "DDS ", 4)) {
6067  *hasAlpha = true;
6068  return true;
6069  } else if (len >= 52 && !qstrncmp(buf + 44, "PVR!", 4)) {
6070  const PvrHeader *pvrHeader =
6071  reinterpret_cast<const PvrHeader *>(buf);
6072  *hasAlpha = (pvrHeader->alphaMask != 0);
6073  return true;
6074  }
6075  } else {
6076  // Validate the format against the header.
6077  if (!qstricmp(format, "DDS")) {
6078  if (len >= 4 && !qstrncmp(buf, "DDS ", 4)) {
6079  *hasAlpha = true;
6080  return true;
6081  }
6082  } else if (!qstricmp(format, "PVR") || !qstricmp(format, "ETC1")) {
6083  if (len >= 52 && !qstrncmp(buf + 44, "PVR!", 4)) {
6084  const PvrHeader *pvrHeader =
6085  reinterpret_cast<const PvrHeader *>(buf);
6086  *hasAlpha = (pvrHeader->alphaMask != 0);
6087  return true;
6088  }
6089  }
6090  }
6091  return false;
6092 }
quint32 alphaMask
Definition: qgl.cpp:6023
int qstrncmp(const char *str1, const char *str2, uint len)
Definition: qbytearray.h:101
Q_CORE_EXPORT int qstricmp(const char *, const char *)

Properties

◆ boundPixmap

QPixmapData* QGLTexture::boundPixmap

Definition at line 614 of file qgl_p.h.

Referenced by QGLTexturePool::hibernate(), and QGLTexturePool::reclaimSpace().

◆ context

QGLContext* QGLTexture::context

Definition at line 607 of file qgl_p.h.

◆ id

GLuint QGLTexture::id

◆ options

QGLContext::BindOptions QGLTexture::options

◆ target

GLenum QGLTexture::target

Definition at line 609 of file qgl_p.h.


The documentation for this class was generated from the following files: