QGLContext Class Reference

#include <qgl.h>

Collaboration diagram for QGLContext:

Collaboration graph
[legend]
List of all members.

Detailed Description

The QGLContext class encapsulates an OpenGL rendering context.

An OpenGL OpenGL is a trademark of Silicon Graphics, Inc. in the United States and other countries. rendering context is a complete set of OpenGL state variables.

The context's format is set in the constructor or later with setFormat(). The format options that are actually set are returned by format(); the options you asked for are returned by requestedFormat(). Note that after a QGLContext object has been constructed, the actual OpenGL context must be created by explicitly calling the create() function. The makeCurrent() function makes this context the current rendering context. You can make no context current using doneCurrent(). The reset() function will reset the context and make it invalid.

You can examine properties of the context with, e.g. isValid(), isSharing(), initialized(), windowCreated() and overlayTransparentColor().

If you're using double buffering you can swap the screen contents with the off-screen buffer using swapBuffers().

Please note that QGLContext is not thread safe.

Definition at line 211 of file qgl.h.

Public Member Functions

 QGLContext (const QGLFormat &format, QPaintDevice *device)
 QGLContext (const QGLFormat &format)
virtual ~QGLContext ()
virtual bool create (const QGLContext *shareContext=0)
bool isValid () const
bool isSharing () const
void reset ()
QGLFormat format () const
QGLFormat requestedFormat () const
void setFormat (const QGLFormat &format)
virtual void makeCurrent ()
virtual void doneCurrent ()
virtual void swapBuffers () const
GLuint bindTexture (const QImage &image, GLenum target=GL_TEXTURE_2D, GLint format=GL_RGBA)
GLuint bindTexture (const QPixmap &pixmap, GLenum target=GL_TEXTURE_2D, GLint format=GL_RGBA)
GLuint bindTexture (const QString &fileName)
void deleteTexture (GLuint tx_id)
void * getProcAddress (const QString &proc) const
QPaintDevicedevice () const
QColor overlayTransparentColor () const

Static Public Member Functions

static void setTextureCacheLimit (int size)
static int textureCacheLimit ()
static const QGLContextcurrentContext ()

Protected Member Functions

virtual bool chooseContext (const QGLContext *shareContext=0)
bool deviceIsPixmap () const
bool windowCreated () const
void setWindowCreated (bool on)
bool initialized () const
void setInitialized (bool on)
void generateFontDisplayLists (const QFont &fnt, int listBase)
uint colorIndex (const QColor &c) const
void setValid (bool valid)
void setDevice (QPaintDevice *pDev)

Static Protected Attributes

static QGLContextcurrentCtx

Private Attributes

QGLContextPrivated_ptr

Friends

class QGLPixelBuffer
class QGLPixelBufferPrivate
class QGLWidget
class QGLDrawable
class QGLWidgetPrivate
class QGLGlyphCache
class QOpenGLPaintEngine
class QOpenGLPaintEnginePrivate


Constructor & Destructor Documentation

QGLContext::QGLContext ( const QGLFormat format,
QPaintDevice device 
)

Constructs an OpenGL context for the given paint device, which can be a widget or a pixmap. The format specifies several display options for the context.

If the underlying OpenGL/Window system cannot satisfy all the features requested in format, the nearest subset of features will be used. After creation, the format() method will return the actual format obtained.

Note that after a QGLContext object has been constructed, create() must be called explicitly to create the actual OpenGL context. The context will be {isValid()}{invalid} if it was not possible to obtain a GL context at all.

Definition at line 1408 of file qgl.cpp.

References d, d_ptr, device(), and format().

01409 {
01410     d_ptr = new QGLContextPrivate(this);
01411     Q_D(QGLContext);
01412     d->init(device, format);
01413 }

Here is the call graph for this function:

QGLContext::QGLContext ( const QGLFormat format  ) 

Constructs an OpenGL context with the given format which specifies several display options for the context.

If the underlying OpenGL/Window system cannot satisfy all the features requested in format, the nearest subset of features will be used. After creation, the format() method will return the actual format obtained.

Note that after a QGLContext object has been constructed, create() must be called explicitly to create the actual OpenGL context. The context will be {isValid()}{invalid} if it was not possible to obtain a GL context at all.

See also:
format(), isValid()

Definition at line 1431 of file qgl.cpp.

References d, d_ptr, and format().

01432 {
01433     d_ptr = new QGLContextPrivate(this);
01434     Q_D(QGLContext);
01435     d->init(0, format);
01436 }

Here is the call graph for this function:

QGLContext::~QGLContext (  )  [virtual]

Destroys the OpenGL context and frees its resources.

Definition at line 1442 of file qgl.cpp.

References QList< T >::at(), d, i, key, QCache< Key, T >::keys(), QCache< Key, T >::object(), qt_image_cleanup_hook, qt_pixmap_cleanup_hook, qt_tex_cache, QCache< Key, T >::remove(), reset(), QCache< Key, T >::size(), and QList< T >::size().

01443 {
01444     Q_D(QGLContext);
01445     // remove any textures cached in this context
01446     if (qt_tex_cache) {
01447         QList<QString> keys = qt_tex_cache->keys();
01448         for (int i = 0; i < keys.size(); ++i) {
01449             const QString &key = keys.at(i);
01450             if (qt_tex_cache->object(key)->context == this)
01451                 qt_tex_cache->remove(key);
01452         }
01453         // ### thread safety
01454         if (qt_tex_cache->size() == 0) {
01455             qt_pixmap_cleanup_hook = 0;
01456             qt_image_cleanup_hook = 0;
01457             delete qt_tex_cache;
01458             qt_tex_cache = 0;
01459         }
01460     }
01461 
01462     reset();
01463     delete d;
01464 }

Here is the call graph for this function:


Member Function Documentation

bool QGLContext::create ( const QGLContext shareContext = 0  )  [virtual]

Creates the GL context. Returns true if it was successful in creating a valid GL rendering context on the paint device specified in the constructor; otherwise returns false (i.e. the context is invalid).

After successful creation, format() returns the set of features of the created GL rendering context.

If shareContext points to a valid QGLContext, this method will try to establish OpenGL display list sharing between this context and the shareContext. Note that this may fail if the two contexts have different formats. Use isSharing() to see if sharing succeeded.

Warning:
Implementation note: initialization of C++ class members usually takes place in the class constructor. QGLContext is an exception because it must be simple to customize. The virtual functions chooseContext() (and chooseVisual() for X11) can be reimplemented in a subclass to select a particular context. The problem is that virtual functions are not properly called during construction (even though this is correct C++) because C++ constructs class hierarchies from the bottom up. For this reason we need a create() function.
See also:
chooseContext(), format(), isValid()

Definition at line 2036 of file qgl.cpp.

References QGLShareRegister::addShare(), chooseContext(), d, qgl_share_reg(), and reset().

02037 {
02038     Q_D(QGLContext);
02039     reset();
02040     d->valid = chooseContext(shareContext);
02041     if (d->sharing)  // ok, we managed to share
02042         qgl_share_reg()->addShare(this, shareContext);
02043     return d->valid;
02044 }

Here is the call graph for this function:

bool QGLContext::isValid (  )  const

Returns true if a GL rendering context has been successfully created; otherwise returns false.

Definition at line 2046 of file qgl.cpp.

References d.

Referenced by chooseContext(), colorIndex(), overlayTransparentColor(), and setDevice().

02047 {
02048     Q_D(const QGLContext);
02049     return d->valid;
02050 }

bool QGLContext::isSharing (  )  const

Returns true if this context is sharing its GL context with another QGLContext, otherwise false is returned. Note that context sharing might not be supported between contexts with different formats.

Definition at line 2058 of file qgl.cpp.

References d.

Referenced by QGLTexture::~QGLTexture().

02059 {
02060     Q_D(const QGLContext);
02061     return d->sharing;
02062 }

void QGLContext::reset (  ) 

Resets the context and makes it invalid.

See also:
create(), isValid()

Definition at line 587 of file qgl_x11.cpp.

References d, QX11Info::display(), doneCurrent(), qgl_share_reg(), qt_x11Info(), and QGLShareRegister::removeShare().

Referenced by create(), setDevice(), setFormat(), and ~QGLContext().

00588 {
00589     Q_D(QGLContext);
00590     if (!d->valid)
00591         return;
00592     const QX11Info *xinfo = qt_x11Info(d->paintDevice);
00593     doneCurrent();
00594     if (d->gpm)
00595         glXDestroyGLXPixmap(xinfo->display(), (GLXPixmap)d->gpm);
00596     d->gpm = 0;
00597     glXDestroyContext(xinfo->display(), (GLXContext)d->cx);
00598     if (d->vi)
00599         XFree(d->vi);
00600     d->vi = 0;
00601     d->cx = 0;
00602     d->crWin = false;
00603     d->sharing = false;
00604     d->valid = false;
00605     d->transpColor = QColor();
00606     d->initDone = false;
00607     qgl_share_reg()->removeShare(this);
00608 }

Here is the call graph for this function:

QGLFormat QGLContext::format (  )  const

Returns the frame buffer format that was obtained (this may be a subset of what was requested).

See also:
requestedFormat()

Definition at line 2064 of file qgl.cpp.

References d.

Referenced by chooseContext(), colorIndex(), QGLDrawable::format(), QGLWidgetPrivate::init(), QGLWidget::qglClearColor(), QGLWidget::qglColor(), QGLContext(), and setFormat().

02065 {
02066     Q_D(const QGLContext);
02067     return d->glFormat;
02068 }

QGLFormat QGLContext::requestedFormat (  )  const

Returns the frame buffer format that was originally requested in the constructor or setFormat().

See also:
format()

Definition at line 2070 of file qgl.cpp.

References d.

02071 {
02072     Q_D(const QGLContext);
02073     return d->reqFormat;
02074 }

void QGLContext::setFormat ( const QGLFormat format  ) 

Sets a format for this context. The context is reset.

Call create() to create a new GL context that tries to match the new format.

    QGLContext *cx;
    //  ...
    QGLFormat f;
    f.setStereo(true);
    cx->setFormat(f);
    if (!cx->create())
        exit(); // no OpenGL support, or cannot render on the specified paintdevice
    if (!cx->format().stereo())
        exit(); // could not create stereo context

See also:
format(), reset(), create()

Definition at line 1878 of file qgl.cpp.

References d, format(), and reset().

01879 {
01880     Q_D(QGLContext);
01881     reset();
01882     d->glFormat = d->reqFormat = format;
01883 }

Here is the call graph for this function:

void QGLContext::makeCurrent (  )  [virtual]

Makes this context the current OpenGL rendering context. All GL functions you call operate on this context until another context is made current.

In some very rare cases the underlying call may fail. If this occurs an error message is output to stderr.

Definition at line 611 of file qgl_x11.cpp.

References currentCtx, QThread::currentThread(), d, deviceIsPixmap(), QX11Info::display(), qgl_context_storage, qt_x11Info(), and qWarning().

Referenced by QGLWidget::renderPixmap(), and QGLPixelBuffer::~QGLPixelBuffer().

00612 {
00613     Q_D(QGLContext);
00614     if (!d->valid) {
00615         qWarning("QGLContext::makeCurrent(): Cannot make invalid context current.");
00616         return;
00617     }
00618     const QX11Info *xinfo = qt_x11Info(d->paintDevice);
00619     bool ok = true;
00620     if (deviceIsPixmap())
00621         ok = glXMakeCurrent(xinfo->display(), (GLXPixmap)d->gpm, (GLXContext)d->cx);
00622 
00623     else
00624         ok = glXMakeCurrent(xinfo->display(), ((QWidget *)d->paintDevice)->winId(),
00625                              (GLXContext)d->cx);
00626     if (!ok)
00627         qWarning("QGLContext::makeCurrent(): Failed.");
00628 
00629 
00630     if (ok) {
00631         if (!qgl_context_storage.hasLocalData() && QThread::currentThread())
00632             qgl_context_storage.setLocalData(new QGLThreadContext);
00633         if (qgl_context_storage.hasLocalData())
00634             qgl_context_storage.localData()->context = this;
00635         currentCtx = this;
00636     }
00637 }

Here is the call graph for this function:

void QGLContext::doneCurrent (  )  [virtual]

Makes no GL context the current context. Normally, you do not need to call this function; QGLContext calls it as necessary.

Definition at line 639 of file qgl_x11.cpp.

References currentCtx, d, QX11Info::display(), qgl_context_storage, and qt_x11Info().

Referenced by QGLWidget::renderPixmap(), and reset().

00640 {
00641     Q_D(QGLContext);
00642     glXMakeCurrent(qt_x11Info(d->paintDevice)->display(), 0, 0);
00643     if (qgl_context_storage.hasLocalData())
00644         qgl_context_storage.localData()->context = 0;
00645     currentCtx = 0;
00646 }

Here is the call graph for this function:

void QGLContext::swapBuffers (  )  const [virtual]

Swaps the screen contents with an off-screen buffer. Only works if the context is in double buffer mode.

See also:
QGLFormat::setDoubleBuffer()

Definition at line 649 of file qgl_x11.cpp.

References d, deviceIsPixmap(), QX11Info::display(), and qt_x11Info().

00650 {
00651     Q_D(const QGLContext);
00652     if (!d->valid)
00653         return;
00654     if (!deviceIsPixmap())
00655         glXSwapBuffers(qt_x11Info(d->paintDevice)->display(),
00656                        static_cast<QWidget *>(d->paintDevice)->winId());
00657 }

Here is the call graph for this function:

GLuint QGLContext::bindTexture ( const QImage image,
GLenum  target = GL_TEXTURE_2D,
GLint  format = GL_RGBA 
)

Generates and binds a 2D GL texture to the current context, based on image. The generated texture id is returned and can be used in later glBindTexture() calls.

The target parameter specifies the texture target. The default target is GL_TEXTURE_2D.

The format parameter sets the internal format for the texture. The default format is GL_RGBA8.

If the GL implementation supports the GL_SGIS_generate_mipmap extension, mipmaps will be automatically generated for the texture. Mipmap generation is only supported for the GL_TEXTURE_2D target.

The texture that is generated is cached, so multiple calls to bindTexture() with the same QImage will return the same texture id.

See also:
deleteTexture()

Definition at line 1774 of file qgl.cpp.

References d, and image.

01775 {
01776     Q_D(QGLContext);
01777     return d->bindTexture(image, target, format, false);
01778 }

GLuint QGLContext::bindTexture ( const QPixmap pixmap,
GLenum  target = GL_TEXTURE_2D,
GLint  format = GL_RGBA 
)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. Generates and binds a 2D GL texture based on pixmap.

Definition at line 1784 of file qgl.cpp.

References d.

01785 {
01786     Q_D(QGLContext);
01787     return d->bindTexture(pixmap, target, format, false);
01788 }

GLuint QGLContext::bindTexture ( const QString fileName  ) 

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. Reads the DirectDrawSurface (DDS) compressed file fileName and generates a 2D GL texture from it.

Only the DXT1, DXT3 and DXT5 DDS formats are supported.

Note that this will only work if the implementation supports the GL_ARB_texture_compression and GL_EXT_texture_compression_s3tc extensions.

See also:
deleteTexture()

Definition at line 1479 of file qgl.cpp.

References QFile::close(), FOURCC_DXT1, FOURCC_DXT3, FOURCC_DXT5, GL_COMPRESSED_RGBA_S3TC_DXT1_EXT, GL_COMPRESSED_RGBA_S3TC_DXT3_EXT, GL_COMPRESSED_RGBA_S3TC_DXT5_EXT, GLenum, h, i, QCache< Key, T >::insert(), int, key, QCache< Key, T >::object(), QFile::open(), qt_tex_cache, qt_tex_cache_limit, qWarning(), QIODevice::read(), QIODevice::ReadOnly, QFile::seek(), size, and w.

01480 {
01481     if (!qt_glCompressedTexImage2DARB) {
01482         qWarning("QGLContext::bindTexture(): The GL implementation does not support texture"
01483                  "compression extensions.");
01484         return 0;
01485     }
01486 
01487     if (!qt_tex_cache)
01488         qt_tex_cache = new QGLTextureCache(qt_tex_cache_limit);
01489 
01490     QString key(fileName);
01491     QGLTexture *texture = qt_tex_cache->object(key);
01492 
01493     if (texture && texture->context == this) {
01494         glBindTexture(GL_TEXTURE_2D, texture->id);
01495         return texture->id;
01496     }
01497 
01498     QFile f(fileName);
01499     f.open(QIODevice::ReadOnly);
01500 
01501     char tag[4];
01502     f.read(&tag[0], 4);
01503     if (strncmp(tag,"DDS ", 4) != 0) {
01504         qWarning("QGLContext::bindTexture(): not a DDS image file.");
01505         return 0;
01506     }
01507 
01508     DDSFormat ddsHeader;
01509     f.read((char *) &ddsHeader, sizeof(DDSFormat));
01510 
01511     if (!ddsHeader.dwLinearSize) {
01512         qWarning("QGLContext::bindTexture() DDS image size is not valid.");
01513         return 0;
01514     }
01515 
01516     int factor = 4;
01517     int bufferSize = 0;
01518     int blockSize = 16;
01519     GLenum format;
01520 
01521     switch(ddsHeader.ddsPixelFormat.dwFourCC) {
01522     case FOURCC_DXT1:
01523         format = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
01524         factor = 2;
01525         blockSize = 8;
01526         break;
01527     case FOURCC_DXT3:
01528         format = GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
01529         break;
01530     case FOURCC_DXT5:
01531         format = GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
01532         break;
01533     default:
01534         qWarning("QGLContext::bindTexture() DDS image format not supported.");
01535         return 0;
01536     }
01537 
01538     if (ddsHeader.dwMipMapCount > 1)
01539         bufferSize = ddsHeader.dwLinearSize * factor;
01540     else
01541         bufferSize = ddsHeader.dwLinearSize;
01542 
01543     GLubyte *pixels = (GLubyte *) malloc(bufferSize*sizeof(GLubyte));
01544     f.seek(ddsHeader.dwSize + 4);
01545     f.read((char *) pixels, bufferSize);
01546     f.close();
01547 
01548     GLuint tx_id;
01549     glGenTextures(1, &tx_id);
01550     glBindTexture(GL_TEXTURE_2D, tx_id);
01551     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
01552 
01553     int size;
01554     int offset = 0;
01555     int w = ddsHeader.dwWidth;
01556     int h = ddsHeader.dwHeight;
01557 
01558     // load mip-maps
01559     for(int i = 0; i < (int) ddsHeader.dwMipMapCount; ++i) {
01560         if (w == 0) w = 1;
01561         if (h == 0) h = 1;
01562 
01563         size = ((w+3)/4) * ((h+3)/4) * blockSize;
01564         qt_glCompressedTexImage2DARB(GL_TEXTURE_2D, i, format, w, h, 0,
01565                                      size, pixels + offset);
01566         offset += size;
01567 
01568         // half size for each mip-map level
01569         w = w/2;
01570         h = h/2;
01571     }
01572 
01573     free(pixels);
01574 
01575     int cost = bufferSize/1024;
01576     qt_tex_cache->insert(key, new QGLTexture(this, tx_id, 0), cost);
01577     return tx_id;
01578 }

Here is the call graph for this function:

void QGLContext::deleteTexture ( GLuint  id  ) 

Removes the texture identified by id from the texture cache. If the context is not shared by any other QGLContext, glDeleteTextures() will be called to delete the texture from the context.

See also:
bindTexture()

Definition at line 1798 of file qgl.cpp.

References QList< T >::at(), i, QCache< Key, T >::keys(), QCache< Key, T >::object(), qt_tex_cache, QCache< Key, T >::remove(), and QList< T >::size().

01799 {
01800     if (!qt_tex_cache)
01801         return;
01802 
01803     QList<QString> keys = qt_tex_cache->keys();
01804     for (int i = 0; i < keys.size(); ++i) {
01805         QGLTexture *tex = qt_tex_cache->object(keys.at(i));
01806         if (tex->id == id && tex->context == this) {
01807             qt_tex_cache->remove(keys.at(i));
01808             break;
01809         }
01810     }
01811 }

Here is the call graph for this function:

void QGLContext::setTextureCacheLimit ( int  size  )  [static]

This function sets the limit for the texture cache to size, expressed in kilobytes.

By default, the cache limit is approximately 64 MB.

See also:
textureCacheLimit()

Definition at line 1821 of file qgl.cpp.

References qt_tex_cache, qt_tex_cache_limit, and QCache< Key, T >::setMaxCost().

01822 {
01823     qt_tex_cache_limit = size;
01824     if (qt_tex_cache)
01825         qt_tex_cache->setMaxCost(qt_tex_cache_limit);
01826 }

Here is the call graph for this function:

int QGLContext::textureCacheLimit (  )  [static]

Returns the current texture cache limit in kilobytes.

See also:
setTextureCacheLimit()

Definition at line 1833 of file qgl.cpp.

References qt_tex_cache_limit.

01834 {
01835     return qt_tex_cache_limit;
01836 }

void * QGLContext::getProcAddress ( const QString proc  )  const

Returns a function pointer to the GL extension function passed in proc. 0 is returned if a pointer to the function could not be obtained.

Definition at line 884 of file qgl_x11.cpp.

References QString::contains(), QByteArray::data(), QX11Info::display(), and QString::toLatin1().

Referenced by QGLExtensions::init_extensions().

00885 {
00886     typedef void *(*qt_glXGetProcAddressARB)(const GLubyte *);
00887     static qt_glXGetProcAddressARB glXGetProcAddressARB = 0;
00888     static bool resolved = false;
00889 
00890     if (resolved && !glXGetProcAddressARB)
00891         return 0;
00892     if (!glXGetProcAddressARB) {
00893         QString glxExt = QString(QLatin1String(glXGetClientString(QX11Info::display(), GLX_EXTENSIONS)));
00894         if (glxExt.contains(QLatin1String("GLX_ARB_get_proc_address"))) {
00895             QLibrary lib(QLatin1String("GL"));
00896             glXGetProcAddressARB = (qt_glXGetProcAddressARB) lib.resolve("glXGetProcAddressARB");
00897         }
00898         resolved = true;
00899     }
00900     if (!glXGetProcAddressARB)
00901         return 0;
00902     return glXGetProcAddressARB(reinterpret_cast<const GLubyte *>(proc.toLatin1().data()));
00903 }

Here is the call graph for this function:

QPaintDevice * QGLContext::device (  )  const

Returns the paint device set for this context.

See also:
QGLContext::QGLContext()

Definition at line 2076 of file qgl.cpp.

References d.

Referenced by QGLWidgetPrivate::initContext(), QGLContext(), and QGLWidget::setContext().

02077 {
02078     Q_D(const QGLContext);
02079     return d->paintDevice;
02080 }

QColor QGLContext::overlayTransparentColor (  )  const

If this context is a valid context in an overlay plane, returns the plane's transparent color. Otherwise returns an invalid color.

The returned color's pixel value is the index of the transparent color in the colormap of the overlay plane. (Naturally, the color's RGB values are meaningless.)

The returned QColor object will generally work as expected only when passed as the argument to QGLWidget::qglColor() or QGLWidget::qglClearColor(). Under certain circumstances it can also be used to draw transparent graphics with a QPainter. See the examples/opengl/overlay_x11 example for details.

Definition at line 659 of file qgl_x11.cpp.

References b, choose_cmap(), d, QX11Info::display(), find_trans_colors(), g, int, isValid(), qRgb(), qt_x11Info(), and trans_colors.

Referenced by colorIndex(), and QGLOverlayWidget::initializeGL().

00660 {
00661     Q_D(const QGLContext);
00662     if (isValid()) {
00663         if (!trans_colors_init)
00664             find_trans_colors();
00665 
00666         VisualID myVisualId = ((XVisualInfo*)d->vi)->visualid;
00667         int myScreen = ((XVisualInfo*)d->vi)->screen;
00668         for (int i = 0; i < (int)trans_colors.size(); i++) {
00669             if (trans_colors[i].vis == myVisualId &&
00670                  trans_colors[i].screen == myScreen) {
00671                 XColor col;
00672                 col.pixel = trans_colors[i].color;
00673                 col.red = col.green = col.blue = 0;
00674                 col.flags = 0;
00675                 Display *dpy = qt_x11Info(d->paintDevice)->display();
00676                 if (col.pixel > (uint) ((XVisualInfo *)d->vi)->colormap_size - 1)
00677                     col.pixel = ((XVisualInfo *)d->vi)->colormap_size - 1;
00678                 XQueryColor(dpy, choose_cmap(dpy, (XVisualInfo *) d->vi), &col);
00679                 uchar r = (uchar)((col.red / 65535.0) * 255.0 + 0.5);
00680                 uchar g = (uchar)((col.green / 65535.0) * 255.0 + 0.5);
00681                 uchar b = (uchar)((col.blue / 65535.0) * 255.0 + 0.5);
00682                 return QColor(qRgb(r,g,b));
00683             }
00684         }
00685     }
00686     return QColor();                // Invalid color
00687 }

Here is the call graph for this function:

const QGLContext * QGLContext::currentContext (  )  [static]

Returns the current context, i.e. the context to which any OpenGL commands will currently be directed. Returns 0 if no context is current.

See also:
makeCurrent()

Definition at line 2114 of file qgl.cpp.

References qgl_context_storage.

Referenced by QGLDrawable::bindTexture(), QGLDrawable::context(), QGLDrawable::format(), QGLFramebufferObjectPrivate::init(), QGLWidget::qglClearColor(), QGLWidget::qglColor(), and QGLPixelBuffer::~QGLPixelBuffer().

02115 {
02116     if (qgl_context_storage.hasLocalData())
02117         return qgl_context_storage.localData()->context;
02118     return 0;
02119 }

bool QGLContext::chooseContext ( const QGLContext shareContext = 0  )  [protected, virtual]

This semi-internal function is called by create(). It creates a system-dependent OpenGL handle that matches the format() of shareContext as closely as possible, returning true if successful or false if a suitable handle could not be found.

On Windows, it calls the virtual function choosePixelFormat(), which finds a matching pixel format identifier. On X11, it calls the virtual function chooseVisual() which finds an appropriate X visual. On other platforms it may work differently.

Definition at line 290 of file qgl_x11.cpp.

References choose_cmap(), d, QX11Info::depth(), deviceIsPixmap(), QGLFormat::directRendering(), QX11Info::display(), format(), GLX_SAMPLE_BUFFERS_ARB, GLX_SAMPLES_ARB, isValid(), NULL, qt_x11Handle(), qt_x11Info(), qWarning(), QGLFormat::rgba(), QX11Info::screen(), and QX11Info::visual().

Referenced by create().

00291 {
00292     Q_D(QGLContext);
00293     const QX11Info *xinfo = qt_x11Info(d->paintDevice);
00294 
00295     Display* disp = xinfo->display();
00296     d->vi = chooseVisual();
00297     if (!d->vi)
00298         return false;
00299 
00300     if (deviceIsPixmap() &&
00301          (((XVisualInfo*)d->vi)->depth != xinfo->depth() ||
00302           ((XVisualInfo*)d->vi)->screen != xinfo->screen()))
00303     {
00304         XFree(d->vi);
00305         XVisualInfo appVisInfo;
00306         memset(&appVisInfo, 0, sizeof(XVisualInfo));
00307         appVisInfo.visualid = XVisualIDFromVisual((Visual *) xinfo->visual());
00308         appVisInfo.screen = xinfo->screen();
00309         int nvis;
00310         d->vi = XGetVisualInfo(disp, VisualIDMask | VisualScreenMask, &appVisInfo, &nvis);
00311         if (!d->vi)
00312             return false;
00313 
00314         int useGL;
00315         glXGetConfig(disp, (XVisualInfo*)d->vi, GLX_USE_GL, &useGL);
00316         if (!useGL)
00317             return false;        //# Chickening out already...
00318     }
00319     int res;
00320     glXGetConfig(disp, (XVisualInfo*)d->vi, GLX_LEVEL, &res);
00321     d->glFormat.setPlane(res);
00322     glXGetConfig(disp, (XVisualInfo*)d->vi, GLX_DOUBLEBUFFER, &res);
00323     d->glFormat.setDoubleBuffer(res);
00324     glXGetConfig(disp, (XVisualInfo*)d->vi, GLX_DEPTH_SIZE, &res);
00325     d->glFormat.setDepth(res);
00326     if (d->glFormat.depth())
00327         d->glFormat.setDepthBufferSize(res);
00328     glXGetConfig(disp, (XVisualInfo*)d->vi, GLX_RGBA, &res);
00329     d->glFormat.setRgba(res);
00330     glXGetConfig(disp, (XVisualInfo*)d->vi, GLX_RED_SIZE, &res);
00331     d->glFormat.setRedBufferSize(res);
00332     glXGetConfig(disp, (XVisualInfo*)d->vi, GLX_GREEN_SIZE, &res);
00333     d->glFormat.setGreenBufferSize(res);
00334     glXGetConfig(disp, (XVisualInfo*)d->vi, GLX_BLUE_SIZE, &res);
00335     d->glFormat.setBlueBufferSize(res);
00336     glXGetConfig(disp, (XVisualInfo*)d->vi, GLX_ALPHA_SIZE, &res);
00337     d->glFormat.setAlpha(res);
00338     if (d->glFormat.alpha())
00339         d->glFormat.setAlphaBufferSize(res);
00340     glXGetConfig(disp, (XVisualInfo*)d->vi, GLX_ACCUM_RED_SIZE, &res);
00341     d->glFormat.setAccum(res);
00342     if (d->glFormat.accum())
00343         d->glFormat.setAccumBufferSize(res);
00344     glXGetConfig(disp, (XVisualInfo*)d->vi, GLX_STENCIL_SIZE, &res);
00345     d->glFormat.setStencil(res);
00346     if (d->glFormat.stencil())
00347         d->glFormat.setStencilBufferSize(res);
00348     glXGetConfig(disp, (XVisualInfo*)d->vi, GLX_STEREO, &res);
00349     d->glFormat.setStereo(res);
00350     glXGetConfig(disp, (XVisualInfo*)d->vi, GLX_SAMPLE_BUFFERS_ARB, &res);
00351     d->glFormat.setSampleBuffers(res);
00352     if (d->glFormat.sampleBuffers()) {
00353         glXGetConfig(disp, (XVisualInfo*)d->vi, GLX_SAMPLES_ARB, &res);
00354         d->glFormat.setSamples(res);
00355     }
00356 
00357     Bool direct = format().directRendering() ? True : False;
00358 
00359     if (shareContext &&
00360          (!shareContext->isValid() || !shareContext->d_func()->cx)) {
00361             qWarning("QGLContext::chooseContext(): Cannot share with invalid context");
00362             shareContext = 0;
00363     }
00364 
00365     // 1. Sharing between rgba and color-index will give wrong colors.
00366     // 2. Contexts cannot be shared btw. direct/non-direct renderers.
00367     // 3. Pixmaps cannot share contexts that are set up for direct rendering.
00368     if (shareContext && (format().rgba() != shareContext->format().rgba() ||
00369                           (deviceIsPixmap() &&
00370                            glXIsDirect(disp, (GLXContext)shareContext->d_func()->cx))))
00371         shareContext = 0;
00372 
00373     d->cx = 0;
00374     if (shareContext) {
00375         d->cx = glXCreateContext(disp, (XVisualInfo *)d->vi,
00376                                (GLXContext)shareContext->d_func()->cx, direct);
00377         if (d->cx) {
00378             QGLContext *share = const_cast<QGLContext *>(shareContext);
00379             d->sharing = true;
00380             share->d_func()->sharing = true;
00381         }
00382     }
00383     if (!d->cx)
00384         d->cx = glXCreateContext(disp, (XVisualInfo *)d->vi, NULL, direct);
00385     if (!d->cx)
00386         return false;
00387     d->glFormat.setDirectRendering(glXIsDirect(disp, (GLXContext)d->cx));
00388     if (deviceIsPixmap()) {
00389 #if defined(GLX_MESA_pixmap_colormap) && defined(QGL_USE_MESA_EXT)
00390         d->gpm = glXCreateGLXPixmapMESA(disp, (XVisualInfo *)d->vi,
00391                                         qt_x11Handle(d->paintDevice),
00392                                         choose_cmap(disp, (XVisualInfo *)d->vi));
00393 #else
00394         d->gpm = (quint32)glXCreateGLXPixmap(disp, (XVisualInfo *)d->vi,
00395                                               qt_x11Handle(d->paintDevice));
00396 #endif
00397         if (!d->gpm)
00398             return false;
00399     }
00400     return true;
00401 }

Here is the call graph for this function:

bool QGLContext::deviceIsPixmap (  )  const [protected]

Returns true if the paint device of this context is a pixmap; otherwise returns false.

Definition at line 2082 of file qgl.cpp.

References d, and QInternal::Pixmap.

Referenced by chooseContext(), makeCurrent(), QGLWidget::setContext(), and swapBuffers().

02083 {
02084     Q_D(const QGLContext);
02085     return d->paintDevice->devType() == QInternal::Pixmap;
02086 }

bool QGLContext::windowCreated (  )  const [protected]

Returns true if a window has been created for this context; otherwise returns false.

See also:
setWindowCreated()

Definition at line 2089 of file qgl.cpp.

References d.

Referenced by QGLWidget::setContext().

02090 {
02091     Q_D(const QGLContext);
02092     return d->crWin;
02093 }

void QGLContext::setWindowCreated ( bool  on  )  [protected]

If on is true the context has had a window created for it. If on is false no window has been created for the context.

See also:
windowCreated()

Definition at line 2096 of file qgl.cpp.

References d.

02097 {
02098     Q_D(QGLContext);
02099     d->crWin = on;
02100 }

bool QGLContext::initialized (  )  const [protected]

Returns true if this context has been initialized, i.e. if QGLWidget::initializeGL() has been performed on it; otherwise returns false.

See also:
setInitialized()

Definition at line 2102 of file qgl.cpp.

References d.

Referenced by QGLWidgetPrivate::renderCxPm().

02103 {
02104     Q_D(const QGLContext);
02105     return d->initDone;
02106 }

void QGLContext::setInitialized ( bool  on  )  [protected]

If on is true the context has been initialized, i.e. QGLContext::setInitialized() has been called on it. If on is false the context has not been initialized.

See also:
initialized()

Definition at line 2108 of file qgl.cpp.

References d.

02109 {
02110     Q_D(QGLContext);
02111     d->initDone = on;
02112 }

void QGLContext::generateFontDisplayLists ( const QFont font,
int  listBase 
) [protected]

Generates a set of 256 display lists for the 256 first characters in the font font. The first list will start at index listBase.

See also:
QGLWidget::renderText()

Definition at line 859 of file qgl_x11.cpp.

References QUnicodeTables::Common, QFont::d, QFontPrivate::engineForScript(), QFontEngine::Freetype, QFont::handle(), QFontEngine::Multi, QFont::OpenGLCompatible, qgl_use_font(), QFont::setStyleStrategy(), QFontEngine::type(), and QFontEngine::XLFD.

00860 {
00861     QFont f(fnt);
00862     QFontEngine *engine = f.d->engineForScript(QUnicodeTables::Common);
00863 
00864     if (engine->type() == QFontEngine::Multi)
00865         engine = static_cast<QFontEngineMulti *>(engine)->engine(0);
00866 #ifndef QT_NO_FONTCONFIG
00867     if(engine->type() == QFontEngine::Freetype) {
00868         qgl_use_font(static_cast<QFontEngineFT *>(engine), 0, 256, listBase);
00869         return;
00870     }
00871 #endif
00872     // glXUseXFont() only works with XLFD font structures and a few GL
00873     // drivers crash if 0 is passed as the font handle
00874     f.setStyleStrategy(QFont::OpenGLCompatible);
00875     if (f.handle() && engine->type() == QFontEngine::XLFD)
00876         glXUseXFont(static_cast<Font>(f.handle()), 0, 256, listBase);
00877 }

Here is the call graph for this function:

uint QGLContext::colorIndex ( const QColor c  )  const [protected]

Definition at line 690 of file qgl_x11.cpp.

References QX11Info::appVisual(), QMap< Key, T >::begin(), c, d, QX11Info::display(), QMap< Key, T >::end(), format(), info, QMap< Key, T >::insert(), QColormap::instance(), int, isValid(), overlayTransparentColor(), p, QColormap::pixel(), qBlue(), qGreen(), qRed(), QRgb, QHash< Key, T >::const_iterator::value(), and x.

Referenced by QGLWidget::qglClearColor(), and QGLWidget::qglColor().

00691 {
00692     Q_D(const QGLContext);
00693     int screen = ((XVisualInfo *)d->vi)->screen;
00694     QColormap colmap = QColormap::instance(screen);
00695     if (isValid()) {
00696         if (format().plane()
00697              && colmap.pixel(c) == colmap.pixel(overlayTransparentColor()))
00698             return colmap.pixel(c);                // Special; don't look-up
00699         if (((XVisualInfo*)d->vi)->visualid ==
00700              XVisualIDFromVisual((Visual *) QX11Info::appVisual(screen)))
00701             return colmap.pixel(c);                // We're using QColor's cmap
00702 
00703         XVisualInfo *info = (XVisualInfo *) d->vi;
00704         CMapEntryHash *hash = cmap_handler()->cmap_hash;
00705         CMapEntryHash::ConstIterator it = hash->constFind(long(info->visualid)
00706                 + (info->screen * 256));
00707         QCMapEntry *x = 0;
00708         if (it != hash->constEnd())
00709             x = it.value();
00710         if (x && !x->alloc) {                // It's a standard colormap
00711             int rf = (int)(((float)c.red() * (x->scmap.red_max+1))/256.0);
00712             int gf = (int)(((float)c.green() * (x->scmap.green_max+1))/256.0);
00713             int bf = (int)(((float)c.blue() * (x->scmap.blue_max+1))/256.0);
00714             uint p = x->scmap.base_pixel
00715                      + (rf * x->scmap.red_mult)
00716                      + (gf * x->scmap.green_mult)
00717                      + (bf * x->scmap.blue_mult);
00718             return p;
00719         } else {
00720             QMap<int, QRgb> &cmap = (*cmap_handler()->qglcmap_hash)[(long)info->visualid];
00721 
00722             // already in the map?
00723             QRgb target = c.rgb();
00724             QMap<int, QRgb>::Iterator it = cmap.begin();
00725             for (; it != cmap.end(); ++it) {
00726                 if ((*it) == target)
00727                     return it.key();
00728             }
00729 
00730             // need to alloc color
00731             unsigned long plane_mask[2];
00732             unsigned long color_map_entry;
00733             if (!XAllocColorCells (QX11Info::display(), x->cmap, true, plane_mask, 0,
00734                                    &color_map_entry, 1))
00735                 return colmap.pixel(c);
00736 
00737             XColor col;
00738             col.flags = DoRed | DoGreen | DoBlue;
00739             col.pixel = color_map_entry;
00740             col.red   = (ushort)((qRed(c.rgb()) / 255.0) * 65535.0 + 0.5);
00741             col.green = (ushort)((qGreen(c.rgb()) / 255.0) * 65535.0 + 0.5);
00742             col.blue  = (ushort)((qBlue(c.rgb()) / 255.0) * 65535.0 + 0.5);
00743             XStoreColor(QX11Info::display(), x->cmap, &col);
00744 
00745             cmap.insert(color_map_entry, target);
00746             return color_map_entry;
00747         }
00748     }
00749     return 0;
00750 }

Here is the call graph for this function:

void QGLContext::setValid ( bool  valid  )  [protected]

Definition at line 2052 of file qgl.cpp.

References d.

02053 {
02054     Q_D(QGLContext);
02055     d->valid = valid;
02056 }

void QGLContext::setDevice ( QPaintDevice pDev  )  [protected]

Definition at line 1888 of file qgl.cpp.

References d, isValid(), QInternal::Pbuffer, QInternal::Pixmap, qWarning(), reset(), and QInternal::Widget.

Referenced by QGLWidgetPrivate::initContext().

01889 {
01890     Q_D(QGLContext);
01891     if (isValid())
01892         reset();
01893     d->paintDevice = pDev;
01894     if (d->paintDevice && (d->paintDevice->devType() != QInternal::Widget
01895                            && d->paintDevice->devType() != QInternal::Pixmap
01896                            && d->paintDevice->devType() != QInternal::Pbuffer)) {
01897         qWarning("QGLContext: Unsupported paint device type");
01898     }
01899 }

Here is the call graph for this function:


Friends And Related Function Documentation

friend class QGLPixelBuffer [friend]

Definition at line 282 of file qgl.h.

friend class QGLPixelBufferPrivate [friend]

Definition at line 283 of file qgl.h.

friend class QGLWidget [friend]

Definition at line 284 of file qgl.h.

friend class QGLDrawable [friend]

Definition at line 285 of file qgl.h.

friend class QGLWidgetPrivate [friend]

Definition at line 286 of file qgl.h.

friend class QGLGlyphCache [friend]

Definition at line 287 of file qgl.h.

friend class QOpenGLPaintEngine [friend]

Definition at line 288 of file qgl.h.

friend class QOpenGLPaintEnginePrivate [friend]

Definition at line 289 of file qgl.h.


Member Data Documentation

QGLContext * QGLContext::currentCtx [static, protected]

Definition at line 277 of file qgl.h.

Referenced by doneCurrent(), and makeCurrent().

QGLContextPrivate* QGLContext::d_ptr [private]

Definition at line 280 of file qgl.h.

Referenced by QGLContext().


The documentation for this class was generated from the following files:
Generated on Thu Mar 15 17:44:58 2007 for Qt 4.2 User's Guide by  doxygen 1.5.1