summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorSamuel Rødal <samuel.rodal@digia.com>2012-11-26 12:57:09 +0100
committerThe Qt Project <gerrit-noreply@qt-project.org>2012-11-30 18:28:04 +0100
commit6385a182f0f93820c96cf274e50a56492491ee10 (patch)
tree5772f4c8570e620448e3ab7473c9e2df5af4005a /src
parent3ee48926e6584b4afeda1fc406d19d7b1a8d6f20 (diff)
downloadqtbase-6385a182f0f93820c96cf274e50a56492491ee10.tar.gz
Added explicit threading API to QtOpenGL.
Since QtOpenGL/QGLContext is implemented in terms of QtGui/QOpenGLContext which has stricter requirements about how it's supposed to be used, we need to apply these requirements to QGLContext as well. This change adds QGLContext::moveToThread(QThread *) and documents it as a necessity for making a context current on another thread. Also introduces QGLPixelbuffer::context() to access the QGLContext of a pixelbuffer, and made QGLWidget::context() return a non-const QGLContext, since there's no good reason why it shouldn't, and it leads to less const_cast clutter. We could have introduced a backdoor in QOpenGLContext instead, making it loosen its requirements, but that would have made it harder / impossible to fully support threaded OpenGL in all the platforms. Task-number: QTBUG-22560 Change-Id: Ibb6f65f342e7c963e80cc42ab5664c5f1cab30b0 Reviewed-by: Friedemann Kleint <Friedemann.Kleint@digia.com> Reviewed-by: Gunnar Sletta <gunnar.sletta@digia.com>
Diffstat (limited to 'src')
-rw-r--r--src/opengl/qgl.cpp45
-rw-r--r--src/opengl/qgl.h4
-rw-r--r--src/opengl/qgl_p.h9
-rw-r--r--src/opengl/qgl_qpa.cpp11
-rw-r--r--src/opengl/qglpaintdevice.cpp7
-rw-r--r--src/opengl/qglpixelbuffer.cpp29
-rw-r--r--src/opengl/qglpixelbuffer.h2
-rw-r--r--src/opengl/qglpixelbuffer_p.h2
8 files changed, 84 insertions, 25 deletions
diff --git a/src/opengl/qgl.cpp b/src/opengl/qgl.cpp
index c94c8f550e..583ecb69a8 100644
--- a/src/opengl/qgl.cpp
+++ b/src/opengl/qgl.cpp
@@ -3127,6 +3127,19 @@ void QGLContextPrivate::setCurrentContext(QGLContext *context)
}
/*!
+ Moves the QGLContext to the given \a thread.
+
+ Enables calling swapBuffers() and makeCurrent() on the context in
+ the given thread.
+*/
+void QGLContext::moveToThread(QThread *thread)
+{
+ Q_D(QGLContext);
+ if (d->guiGlContext)
+ d->guiGlContext->moveToThread(thread);
+}
+
+/*!
\fn bool QGLContext::chooseContext(const QGLContext* shareContext = 0)
This semi-internal function is called by create(). It creates a
@@ -3195,16 +3208,18 @@ void QGLContextPrivate::setCurrentContext(QGLContext *context)
In some very rare cases the underlying call may fail. If this
occurs an error message is output to stderr.
+
+ If you call this from a thread other than the main UI thread,
+ make sure you've first pushed the context to the relevant thread
+ from the UI thread using moveToThread().
*/
/*!
\fn void QGLContext::swapBuffers() const
- Swaps the screen contents with an off-screen buffer. Only works if
- the context is in double buffer mode.
-
- \sa QGLFormat::setDoubleBuffer()
+ Call this to finish a frame of OpenGL rendering, and make sure to
+ call makeCurrent() again before you begin a new frame.
*/
@@ -3362,13 +3377,18 @@ void QGLContextPrivate::setCurrentContext(QGLContext *context)
1. Call doneCurrent() in the main thread when the rendering is
finished.
- 2. Notify the swapping thread that it can grab the context.
+ 2. Call QGLContext::moveToThread(swapThread) to transfer ownership
+ of the context to the swapping thread.
- 3. Make the rendering context current in the swapping thread with
+ 3. Notify the swapping thread that it can grab the context.
+
+ 4. Make the rendering context current in the swapping thread with
makeCurrent() and then call swapBuffers().
- 4. Call doneCurrent() in the swapping thread and notify the main
- thread that swapping is done.
+ 5. Call doneCurrent() in the swapping thread.
+
+ 6. Call QGLContext::moveToThread(qApp->thread()) and notify the
+ main thread that swapping is done.
Doing this will free up the main thread so that it can continue
with, for example, handling UI events or network requests. Even if
@@ -3400,7 +3420,10 @@ void QGLContextPrivate::setCurrentContext(QGLContext *context)
QGLWidgets can only be created in the main GUI thread. This means
a call to doneCurrent() is necessary to release the GL context
from the main thread, before the widget can be drawn into by
- another thread. Also, the main GUI thread will dispatch resize and
+ another thread. You then need to call QGLContext::moveToThread()
+ to transfer ownership of the context to the thread in which you
+ want to make it current.
+ Also, the main GUI thread will dispatch resize and
paint events to a QGLWidget when the widget is resized, or parts
of it becomes exposed or needs redrawing. It is therefore
necessary to handle those events because the default
@@ -3749,7 +3772,7 @@ void QGLWidget::setFormat(const QGLFormat &format)
/*!
- \fn const QGLContext *QGLWidget::context() const
+ \fn QGLContext *QGLWidget::context() const
Returns the context of this widget.
@@ -4483,7 +4506,7 @@ QGLFormat QGLWidget::format() const
return d->glcx->format();
}
-const QGLContext *QGLWidget::context() const
+QGLContext *QGLWidget::context() const
{
Q_D(const QGLWidget);
return d->glcx;
diff --git a/src/opengl/qgl.h b/src/opengl/qgl.h
index ab2fd8d203..1d21b42cd5 100644
--- a/src/opengl/qgl.h
+++ b/src/opengl/qgl.h
@@ -281,6 +281,8 @@ public:
QGLFormat requestedFormat() const;
void setFormat(const QGLFormat& format);
+ void moveToThread(QThread *thread);
+
virtual void makeCurrent();
virtual void doneCurrent();
@@ -413,7 +415,7 @@ public:
QGLFormat format() const;
void setFormat(const QGLFormat& format);
- const QGLContext* context() const;
+ QGLContext* context() const;
void setContext(QGLContext* context, const QGLContext* shareContext = 0,
bool deleteOldContext = true);
diff --git a/src/opengl/qgl_p.h b/src/opengl/qgl_p.h
index df099ea281..904f3c1a3e 100644
--- a/src/opengl/qgl_p.h
+++ b/src/opengl/qgl_p.h
@@ -380,19 +380,18 @@ class Q_OPENGL_EXPORT QGLTextureDestroyer : public QObject
Q_OBJECT
public:
QGLTextureDestroyer() : QObject() {
- qRegisterMetaType<GLuint>();
- connect(this, SIGNAL(freeTexture(QGLContext *, QPlatformPixmap *, GLuint)),
- this, SLOT(freeTexture_slot(QGLContext *, QPlatformPixmap *, GLuint)));
+ connect(this, SIGNAL(freeTexture(QGLContext *, QPlatformPixmap *, quint32)),
+ this, SLOT(freeTexture_slot(QGLContext *, QPlatformPixmap *, quint32)));
}
void emitFreeTexture(QGLContext *context, QPlatformPixmap *boundPixmap, GLuint id) {
emit freeTexture(context, boundPixmap, id);
}
Q_SIGNALS:
- void freeTexture(QGLContext *context, QPlatformPixmap *boundPixmap, GLuint id);
+ void freeTexture(QGLContext *context, QPlatformPixmap *boundPixmap, quint32 id);
private slots:
- void freeTexture_slot(QGLContext *context, QPlatformPixmap *boundPixmap, GLuint id) {
+ void freeTexture_slot(QGLContext *context, QPlatformPixmap *boundPixmap, quint32 id) {
Q_UNUSED(boundPixmap);
QGLShareContextScope scope(context);
glDeleteTextures(1, &id);
diff --git a/src/opengl/qgl_qpa.cpp b/src/opengl/qgl_qpa.cpp
index f52beceaae..ba07f6121c 100644
--- a/src/opengl/qgl_qpa.cpp
+++ b/src/opengl/qgl_qpa.cpp
@@ -186,9 +186,14 @@ void QGLContext::reset()
d->initDone = false;
QGLContextGroup::removeShare(this);
if (d->guiGlContext) {
- if (d->ownContext)
- delete d->guiGlContext;
- else
+ if (QOpenGLContext::currentContext() == d->guiGlContext)
+ doneCurrent();
+ if (d->ownContext) {
+ if (d->guiGlContext->thread() == QThread::currentThread())
+ delete d->guiGlContext;
+ else
+ d->guiGlContext->deleteLater();
+ } else
d->guiGlContext->setQGLContextHandle(0,0);
d->guiGlContext = 0;
}
diff --git a/src/opengl/qglpaintdevice.cpp b/src/opengl/qglpaintdevice.cpp
index 5aa125f816..e870e45e15 100644
--- a/src/opengl/qglpaintdevice.cpp
+++ b/src/opengl/qglpaintdevice.cpp
@@ -43,6 +43,7 @@
#include <private/qgl_p.h>
#include <private/qglpixelbuffer_p.h>
#include <private/qglframebufferobject_p.h>
+#include <qopenglfunctions.h>
QT_BEGIN_NAMESPACE
@@ -90,7 +91,7 @@ void QGLPaintDevice::beginPaint()
if (m_previousFBO != m_thisFBO) {
ctx->d_ptr->current_fbo = m_thisFBO;
- glBindFramebuffer(GL_FRAMEBUFFER, m_thisFBO);
+ ctx->contextHandle()->functions()->glBindFramebuffer(GL_FRAMEBUFFER, m_thisFBO);
}
// Set the default fbo for the context to m_thisFBO so that
@@ -108,7 +109,7 @@ void QGLPaintDevice::ensureActiveTarget()
if (ctx->d_ptr->current_fbo != m_thisFBO) {
ctx->d_ptr->current_fbo = m_thisFBO;
- glBindFramebuffer(GL_FRAMEBUFFER, m_thisFBO);
+ ctx->contextHandle()->functions()->glBindFramebuffer(GL_FRAMEBUFFER, m_thisFBO);
}
ctx->d_ptr->default_fbo = m_thisFBO;
@@ -120,7 +121,7 @@ void QGLPaintDevice::endPaint()
QGLContext *ctx = context();
if (m_previousFBO != ctx->d_func()->current_fbo) {
ctx->d_ptr->current_fbo = m_previousFBO;
- glBindFramebuffer(GL_FRAMEBUFFER, m_previousFBO);
+ ctx->contextHandle()->functions()->glBindFramebuffer(GL_FRAMEBUFFER, m_previousFBO);
}
ctx->d_ptr->default_fbo = 0;
diff --git a/src/opengl/qglpixelbuffer.cpp b/src/opengl/qglpixelbuffer.cpp
index a5e748a1e7..dd13ef24eb 100644
--- a/src/opengl/qglpixelbuffer.cpp
+++ b/src/opengl/qglpixelbuffer.cpp
@@ -100,6 +100,7 @@
#include "gl2paintengineex/qpaintengineex_opengl2_p.h"
+#include <qglframebufferobject.h>
#include <qglpixelbuffer.h>
#include <private/qglpixelbuffer_p.h>
#include <private/qfont_p.h>
@@ -115,11 +116,23 @@ QGLContext* QGLPBufferGLPaintDevice::context() const
return pbuf->d_func()->qctx;
}
-void QGLPBufferGLPaintDevice::endPaint() {
+void QGLPBufferGLPaintDevice::beginPaint()
+{
+ pbuf->makeCurrent();
+ QGLPaintDevice::beginPaint();
+}
+
+void QGLPBufferGLPaintDevice::endPaint()
+{
glFlush();
QGLPaintDevice::endPaint();
}
+void QGLPBufferGLPaintDevice::setFbo(GLuint fbo)
+{
+ m_thisFBO = fbo;
+}
+
void QGLPBufferGLPaintDevice::setPBuffer(QGLPixelBuffer* pb)
{
pbuf = pb;
@@ -221,6 +234,7 @@ bool QGLPixelBuffer::makeCurrent()
format.setSamples(d->req_format.samples());
d->fbo = new QOpenGLFramebufferObject(d->req_size, format);
d->fbo->bind();
+ d->glDevice.setFbo(d->fbo->handle());
glViewport(0, 0, d->req_size.width(), d->req_size.height());
}
return true;
@@ -242,6 +256,15 @@ bool QGLPixelBuffer::doneCurrent()
}
/*!
+ Returns the context of this pixelbuffer.
+*/
+QGLContext *QGLPixelBuffer::context() const
+{
+ Q_D(const QGLPixelBuffer);
+ return d->qctx;
+}
+
+/*!
\fn GLuint QGLPixelBuffer::generateDynamicTexture() const
Generates and binds a 2D GL texture that is the same size as the
@@ -366,6 +389,8 @@ QImage QGLPixelBuffer::toImage() const
return QImage();
const_cast<QGLPixelBuffer *>(this)->makeCurrent();
+ if (d->fbo)
+ d->fbo->bind();
return qt_gl_read_framebuffer(d->req_size, d->format.alpha(), true);
}
@@ -615,7 +640,7 @@ GLuint QGLPixelBuffer::generateDynamicTexture() const
bool QGLPixelBuffer::hasOpenGLPbuffers()
{
- return QOpenGLFramebufferObject::hasOpenGLFramebufferObjects();
+ return QGLFramebufferObject::hasOpenGLFramebufferObjects();
}
QT_END_NAMESPACE
diff --git a/src/opengl/qglpixelbuffer.h b/src/opengl/qglpixelbuffer.h
index 58cd470d3d..f597a48695 100644
--- a/src/opengl/qglpixelbuffer.h
+++ b/src/opengl/qglpixelbuffer.h
@@ -66,6 +66,8 @@ public:
bool makeCurrent();
bool doneCurrent();
+ QGLContext *context() const;
+
GLuint generateDynamicTexture() const;
bool bindToDynamicTexture(GLuint texture);
void releaseFromDynamicTexture();
diff --git a/src/opengl/qglpixelbuffer_p.h b/src/opengl/qglpixelbuffer_p.h
index fd20cee07a..caa2e2f7d1 100644
--- a/src/opengl/qglpixelbuffer_p.h
+++ b/src/opengl/qglpixelbuffer_p.h
@@ -68,8 +68,10 @@ public:
virtual QPaintEngine* paintEngine() const {return pbuf->paintEngine();}
virtual QSize size() const {return pbuf->size();}
virtual QGLContext* context() const;
+ virtual void beginPaint();
virtual void endPaint();
void setPBuffer(QGLPixelBuffer* pb);
+ void setFbo(GLuint fbo);
private:
QGLPixelBuffer* pbuf;
};