diff options
author | Samuel Rødal <samuel.rodal@nokia.com> | 2011-08-16 09:29:44 +0200 |
---|---|---|
committer | Samuel Rødal <samuel.rodal@nokia.com> | 2011-08-29 10:24:55 +0200 |
commit | aaa4a26f82f99fa8724841eba91bad029306e0ce (patch) | |
tree | 3d24e874bff00efc0e73af610bc80e645d14f789 /src/opengl/qglbuffer.cpp | |
parent | 00fd783a39d55d2365ddead4fab2cc06091c119f (diff) | |
download | qtbase-aaa4a26f82f99fa8724841eba91bad029306e0ce.tar.gz |
Move GL resource handling enablers to QtGui.
Made resource handling more robust by attempting to free GL resources in
the correct thread, and not forcing a context to become current to free
resources.
Change-Id: Ie81d4005b608972375755571d9b50ce82080709b
Reviewed-on: http://codereview.qt.nokia.com/3258
Reviewed-by: Qt Sanity Bot <qt_sanity_bot@ovi.com>
Reviewed-by: Gunnar Sletta <gunnar.sletta@nokia.com>
Diffstat (limited to 'src/opengl/qglbuffer.cpp')
-rw-r--r-- | src/opengl/qglbuffer.cpp | 59 |
1 files changed, 32 insertions, 27 deletions
diff --git a/src/opengl/qglbuffer.cpp b/src/opengl/qglbuffer.cpp index 637e48d27d..53f27392b0 100644 --- a/src/opengl/qglbuffer.cpp +++ b/src/opengl/qglbuffer.cpp @@ -141,7 +141,7 @@ public: QAtomicInt ref; QGLBuffer::Type type; - QGLSharedResourceGuard guard; + QGLSharedResourceGuardBase *guard; QGLBuffer::UsagePattern usagePattern; QGLBuffer::UsagePattern actualUsagePattern; }; @@ -184,7 +184,7 @@ QGLBuffer::QGLBuffer(const QGLBuffer &other) d_ptr->ref.ref(); } -#define ctx d->guard.context() +#define ctx QGLContext::currentContext(); /*! Destroys this buffer object, including the storage being @@ -250,6 +250,14 @@ void QGLBuffer::setUsagePattern(QGLBuffer::UsagePattern value) #undef ctx +namespace { + void freeBufferFunc(QGLContext *ctx, GLuint id) + { + Q_UNUSED(ctx); + glDeleteBuffers(1, &id); + } +} + /*! Creates the buffer object in the GL server. Returns true if the object was created; false otherwise. @@ -266,24 +274,26 @@ void QGLBuffer::setUsagePattern(QGLBuffer::UsagePattern value) bool QGLBuffer::create() { Q_D(QGLBuffer); - if (d->guard.id()) + if (d->guard && d->guard->id()) return true; - const QGLContext *ctx = QGLContext::currentContext(); + QGLContext *ctx = const_cast<QGLContext *>(QGLContext::currentContext()); if (ctx) { - if (!qt_resolve_buffer_extensions(const_cast<QGLContext *>(ctx))) + if (!qt_resolve_buffer_extensions(ctx)) return false; GLuint bufferId = 0; glGenBuffers(1, &bufferId); if (bufferId) { - d->guard.setContext(ctx); - d->guard.setId(bufferId); + if (d->guard) + d->guard->free(); + + d->guard = createSharedResourceGuard(ctx, bufferId, freeBufferFunc); return true; } } return false; } -#define ctx d->guard.context() +#define ctx QGLContext::currentContext() /*! Returns true if this buffer has been created; false otherwise. @@ -293,7 +303,7 @@ bool QGLBuffer::create() bool QGLBuffer::isCreated() const { Q_D(const QGLBuffer); - return d->guard.id() != 0; + return d->guard && d->guard->id(); } /*! @@ -304,14 +314,10 @@ bool QGLBuffer::isCreated() const void QGLBuffer::destroy() { Q_D(QGLBuffer); - GLuint bufferId = d->guard.id(); - if (bufferId) { - // Switch to the original creating context to destroy it. - QGLShareContextScope scope(d->guard.context()); - glDeleteBuffers(1, &bufferId); + if (d->guard) { + d->guard->free(); + d->guard = 0; } - d->guard.setId(0); - d->guard.setContext(0); } /*! @@ -358,7 +364,7 @@ void QGLBuffer::write(int offset, const void *data, int count) qWarning("QGLBuffer::allocate(): buffer not created"); #endif Q_D(QGLBuffer); - if (d->guard.id()) + if (d->guard && d->guard->id()) glBufferSubData(d->type, offset, count, data); } @@ -378,7 +384,7 @@ void QGLBuffer::allocate(const void *data, int count) qWarning("QGLBuffer::allocate(): buffer not created"); #endif Q_D(QGLBuffer); - if (d->guard.id()) + if (d->guard && d->guard->id()) glBufferData(d->type, count, data, d->actualUsagePattern); } @@ -413,10 +419,9 @@ bool QGLBuffer::bind() qWarning("QGLBuffer::bind(): buffer not created"); #endif Q_D(const QGLBuffer); - GLuint bufferId = d->guard.id(); + GLuint bufferId = d->guard ? d->guard->id() : 0; if (bufferId) { - if (!QGLContext::areSharing(QGLContext::currentContext(), - d->guard.context())) { + if (d->guard->group() != QGuiGLContextGroup::currentContextGroup()) { #ifndef QT_NO_DEBUG qWarning("QGLBuffer::bind: buffer is not valid in the current context"); #endif @@ -445,7 +450,7 @@ void QGLBuffer::release() qWarning("QGLBuffer::release(): buffer not created"); #endif Q_D(const QGLBuffer); - if (d->guard.id()) + if (d->guard && d->guard->id()) glBindBuffer(d->type, 0); } @@ -471,7 +476,7 @@ void QGLBuffer::release(QGLBuffer::Type type) glBindBuffer(GLenum(type), 0); } -#define ctx d->guard.context() +#define ctx QGLContext::currentContext() /*! Returns the GL identifier associated with this buffer; zero if @@ -482,7 +487,7 @@ void QGLBuffer::release(QGLBuffer::Type type) GLuint QGLBuffer::bufferId() const { Q_D(const QGLBuffer); - return d->guard.id(); + return d->guard ? d->guard->id() : 0; } #ifndef GL_BUFFER_SIZE @@ -501,7 +506,7 @@ GLuint QGLBuffer::bufferId() const int QGLBuffer::size() const { Q_D(const QGLBuffer); - if (!d->guard.id()) + if (!d->guard || !d->guard->id()) return -1; GLint value = -1; glGetBufferParameteriv(d->type, GL_BUFFER_SIZE, &value); @@ -529,7 +534,7 @@ void *QGLBuffer::map(QGLBuffer::Access access) if (!isCreated()) qWarning("QGLBuffer::map(): buffer not created"); #endif - if (!d->guard.id()) + if (!d->guard || !d->guard->id()) return 0; if (!glMapBufferARB) return 0; @@ -556,7 +561,7 @@ bool QGLBuffer::unmap() if (!isCreated()) qWarning("QGLBuffer::unmap(): buffer not created"); #endif - if (!d->guard.id()) + if (!d->guard || !d->guard->id()) return false; if (!glUnmapBufferARB) return false; |