diff options
author | Rhys Weatherley <rhys.weatherley@nokia.com> | 2009-10-05 01:55:53 (GMT) |
---|---|---|
committer | Rhys Weatherley <rhys.weatherley@nokia.com> | 2009-10-05 01:55:53 (GMT) |
commit | 7b19bfec4c496f2112b363cbdc8a0c302a48cfce (patch) | |
tree | 0de2b3f93d6b58aa54c31a39ca44d1cb8fea0337 /src/opengl/qgl_p.h | |
parent | a3add3d6ab1b7a3e4dbb2df81fd51625b834fe45 (diff) | |
download | Qt-7b19bfec4c496f2112b363cbdc8a0c302a48cfce.zip Qt-7b19bfec4c496f2112b363cbdc8a0c302a48cfce.tar.gz Qt-7b19bfec4c496f2112b363cbdc8a0c302a48cfce.tar.bz2 |
Consistently use QGLShareContextScope for context switching
QGLShareContextScope is safer and more reliable than trying to manually
detect how and when to temporarily switch contexts. Replace the few
remaining instances of context-switching with it.
Reviewed-by: trustme
Diffstat (limited to 'src/opengl/qgl_p.h')
-rw-r--r-- | src/opengl/qgl_p.h | 90 |
1 files changed, 42 insertions, 48 deletions
diff --git a/src/opengl/qgl_p.h b/src/opengl/qgl_p.h index 38a1845..8d4f673 100644 --- a/src/opengl/qgl_p.h +++ b/src/opengl/qgl_p.h @@ -400,6 +400,46 @@ public: extern Q_OPENGL_EXPORT QGLShareRegister* qgl_share_reg(); +// Temporarily make a context current if not already current or +// shared with the current contex. The previous context is made +// current when the object goes out of scope. +class Q_OPENGL_EXPORT QGLShareContextScope +{ +public: + QGLShareContextScope(const QGLContext *ctx) + : m_oldContext(0) + { + QGLContext *currentContext = const_cast<QGLContext *>(QGLContext::currentContext()); + if (currentContext != ctx && !QGLContext::areSharing(ctx, currentContext)) { + m_oldContext = currentContext; + m_ctx = const_cast<QGLContext *>(ctx); + m_ctx->makeCurrent(); + } else { + m_ctx = currentContext; + } + } + + operator QGLContext *() + { + return m_ctx; + } + + QGLContext *operator->() + { + return m_ctx; + } + + ~QGLShareContextScope() + { + if (m_oldContext) + m_oldContext->makeCurrent(); + } + +private: + QGLContext *m_oldContext; + QGLContext *m_ctx; +}; + class QGLTexture { public: QGLTexture(QGLContext *ctx = 0, GLuint tx_id = 0, GLenum tx_target = GL_TEXTURE_2D, @@ -415,12 +455,8 @@ public: ~QGLTexture() { if (options & QGLContext::MemoryManagedBindOption) { - QGLContext *current = const_cast<QGLContext *>(QGLContext::currentContext()); - QGLContext *ctx = const_cast<QGLContext *>(context); - Q_ASSERT(ctx); - bool switch_context = current != ctx && !QGLContext::areSharing(current, ctx); - if (switch_context) - ctx->makeCurrent(); + Q_ASSERT(context); + QGLShareContextScope scope(context); #if defined(Q_WS_X11) // Although glXReleaseTexImage is a glX call, it must be called while there // is a current context - the context the pixmap was bound to a texture in. @@ -430,8 +466,6 @@ public: QGLContextPrivate::unbindPixmapFromTexture(boundPixmap); #endif glDeleteTextures(1, &id); - if (switch_context && current) - current->makeCurrent(); } } @@ -512,46 +546,6 @@ private: QAtomicInt active; }; -// Temporarily make a context current if not already current or -// shared with the current contex. The previous context is made -// current when the object goes out of scope. -class Q_OPENGL_EXPORT QGLShareContextScope -{ -public: - QGLShareContextScope(const QGLContext *ctx) - : m_oldContext(0) - { - QGLContext *currentContext = const_cast<QGLContext *>(QGLContext::currentContext()); - if (currentContext != ctx && !QGLContext::areSharing(ctx, currentContext)) { - m_oldContext = currentContext; - m_ctx = const_cast<QGLContext *>(ctx); - m_ctx->makeCurrent(); - } else { - m_ctx = currentContext; - } - } - - operator QGLContext *() - { - return m_ctx; - } - - QGLContext *operator->() - { - return m_ctx; - } - - ~QGLShareContextScope() - { - if (m_oldContext) - m_oldContext->makeCurrent(); - } - -private: - QGLContext *m_oldContext; - QGLContext *m_ctx; -}; - // Put a guard around a GL object identifier and its context. // When the context goes away, a shared context will be used // in its place. If there are no more shared contexts, then |