diff options
author | Jørgen Lind <jorgen.lind@nokia.com> | 2010-11-18 13:03:59 +0100 |
---|---|---|
committer | Jørgen Lind <jorgen.lind@nokia.com> | 2010-11-22 14:14:08 +0100 |
commit | 292f6a9ba1b5da049e4898525974c6f0575ccd65 (patch) | |
tree | b44bdf07f56298d7206a180a41690c2de3371fc6 /src/gui/kernel/qplatformglcontext_qpa.cpp | |
parent | a1acef227647b3043998f9ccf364ead5c29b882d (diff) |
Lighthouse: move the currentContext functionality to QPlatformGLContext
This means the threading functionality has been delegated down to
QPlatformGLContext. However, it is still possible to use
QGLContext::currentContext to retrieve the QGLContext. This so that
QGLFunctions, QGLShaderProgram etc can be used without a QGLWidget.
Reviewed-by: paul
Diffstat (limited to 'src/gui/kernel/qplatformglcontext_qpa.cpp')
-rw-r--r-- | src/gui/kernel/qplatformglcontext_qpa.cpp | 116 |
1 files changed, 111 insertions, 5 deletions
diff --git a/src/gui/kernel/qplatformglcontext_qpa.cpp b/src/gui/kernel/qplatformglcontext_qpa.cpp index 36db2b047d..5ed1d3dd36 100644 --- a/src/gui/kernel/qplatformglcontext_qpa.cpp +++ b/src/gui/kernel/qplatformglcontext_qpa.cpp @@ -41,17 +41,123 @@ #include "qplatformglcontext_qpa.h" +#include <QtCore/QThreadStorage> +#include <QtCore/QThread> + +#include <QDebug> + +class QPlatformGLThreadContext +{ +public: + ~QPlatformGLThreadContext() { + if (context) + context->doneCurrent(); + } + QPlatformGLContext *context; +}; + +static QThreadStorage<QPlatformGLThreadContext *> qplatformgl_context_storage; + +class QPlatformGLContextPrivate +{ +public: + QPlatformGLContextPrivate(QPlatformWindow *platformWindow) + :qGLContextHandle(0),platformWindow(platformWindow) + { + } + + virtual ~QPlatformGLContextPrivate() + { + //do not delete the QGLContext handle here as it is deleted in + //QWidgetPrivate::deleteTLSysExtra() + } + void *qGLContextHandle; + void (*qGLContextDeleteFunction)(void *handle); + QPlatformWindow *platformWindow; + static QPlatformGLContext *staticSharedContext; + + static void setCurrentContext(QPlatformGLContext *context); +}; + +QPlatformGLContext *QPlatformGLContextPrivate::staticSharedContext = 0; + +void QPlatformGLContextPrivate::setCurrentContext(QPlatformGLContext *context) +{ + QPlatformGLThreadContext *threadContext = qplatformgl_context_storage.localData(); + if (!threadContext) { + if (!QThread::currentThread()) { + qWarning("No QTLS available. currentContext wont work"); + return; + } + threadContext = new QPlatformGLThreadContext; + qplatformgl_context_storage.setLocalData(threadContext); + } + threadContext->context = context; +} + +QPlatformWindow *QPlatformGLContext::platformWindow() const +{ + Q_D(const QPlatformGLContext); + return d->platformWindow; +} + +const QPlatformGLContext* QPlatformGLContext::currentContext() +{ + QPlatformGLThreadContext *threadContext = qplatformgl_context_storage.localData(); + if(threadContext) { + return threadContext->context; + } + return 0; +} + +QPlatformGLContext::QPlatformGLContext(QPlatformWindow *platformWindow) + :d_ptr(new QPlatformGLContextPrivate(platformWindow)) +{ +} + QPlatformGLContext::~QPlatformGLContext() -{ } +{ + if (QPlatformGLContext::currentContext() == this) { + doneCurrent(); + } -static QPlatformGLContext *staticSharedContext = 0; +} void QPlatformGLContext::setDefaultSharedContext(QPlatformGLContext *sharedContext) { - staticSharedContext = sharedContext; + QPlatformGLContextPrivate::staticSharedContext = sharedContext; +} + +const QPlatformGLContext *QPlatformGLContext::defaultSharedContext() +{ + return QPlatformGLContextPrivate::staticSharedContext; +} + +void QPlatformGLContext::makeCurrent() +{ + QPlatformGLContextPrivate::setCurrentContext(this); +} + +void QPlatformGLContext::doneCurrent() +{ + QPlatformGLContextPrivate::setCurrentContext(0); +} + +void *QPlatformGLContext::qGLContextHandle() const +{ + Q_D(const QPlatformGLContext); + return d->qGLContextHandle; +} + +void QPlatformGLContext::setQGLContextHandle(void *handle,void (*qGLContextDeleteFunction)(void *)) +{ + Q_D(QPlatformGLContext); + d->qGLContextHandle = handle; + d->qGLContextDeleteFunction = qGLContextDeleteFunction; } -QPlatformGLContext *QPlatformGLContext::defaultSharedContext() +void QPlatformGLContext::deleteQGLContext() { - return staticSharedContext; + Q_D(QPlatformGLContext); + d->qGLContextDeleteFunction(d->qGLContextHandle); } |