From 82fa80a6b28ea5a4d3e245f531fea22a689c7ad3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Trond=20Kjern=C3=A5sen?= Date: Thu, 11 Mar 2010 15:52:37 +0100 Subject: Made QGLTextureCache::instance() threadsafe. Reviewed-by: Tom Cooksey --- src/opengl/qgl.cpp | 27 +++++++-------------------- src/opengl/qgl_p.h | 1 - 2 files changed, 7 insertions(+), 21 deletions(-) diff --git a/src/opengl/qgl.cpp b/src/opengl/qgl.cpp index 7839191..c6ecef6 100644 --- a/src/opengl/qgl.cpp +++ b/src/opengl/qgl.cpp @@ -1688,14 +1688,12 @@ typedef void (*_qt_image_cleanup_hook_64)(qint64); extern Q_GUI_EXPORT _qt_pixmap_cleanup_hook_64 qt_pixmap_cleanup_hook_64; extern Q_GUI_EXPORT _qt_image_cleanup_hook_64 qt_image_cleanup_hook_64; -static QGLTextureCache *qt_gl_texture_cache = 0; + +Q_GLOBAL_STATIC(QGLTextureCache, qt_gl_texture_cache) QGLTextureCache::QGLTextureCache() : m_cache(64*1024) // cache ~64 MB worth of textures - this is not accurate though { - Q_ASSERT(qt_gl_texture_cache == 0); - qt_gl_texture_cache = this; - QImagePixmapCleanupHooks::instance()->addPixmapDataModificationHook(cleanupTexturesForPixampData); QImagePixmapCleanupHooks::instance()->addPixmapDataDestructionHook(cleanupBeforePixmapDestruction); QImagePixmapCleanupHooks::instance()->addImageHook(cleanupTexturesForCacheKey); @@ -1703,8 +1701,7 @@ QGLTextureCache::QGLTextureCache() QGLTextureCache::~QGLTextureCache() { - qt_gl_texture_cache = 0; - + Q_ASSERT(size() == 0); QImagePixmapCleanupHooks::instance()->removePixmapDataModificationHook(cleanupTexturesForPixampData); QImagePixmapCleanupHooks::instance()->removePixmapDataDestructionHook(cleanupBeforePixmapDestruction); QImagePixmapCleanupHooks::instance()->removeImageHook(cleanupTexturesForCacheKey); @@ -1754,22 +1751,14 @@ void QGLTextureCache::removeContextTextures(QGLContext* ctx) } } -QGLTextureCache* QGLTextureCache::instance() -{ - if (!qt_gl_texture_cache) - qt_gl_texture_cache = new QGLTextureCache; - - return qt_gl_texture_cache; -} - /* a hook that removes textures from the cache when a pixmap/image is deref'ed */ void QGLTextureCache::cleanupTexturesForCacheKey(qint64 cacheKey) { - instance()->remove(cacheKey); - Q_ASSERT(instance()->getTexture(cacheKey) == 0); + qt_gl_texture_cache()->remove(cacheKey); + Q_ASSERT(qt_gl_texture_cache()->getTexture(cacheKey) == 0); } @@ -1791,10 +1780,9 @@ void QGLTextureCache::cleanupBeforePixmapDestruction(QPixmapData* pmd) #endif } -void QGLTextureCache::deleteIfEmpty() +QGLTextureCache *QGLTextureCache::instance() { - if (instance()->size() == 0) - delete instance(); + return qt_gl_texture_cache(); } // DDS format structure @@ -1960,7 +1948,6 @@ QGLContext::~QGLContext() { // remove any textures cached in this context QGLTextureCache::instance()->removeContextTextures(this); - QGLTextureCache::deleteIfEmpty(); // ### thread safety d_ptr->group->cleanupResources(this); diff --git a/src/opengl/qgl_p.h b/src/opengl/qgl_p.h index ed34f79..5e524a7 100644 --- a/src/opengl/qgl_p.h +++ b/src/opengl/qgl_p.h @@ -532,7 +532,6 @@ public: bool remove(QGLContext *ctx, GLuint textureId); void removeContextTextures(QGLContext *ctx); static QGLTextureCache *instance(); - static void deleteIfEmpty(); static void cleanupTexturesForCacheKey(qint64 cacheKey); static void cleanupTexturesForPixampData(QPixmapData* pixmap); static void cleanupBeforePixmapDestruction(QPixmapData* pixmap); -- cgit v0.12