summaryrefslogtreecommitdiffstats
path: root/src/opengl
diff options
context:
space:
mode:
authorRhys Weatherley <rhys.weatherley@nokia.com>2009-10-05 01:55:53 (GMT)
committerRhys Weatherley <rhys.weatherley@nokia.com>2009-10-05 01:55:53 (GMT)
commit7b19bfec4c496f2112b363cbdc8a0c302a48cfce (patch)
tree0de2b3f93d6b58aa54c31a39ca44d1cb8fea0337 /src/opengl
parenta3add3d6ab1b7a3e4dbb2df81fd51625b834fe45 (diff)
downloadQt-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')
-rw-r--r--src/opengl/gl2paintengineex/qglengineshadermanager.cpp6
-rw-r--r--src/opengl/gl2paintengineex/qglgradientcache.cpp6
-rw-r--r--src/opengl/gl2paintengineex/qpaintengineex_opengl2.cpp6
-rw-r--r--src/opengl/qgl_p.h90
4 files changed, 45 insertions, 63 deletions
diff --git a/src/opengl/gl2paintengineex/qglengineshadermanager.cpp b/src/opengl/gl2paintengineex/qglengineshadermanager.cpp
index 59c50aa..e22303d 100644
--- a/src/opengl/gl2paintengineex/qglengineshadermanager.cpp
+++ b/src/opengl/gl2paintengineex/qglengineshadermanager.cpp
@@ -60,12 +60,8 @@ QGLEngineSharedShaders *QGLEngineSharedShaders::shadersForContext(const QGLConte
{
QGLEngineSharedShaders *p = reinterpret_cast<QGLEngineSharedShaders *>(qt_shared_shaders()->value(context));
if (!p) {
- QGLContext *oldContext = const_cast<QGLContext *>(QGLContext::currentContext());
- if (oldContext != context)
- const_cast<QGLContext *>(context)->makeCurrent();
+ QGLShareContextScope scope(context);
qt_shared_shaders()->insert(context, p = new QGLEngineSharedShaders(context));
- if (oldContext && oldContext != context)
- oldContext->makeCurrent();
}
return p;
}
diff --git a/src/opengl/gl2paintengineex/qglgradientcache.cpp b/src/opengl/gl2paintengineex/qglgradientcache.cpp
index 4b2b2a0..e06f15d 100644
--- a/src/opengl/gl2paintengineex/qglgradientcache.cpp
+++ b/src/opengl/gl2paintengineex/qglgradientcache.cpp
@@ -57,13 +57,9 @@ QGL2GradientCache *QGL2GradientCache::cacheForContext(const QGLContext *context)
{
QGL2GradientCache *p = reinterpret_cast<QGL2GradientCache *>(qt_gradient_caches()->value(context));
if (!p) {
- QGLContext *oldContext = const_cast<QGLContext *>(QGLContext::currentContext());
- if (oldContext != context)
- const_cast<QGLContext *>(context)->makeCurrent();
+ QGLShareContextScope scope(context);
p = new QGL2GradientCache;
qt_gradient_caches()->insert(context, p);
- if (oldContext && oldContext != context)
- oldContext->makeCurrent();
}
return p;
}
diff --git a/src/opengl/gl2paintengineex/qpaintengineex_opengl2.cpp b/src/opengl/gl2paintengineex/qpaintengineex_opengl2.cpp
index 992d47d..073f7db 100644
--- a/src/opengl/gl2paintengineex/qpaintengineex_opengl2.cpp
+++ b/src/opengl/gl2paintengineex/qpaintengineex_opengl2.cpp
@@ -162,15 +162,11 @@ QGLTextureGlyphCache::QGLTextureGlyphCache(QGLContext *context, QFontEngineGlyph
QGLTextureGlyphCache::~QGLTextureGlyphCache()
{
if (ctx) {
- QGLContext *oldContext = const_cast<QGLContext *>(QGLContext::currentContext());
- if (oldContext != ctx)
- ctx->makeCurrent();
+ QGLShareContextScope scope(ctx);
glDeleteFramebuffers(1, &m_fbo);
if (m_width || m_height)
glDeleteTextures(1, &m_texture);
- if (oldContext && oldContext != ctx)
- oldContext->makeCurrent();
}
}
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