diff options
author | Rhys Weatherley <rhys.weatherley@nokia.com> | 2009-09-14 02:30:44 (GMT) |
---|---|---|
committer | Rhys Weatherley <rhys.weatherley@nokia.com> | 2009-09-14 05:06:06 (GMT) |
commit | 9e3fb515207f8d80fe87b6e34e78d3d201b0b438 (patch) | |
tree | e89933d4ba8f65f5099e1beaab33a514c247e2cd /src/opengl | |
parent | 732660e89af34c76f6aa8a46ce8507dfd5fad077 (diff) | |
download | Qt-9e3fb515207f8d80fe87b6e34e78d3d201b0b438.zip Qt-9e3fb515207f8d80fe87b6e34e78d3d201b0b438.tar.gz Qt-9e3fb515207f8d80fe87b6e34e78d3d201b0b438.tar.bz2 |
Performance: reduce TLS overhead of QGLContext::currentContext()
The handling for the current QGLContext was looking up the same
TLS data several times per call (hasLocalData() and localData()
calls particularly).
This change also refactors the code a little so that the
setting of the QGLContext within makeCurrent() and doneCurrent()
is in one location in the code instead of six (one per platform).
Reviewed-by: Michael Brasser
Reviewed-by: Sarah Smith
Diffstat (limited to 'src/opengl')
-rw-r--r-- | src/opengl/qgl.cpp | 27 | ||||
-rw-r--r-- | src/opengl/qgl_mac.mm | 10 | ||||
-rw-r--r-- | src/opengl/qgl_p.h | 7 | ||||
-rw-r--r-- | src/opengl/qgl_qws.cpp | 13 | ||||
-rw-r--r-- | src/opengl/qgl_win.cpp | 10 | ||||
-rw-r--r-- | src/opengl/qgl_wince.cpp | 13 | ||||
-rw-r--r-- | src/opengl/qgl_x11.cpp | 13 | ||||
-rw-r--r-- | src/opengl/qgl_x11egl.cpp | 13 |
8 files changed, 42 insertions, 64 deletions
diff --git a/src/opengl/qgl.cpp b/src/opengl/qgl.cpp index c2b747a..1a0957c 100644 --- a/src/opengl/qgl.cpp +++ b/src/opengl/qgl.cpp @@ -103,7 +103,11 @@ QT_BEGIN_NAMESPACE QGLExtensionFuncs QGLContextPrivate::qt_extensionFuncs; #endif -QThreadStorage<QGLThreadContext *> qgl_context_storage; +struct QGLThreadContext { + QGLContext *context; +}; + +static QThreadStorage<QGLThreadContext *> qgl_context_storage; Q_GLOBAL_STATIC(QGLFormat, qgl_default_format) @@ -2939,11 +2943,28 @@ void QGLContext::setInitialized(bool on) const QGLContext* QGLContext::currentContext() { - if (qgl_context_storage.hasLocalData()) - return qgl_context_storage.localData()->context; + QGLThreadContext *threadContext = qgl_context_storage.localData(); + if (threadContext) + return threadContext->context; return 0; } +void QGLContextPrivate::setCurrentContext(QGLContext *context) +{ + QGLThreadContext *threadContext = qgl_context_storage.localData(); + if (!threadContext) { + if (!QThread::currentThread()) { + // We don't have a current QThread, so just set the static. + QGLContext::currentCtx = context; + return; + } + threadContext = new QGLThreadContext; + qgl_context_storage.setLocalData(threadContext); + } + threadContext->context = context; + QGLContext::currentCtx = context; // XXX: backwards-compat, not thread-safe +} + /*! \fn bool QGLContext::chooseContext(const QGLContext* shareContext = 0) diff --git a/src/opengl/qgl_mac.mm b/src/opengl/qgl_mac.mm index dd9d9ff..063082b 100644 --- a/src/opengl/qgl_mac.mm +++ b/src/opengl/qgl_mac.mm @@ -493,11 +493,7 @@ void QGLContext::makeCurrent() #else [static_cast<NSOpenGLContext *>(d->cx) makeCurrentContext]; #endif - currentCtx = this; - if (!qgl_context_storage.hasLocalData() && QThread::currentThread()) - qgl_context_storage.setLocalData(new QGLThreadContext); - if (qgl_context_storage.hasLocalData()) - qgl_context_storage.localData()->context = this; + QGLContextPrivate::setCurrentContext(this); } #ifndef QT_MAC_USE_COCOA @@ -656,9 +652,7 @@ void QGLContext::doneCurrent() ) return; - currentCtx = 0; - if (qgl_context_storage.hasLocalData()) - qgl_context_storage.localData()->context = 0; + QGLContextPrivate::setCurrentContext(0); #ifndef QT_MAC_USE_COCOA aglSetCurrentContext(0); #else diff --git a/src/opengl/qgl_p.h b/src/opengl/qgl_p.h index 57b40fb..2b74e69 100644 --- a/src/opengl/qgl_p.h +++ b/src/opengl/qgl_p.h @@ -351,6 +351,8 @@ public: static inline QGLExtensionFuncs& extensionFuncs(QGLContextGroup *) { return qt_extensionFuncs; } #endif + static void setCurrentContext(QGLContext *context); + QPixmapFilter *createPixmapFilter(int type) const; }; @@ -398,11 +400,6 @@ public: Q_DECLARE_OPERATORS_FOR_FLAGS(QGLExtensions::Extensions) -struct QGLThreadContext { - QGLContext *context; -}; -extern QThreadStorage<QGLThreadContext *> qgl_context_storage; - class QGLShareRegister { public: diff --git a/src/opengl/qgl_qws.cpp b/src/opengl/qgl_qws.cpp index 759f9de..7197776 100644 --- a/src/opengl/qgl_qws.cpp +++ b/src/opengl/qgl_qws.cpp @@ -260,13 +260,8 @@ void QGLContext::makeCurrent() return; } - if (d->eglContext->makeCurrent()) { - if (!qgl_context_storage.hasLocalData() && QThread::currentThread()) - qgl_context_storage.setLocalData(new QGLThreadContext); - if (qgl_context_storage.hasLocalData()) - qgl_context_storage.localData()->context = this; - currentCtx = this; - } + if (d->eglContext->makeCurrent()) + QGLContextPrivate::setCurrentContext(this); } void QGLContext::doneCurrent() @@ -275,9 +270,7 @@ void QGLContext::doneCurrent() if (d->eglContext) d->eglContext->doneCurrent(); - if (qgl_context_storage.hasLocalData()) - qgl_context_storage.localData()->context = 0; - currentCtx = 0; + QGLContextPrivate::setCurrentContext(0); } diff --git a/src/opengl/qgl_win.cpp b/src/opengl/qgl_win.cpp index 78dad80..2f9e225 100644 --- a/src/opengl/qgl_win.cpp +++ b/src/opengl/qgl_win.cpp @@ -1173,11 +1173,7 @@ void QGLContext::makeCurrent() } if (wglMakeCurrent(d->dc, d->rc)) { - if (!qgl_context_storage.hasLocalData() && QThread::currentThread()) - qgl_context_storage.setLocalData(new QGLThreadContext); - if (qgl_context_storage.hasLocalData()) - qgl_context_storage.localData()->context = this; - currentCtx = this; + QGLContextPrivate::setCurrentContext(this); } else { qwglError("QGLContext::makeCurrent()", "wglMakeCurrent"); } @@ -1187,10 +1183,8 @@ void QGLContext::makeCurrent() void QGLContext::doneCurrent() { Q_D(QGLContext); - currentCtx = 0; wglMakeCurrent(0, 0); - if (qgl_context_storage.hasLocalData()) - qgl_context_storage.localData()->context = 0; + QGLContextPrivate::setCurrentContext(0); if (deviceIsPixmap() && d->hbitmap) { QPixmap *pm = static_cast<QPixmap *>(d->paintDevice); *pm = QPixmap::fromWinHBITMAP(d->hbitmap); diff --git a/src/opengl/qgl_wince.cpp b/src/opengl/qgl_wince.cpp index 83efca8..4e655f1 100644 --- a/src/opengl/qgl_wince.cpp +++ b/src/opengl/qgl_wince.cpp @@ -450,13 +450,8 @@ void QGLContext::makeCurrent() return; } - if (d->eglContext->makeCurrent()) { - if (!qgl_context_storage.hasLocalData() && QThread::currentThread()) - qgl_context_storage.setLocalData(new QGLThreadContext); - if (qgl_context_storage.hasLocalData()) - qgl_context_storage.localData()->context = this; - currentCtx = this; - } + if (d->eglContext->makeCurrent()) + QGLContextPrivate::setCurrentContext(this); } @@ -467,9 +462,7 @@ void QGLContext::doneCurrent() if (d->eglContext) d->eglContext->doneCurrent(); - if (qgl_context_storage.hasLocalData()) - qgl_context_storage.localData()->context = 0; - currentCtx = 0; + QGLContextPrivate::setCurrentContext(0); } void QGLContext::swapBuffers() const diff --git a/src/opengl/qgl_x11.cpp b/src/opengl/qgl_x11.cpp index cb1da18..da7972d 100644 --- a/src/opengl/qgl_x11.cpp +++ b/src/opengl/qgl_x11.cpp @@ -791,22 +791,15 @@ void QGLContext::makeCurrent() if (!ok) qWarning("QGLContext::makeCurrent(): Failed."); - if (ok) { - if (!qgl_context_storage.hasLocalData() && QThread::currentThread()) - qgl_context_storage.setLocalData(new QGLThreadContext); - if (qgl_context_storage.hasLocalData()) - qgl_context_storage.localData()->context = this; - currentCtx = this; - } + if (ok) + QGLContextPrivate::setCurrentContext(this); } void QGLContext::doneCurrent() { Q_D(QGLContext); glXMakeCurrent(qt_x11Info(d->paintDevice)->display(), 0, 0); - if (qgl_context_storage.hasLocalData()) - qgl_context_storage.localData()->context = 0; - currentCtx = 0; + QGLContextPrivate::setCurrentContext(0); } diff --git a/src/opengl/qgl_x11egl.cpp b/src/opengl/qgl_x11egl.cpp index c54315f..7dfd642 100644 --- a/src/opengl/qgl_x11egl.cpp +++ b/src/opengl/qgl_x11egl.cpp @@ -146,13 +146,8 @@ void QGLContext::makeCurrent() return; } - if (d->eglContext->makeCurrent()) { - if (!qgl_context_storage.hasLocalData() && QThread::currentThread()) - qgl_context_storage.setLocalData(new QGLThreadContext); - if (qgl_context_storage.hasLocalData()) - qgl_context_storage.localData()->context = this; - currentCtx = this; - } + if (d->eglContext->makeCurrent()) + QGLContextPrivate::setCurrentContext(this); } void QGLContext::doneCurrent() @@ -161,9 +156,7 @@ void QGLContext::doneCurrent() if (d->eglContext) d->eglContext->doneCurrent(); - if (qgl_context_storage.hasLocalData()) - qgl_context_storage.localData()->context = 0; - currentCtx = 0; + QGLContextPrivate::setCurrentContext(0); } |