diff options
author | Trond Kjernåsen <trond.kjernasen@nokia.com> | 2010-06-01 08:50:47 (GMT) |
---|---|---|
committer | Trond Kjernåsen <trond.kjernasen@nokia.com> | 2010-06-01 08:53:47 (GMT) |
commit | 805126ebc2aa9a683edefbc21d4ee7362ae26f55 (patch) | |
tree | a652e127a57f96e81ef7ebc74426a9af0ad684e7 /src/opengl | |
parent | 1b6d41deebdd8103e2d8abb53bd2bb1dd26bd0ae (diff) | |
download | Qt-805126ebc2aa9a683edefbc21d4ee7362ae26f55.zip Qt-805126ebc2aa9a683edefbc21d4ee7362ae26f55.tar.gz Qt-805126ebc2aa9a683edefbc21d4ee7362ae26f55.tar.bz2 |
Fixed a problem where QPixmaps where re-bound in non-sharing contexts.
If you have two non-sharing GL contexts and try to draw the same
QPixmap in both of them, they will wind up competing for a spot in the
texture cache. Make the context group a part of the cache key to avoid
that.
Task-number: QT-3462
Reviewed-by: Kim
Diffstat (limited to 'src/opengl')
-rw-r--r-- | src/opengl/qgl.cpp | 51 | ||||
-rw-r--r-- | src/opengl/qgl_p.h | 36 |
2 files changed, 67 insertions, 20 deletions
diff --git a/src/opengl/qgl.cpp b/src/opengl/qgl.cpp index 922a96c..c8502c2 100644 --- a/src/opengl/qgl.cpp +++ b/src/opengl/qgl.cpp @@ -91,6 +91,7 @@ #include "qcolormap.h" #include "qfile.h" #include "qlibrary.h" +#include <qmutex.h> QT_BEGIN_NAMESPACE @@ -1515,10 +1516,33 @@ bool operator!=(const QGLFormat& a, const QGLFormat& b) return !(a == b); } +struct QGLContextGroupList { + void append(QGLContextGroup *group) { + QMutexLocker locker(&m_mutex); + m_list.append(group); + } + + void remove(QGLContextGroup *group) { + QMutexLocker locker(&m_mutex); + m_list.removeOne(group); + } + + QList<QGLContextGroup *> m_list; + QMutex m_mutex; +}; + +Q_GLOBAL_STATIC(QGLContextGroupList, qt_context_groups) + /***************************************************************************** QGLContext implementation *****************************************************************************/ +QGLContextGroup::QGLContextGroup(const QGLContext *context) + : m_context(context), m_guards(0), m_refs(1) +{ + qt_context_groups()->append(this); +} + QGLContextGroup::~QGLContextGroup() { // Clear any remaining QGLSharedResourceGuard objects on the group. @@ -1528,6 +1552,7 @@ QGLContextGroup::~QGLContextGroup() guard->m_id = 0; guard = guard->m_next; } + qt_context_groups()->remove(this); } void QGLContextGroup::addGuard(QGLSharedResourceGuard *guard) @@ -1736,7 +1761,7 @@ void QGLTextureCache::insert(QGLContext* ctx, qint64 key, QGLTexture* texture, i QWriteLocker locker(&m_lock); if (m_cache.totalCost() + cost > m_cache.maxCost()) { // the cache is full - make an attempt to remove something - const QList<qint64> keys = m_cache.keys(); + const QList<QGLTextureCacheKey> keys = m_cache.keys(); int i = 0; while (i < m_cache.count() && (m_cache.totalCost() + cost > m_cache.maxCost())) { @@ -1746,13 +1771,26 @@ void QGLTextureCache::insert(QGLContext* ctx, qint64 key, QGLTexture* texture, i ++i; } } - m_cache.insert(key, texture, cost); + const QGLTextureCacheKey cacheKey = {key, QGLContextPrivate::contextGroup(ctx)}; + m_cache.insert(cacheKey, texture, cost); +} + +void QGLTextureCache::remove(qint64 key) +{ + QWriteLocker locker(&m_lock); + QMutexLocker groupLocker(&qt_context_groups()->m_mutex); + QList<QGLContextGroup *>::const_iterator it = qt_context_groups()->m_list.constBegin(); + while (it != qt_context_groups()->m_list.constEnd()) { + const QGLTextureCacheKey cacheKey = {key, *it}; + m_cache.remove(cacheKey); + ++it; + } } bool QGLTextureCache::remove(QGLContext* ctx, GLuint textureId) { QWriteLocker locker(&m_lock); - QList<qint64> keys = m_cache.keys(); + QList<QGLTextureCacheKey> keys = m_cache.keys(); for (int i = 0; i < keys.size(); ++i) { QGLTexture *tex = m_cache.object(keys.at(i)); if (tex->id == textureId && tex->context == ctx) { @@ -1767,9 +1805,9 @@ bool QGLTextureCache::remove(QGLContext* ctx, GLuint textureId) void QGLTextureCache::removeContextTextures(QGLContext* ctx) { QWriteLocker locker(&m_lock); - QList<qint64> keys = m_cache.keys(); + QList<QGLTextureCacheKey> keys = m_cache.keys(); for (int i = 0; i < keys.size(); ++i) { - const qint64 &key = keys.at(i); + const QGLTextureCacheKey &key = keys.at(i); if (m_cache.object(key)->context == ctx) m_cache.remove(key); } @@ -1782,7 +1820,6 @@ void QGLTextureCache::removeContextTextures(QGLContext* ctx) void QGLTextureCache::cleanupTexturesForCacheKey(qint64 cacheKey) { qt_gl_texture_cache()->remove(cacheKey); - Q_ASSERT(qt_gl_texture_cache()->getTexture(cacheKey) == 0); } @@ -2425,7 +2462,7 @@ QGLTexture* QGLContextPrivate::bindTexture(const QImage &image, GLenum target, G QGLTexture *QGLContextPrivate::textureCacheLookup(const qint64 key, GLenum target) { Q_Q(QGLContext); - QGLTexture *texture = QGLTextureCache::instance()->getTexture(key); + QGLTexture *texture = QGLTextureCache::instance()->getTexture(q, key); if (texture && texture->target == target && (texture->context == q || QGLContext::areSharing(q, texture->context))) { diff --git a/src/opengl/qgl_p.h b/src/opengl/qgl_p.h index d92f963..16c225f 100644 --- a/src/opengl/qgl_p.h +++ b/src/opengl/qgl_p.h @@ -230,7 +230,7 @@ public: static void addShare(const QGLContext *context, const QGLContext *share); static void removeShare(const QGLContext *context); private: - QGLContextGroup(const QGLContext *context) : m_context(context), m_guards(0), m_refs(1) { } + QGLContextGroup(const QGLContext *context); QGLExtensionFuncs m_extensionFuncs; const QGLContext *m_context; // context group's representative @@ -522,17 +522,33 @@ public: QSize bindCompressedTexturePVR(const char *buf, int len); }; +struct QGLTextureCacheKey { + qint64 key; + QGLContextGroup *group; +}; + +inline bool operator==(const QGLTextureCacheKey &a, const QGLTextureCacheKey &b) +{ + return a.key == b.key && a.group == b.group; +} + +inline uint qHash(const QGLTextureCacheKey &key) +{ + return qHash(key.key) ^ qHash(key.group); +} + + class Q_AUTOTEST_EXPORT QGLTextureCache { public: QGLTextureCache(); ~QGLTextureCache(); void insert(QGLContext *ctx, qint64 key, QGLTexture *texture, int cost); - inline void remove(quint64 key); + void remove(qint64 key); inline int size(); inline void setMaxCost(int newMax); inline int maxCost(); - inline QGLTexture* getTexture(quint64 key); + inline QGLTexture* getTexture(QGLContext *ctx, qint64 key); bool remove(QGLContext *ctx, GLuint textureId); void removeContextTextures(QGLContext *ctx); @@ -542,7 +558,7 @@ public: static void cleanupBeforePixmapDestruction(QPixmapData* pixmap); private: - QCache<qint64, QGLTexture> m_cache; + QCache<QGLTextureCacheKey, QGLTexture> m_cache; QReadWriteLock m_lock; }; @@ -563,19 +579,13 @@ int QGLTextureCache::maxCost() return m_cache.maxCost(); } -QGLTexture* QGLTextureCache::getTexture(quint64 key) +QGLTexture* QGLTextureCache::getTexture(QGLContext *ctx, qint64 key) { QReadLocker locker(&m_lock); - return m_cache.object(key); + const QGLTextureCacheKey cacheKey = {key, QGLContextPrivate::contextGroup(ctx)}; + return m_cache.object(cacheKey); } -void QGLTextureCache::remove(quint64 key) -{ - QWriteLocker locker(&m_lock); - m_cache.remove(key); -} - - extern Q_OPENGL_EXPORT QPaintEngine* qt_qgl_paint_engine(); bool qt_gl_preferGL2Engine(); |