diff options
author | Trond Kjernåsen <trond.kjernasen@nokia.com> | 2010-07-07 13:10:13 (GMT) |
---|---|---|
committer | Trond Kjernåsen <trond.kjernasen@nokia.com> | 2010-07-07 13:10:13 (GMT) |
commit | f2077762cffba8c66cc3880c486161201421e074 (patch) | |
tree | a1e03a3111bd3174b4dba1f5fe0ca4f988c28a19 /src/opengl/gl2paintengineex | |
parent | 6d337a3dd715597aa8ec797f6aeb8f42db129a00 (diff) | |
download | Qt-f2077762cffba8c66cc3880c486161201421e074.zip Qt-f2077762cffba8c66cc3880c486161201421e074.tar.gz Qt-f2077762cffba8c66cc3880c486161201421e074.tar.bz2 |
Made QGL2GradientCache thread safe.
This can be improved by using a QReadWriteLocker, but it means
restructuring the find/insert code. Use a QMutex for now.
Diffstat (limited to 'src/opengl/gl2paintengineex')
-rw-r--r-- | src/opengl/gl2paintengineex/qglgradientcache.cpp | 28 | ||||
-rw-r--r-- | src/opengl/gl2paintengineex/qglgradientcache_p.h | 5 |
2 files changed, 26 insertions, 7 deletions
diff --git a/src/opengl/gl2paintengineex/qglgradientcache.cpp b/src/opengl/gl2paintengineex/qglgradientcache.cpp index f1b095d..cbd5eb8 100644 --- a/src/opengl/gl2paintengineex/qglgradientcache.cpp +++ b/src/opengl/gl2paintengineex/qglgradientcache.cpp @@ -42,18 +42,33 @@ #include "qglgradientcache_p.h" #include <private/qdrawhelper_p.h> #include <private/qgl_p.h> - +#include <QtCore/qmutex.h> QT_BEGIN_NAMESPACE -Q_GLOBAL_STATIC(QGLContextGroupResource<QGL2GradientCache>, qt_gradient_caches) +class QGL2GradientCacheWrapper +{ +public: + QGL2GradientCache *cacheForContext(const QGLContext *context) { + QMutexLocker lock(&m_mutex); + return m_resource.value(context); + } + +private: + QGLContextGroupResource<QGL2GradientCache> m_resource; + QMutex m_mutex; +}; + +Q_GLOBAL_STATIC(QGL2GradientCacheWrapper, qt_gradient_caches) QGL2GradientCache *QGL2GradientCache::cacheForContext(const QGLContext *context) { - return qt_gradient_caches()->value(context); + return qt_gradient_caches()->cacheForContext(context); } -void QGL2GradientCache::cleanCache() { +void QGL2GradientCache::cleanCache() +{ + QMutexLocker lock(&m_mutex); QGLGradientColorTableHash::const_iterator it = cache.constBegin(); for (; it != cache.constEnd(); ++it) { const CacheInfo &cache_info = it.value(); @@ -64,6 +79,7 @@ void QGL2GradientCache::cleanCache() { GLuint QGL2GradientCache::getBuffer(const QGradient &gradient, qreal opacity) { + QMutexLocker lock(&m_mutex); quint64 hash_val = 0; QGradientStops stops = gradient.stops(); @@ -77,7 +93,9 @@ GLuint QGL2GradientCache::getBuffer(const QGradient &gradient, qreal opacity) else { do { const CacheInfo &cache_info = it.value(); - if (cache_info.stops == stops && cache_info.opacity == opacity && cache_info.interpolationMode == gradient.interpolationMode()) { + if (cache_info.stops == stops && cache_info.opacity == opacity + && cache_info.interpolationMode == gradient.interpolationMode()) + { return cache_info.texId; } ++it; diff --git a/src/opengl/gl2paintengineex/qglgradientcache_p.h b/src/opengl/gl2paintengineex/qglgradientcache_p.h index 8ecb34e..7e93d87 100644 --- a/src/opengl/gl2paintengineex/qglgradientcache_p.h +++ b/src/opengl/gl2paintengineex/qglgradientcache_p.h @@ -54,6 +54,7 @@ #include <QObject> #include <QtOpenGL/QtOpenGL> #include <private/qgl_p.h> +#include <QtCore/qmutex.h> QT_BEGIN_NAMESPACE @@ -81,16 +82,16 @@ public: GLuint getBuffer(const QGradient &gradient, qreal opacity); inline int paletteSize() const { return 1024; } -protected: +private: inline int maxCacheSize() const { return 60; } inline void generateGradientColorTable(const QGradient& gradient, uint *colorTable, int size, qreal opacity) const; GLuint addCacheElement(quint64 hash_val, const QGradient &gradient, qreal opacity); - void cleanCache(); QGLGradientColorTableHash cache; + QMutex m_mutex; }; QT_END_NAMESPACE |