diff options
-rw-r--r-- | src/scenegraph/coreapi/qsgtexturemanager.cpp | 33 |
1 files changed, 32 insertions, 1 deletions
diff --git a/src/scenegraph/coreapi/qsgtexturemanager.cpp b/src/scenegraph/coreapi/qsgtexturemanager.cpp index d8ce090..89cbe19 100644 --- a/src/scenegraph/coreapi/qsgtexturemanager.cpp +++ b/src/scenegraph/coreapi/qsgtexturemanager.cpp @@ -44,6 +44,7 @@ #include <QImage> #include <qgl.h> +#include <qhash.h> QSGTexture::QSGTexture() : m_status(Null) @@ -74,7 +75,30 @@ void QSGTexture::setStatus(Status s) +struct QSGTextureCacheKey { + quint64 cacheKey; + + bool operator==(const QSGTextureCacheKey &other) const { + return other.cacheKey == cacheKey; + } +}; + + +uint qHash(const QSGTextureCacheKey &key) +{ + return (key.cacheKey >> 32) ^ uint(key.cacheKey); +} + +class QSGTextureManagerPrivate +{ +public: + QHash<QSGTextureCacheKey, QSGTexture *> cache; +}; + + + QSGTextureManager::QSGTextureManager() + : d(new QSGTextureManagerPrivate) { } @@ -97,6 +121,11 @@ QImage QSGTextureManager::swizzleBGGRAToRGBA(const QImage &image) QSGTextureRef QSGTextureManager::upload(const QImage &image) { + QSGTextureCacheKey key = { image.cacheKey() }; + QSGTexture *texture = d->cache.value(key); + if (texture) + return QSGTextureRef(texture); + GLuint id; glGenTextures(1, &id); glBindTexture(GL_TEXTURE_2D, id); @@ -108,12 +137,14 @@ QSGTextureRef QSGTextureManager::upload(const QImage &image) glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image.width(), image.height(), 0, GL_BGRA, GL_UNSIGNED_BYTE, image.constBits()); #endif - QSGTexture *texture = new QSGTexture; + texture = new QSGTexture; texture->setTextureId(id); texture->setTextureSize(image.size()); texture->setAlphaChannel(image.hasAlphaChannel()); texture->setStatus(QSGTexture::Ready); + d->cache.insert(key, texture); + QSGTextureRef ref(texture); return ref; } |