summaryrefslogtreecommitdiffstats
path: root/src/opengl/qgl_p.h
diff options
context:
space:
mode:
authorRhys Weatherley <rhys.weatherley@nokia.com>2009-09-09 06:23:54 (GMT)
committerRhys Weatherley <rhys.weatherley@nokia.com>2009-09-09 06:23:54 (GMT)
commit6eb6920d134ee6a0cc0f27d63d65003a3d9c1fee (patch)
treee4abaa818134c282dc37e62a241d9b7b89b5fb0b /src/opengl/qgl_p.h
parent39bcbc8df52eb5df5f7e39dc4265c8a77d3881be (diff)
downloadQt-6eb6920d134ee6a0cc0f27d63d65003a3d9c1fee.zip
Qt-6eb6920d134ee6a0cc0f27d63d65003a3d9c1fee.tar.gz
Qt-6eb6920d134ee6a0cc0f27d63d65003a3d9c1fee.tar.bz2
Clean up shader programs properly
QGLShader and QGLShaderProgram used to delete the GL object id in the wrong context. But fixing this means we must keep track of when contexts are destroyed so that we don't try to access a context from a shader if it goes away. We also need to transfer ownership from one context to another when they are shared. This change introduces QGLSharedResourceGuard, which keeps track of a context and a GL object identifier. When the context goes away, ownership is passed to a shared context. When there are no more shared contexts, the identifier automatically zeroes. Reviewed-by: trustme
Diffstat (limited to 'src/opengl/qgl_p.h')
-rw-r--r--src/opengl/qgl_p.h86
1 files changed, 85 insertions, 1 deletions
diff --git a/src/opengl/qgl_p.h b/src/opengl/qgl_p.h
index b10d5da..410dfc1 100644
--- a/src/opengl/qgl_p.h
+++ b/src/opengl/qgl_p.h
@@ -237,13 +237,37 @@ private:
friend class QGLContextPrivate;
};
+// Reference to a QGLContext which automatically switches to another
+// shared context when the main one is destroyed. If there is no
+// shared context to switch to, the context pointer is set to null.
+// Note: should be merged into QGLContextGroup at some point.
+class QGLContextReference : public QObject
+{
+ Q_OBJECT
+public:
+ QGLContextReference(const QGLContext *ctx);
+ ~QGLContextReference() {}
+
+ const QGLContext *context() const { return m_ctx; }
+
+ void ref() { m_ref.ref(); }
+ bool deref() { return m_ref.deref(); }
+
+private slots:
+ void aboutToDestroyContext(const QGLContext *ctx);
+
+private:
+ QAtomicInt m_ref;
+ const QGLContext *m_ctx;
+};
+
class QGLTexture;
class QGLContextPrivate
{
Q_DECLARE_PUBLIC(QGLContext)
public:
- explicit QGLContextPrivate(QGLContext *context) : internal_context(false), q_ptr(context) {group = new QGLContextGroup(context);}
+ explicit QGLContextPrivate(QGLContext *context) : internal_context(false), q_ptr(context) {reference = new QGLContextReference(context); group = new QGLContextGroup(context);}
~QGLContextPrivate();
QGLTexture *bindTexture(const QImage &image, GLenum target, GLint format,
QGLContext::BindOptions options);
@@ -305,6 +329,7 @@ public:
QGLContext *q_ptr;
QGLFormat::OpenGLVersionFlags version_flags;
+ QGLContextReference *reference;
QGLContextGroup *group;
GLint max_texture_size;
@@ -554,6 +579,65 @@ private:
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
+// the identifier is returned as zero - it is assumed that the
+// context destruction cleaned up the identifier in this case.
+class Q_OPENGL_EXPORT QGLSharedResourceGuard
+{
+public:
+ QGLSharedResourceGuard(const QGLContext *context)
+ : m_ctxref(0), m_id(0)
+ {
+ setContext(context);
+ }
+ QGLSharedResourceGuard(const QGLContext *context, GLuint id)
+ : m_ctxref(0), m_id(id)
+ {
+ setContext(context);
+ }
+ ~QGLSharedResourceGuard()
+ {
+ if (m_ctxref && !m_ctxref->deref())
+ delete m_ctxref;
+ }
+
+ const QGLContext *context() const
+ {
+ return m_ctxref ? m_ctxref->context() : 0;
+ }
+
+ void setContext(const QGLContext *context)
+ {
+ if (m_ctxref && !m_ctxref->deref())
+ delete m_ctxref;
+ if (context) {
+ m_ctxref = context->d_ptr->reference;
+ m_ctxref->ref();
+ } else {
+ m_ctxref = 0;
+ }
+ }
+
+ GLuint id() const
+ {
+ if (m_ctxref && m_ctxref->context())
+ return m_id;
+ else
+ return 0;
+ }
+
+ void setId(GLuint id)
+ {
+ m_id = id;
+ }
+
+private:
+ QGLContextReference *m_ctxref;
+ GLuint m_id;
+};
+
QT_END_NAMESPACE
#endif // QGL_P_H