diff options
Diffstat (limited to 'src/scenegraph/convenience/utilities.cpp')
-rw-r--r-- | src/scenegraph/convenience/utilities.cpp | 260 |
1 files changed, 130 insertions, 130 deletions
diff --git a/src/scenegraph/convenience/utilities.cpp b/src/scenegraph/convenience/utilities.cpp index ca6d2bb..ea36666 100644 --- a/src/scenegraph/convenience/utilities.cpp +++ b/src/scenegraph/convenience/utilities.cpp @@ -50,30 +50,30 @@ #include <QGLShaderProgram> -SubTexture2D::SubTexture2D(TextureAtlasInterface *atlas, const QRect &allocatedRect) - : m_allocated(allocatedRect) - , m_atlas(atlas) - , m_texture(atlas->texture()) -{ - qreal w = m_texture->size().width(); - qreal h = m_texture->size().height(); - m_source = QRectF(m_allocated.x() / w, m_allocated.y() / h, m_allocated.width() / w, m_allocated.height() / h); -} +//SubTexture2D::SubTexture2D(TextureAtlasInterface *atlas, const QRect &allocatedRect) +// : m_allocated(allocatedRect) +// , m_atlas(atlas) +// , m_texture(atlas->texture()) +//{ +// qreal w = m_texture->size().width(); +// qreal h = m_texture->size().height(); +// m_source = QRectF(m_allocated.x() / w, m_allocated.y() / h, m_allocated.width() / w, m_allocated.height() / h); +//} -SubTexture2D::SubTexture2D(const QGLTexture2DConstPtr &texture) - : m_source(0, 0, 1, 1) - , m_atlas(0) - , m_texture(texture) -{ -} +//SubTexture2D::SubTexture2D(const QGLTexture2DConstPtr &texture) +// : m_source(0, 0, 1, 1) +// , m_atlas(0) +// , m_texture(texture) +//{ +//} -SubTexture2D::~SubTexture2D() -{ - if (m_atlas) { - Q_ASSERT(!m_allocated.isNull()); - m_atlas->deallocate(m_allocated); - } -} +//SubTexture2D::~SubTexture2D() +//{ +// if (m_atlas) { +// Q_ASSERT(!m_allocated.isNull()); +// m_atlas->deallocate(m_allocated); +// } +//} void Utilities::setupRectGeometry(Geometry *geometry, const QRectF &rect, const QSize &textureSize, const QRectF &sourceRect) @@ -155,125 +155,125 @@ Geometry *Utilities::createTexturedRectGeometry(const QRectF &rect, const QSize return g; } -static struct TextureCache -{ - ~TextureCache() - { - QList<QWeakPointer<SubTexture2D> > t = textures.values(); - int count = 0; - for (int i = 0; i < t.size(); ++i) { - if (!t.at(i).isNull()) - ++count; - } - qDebug("Textures left in the texture cache: %i", count); - count = 0; - for (int i = 0; i < AtlasTypeCount; ++i) { - count += atlases[i].size(); - qDeleteAll(atlases[i]); - } - qDebug("Number of texture atlases used: %i", count); - } +//static struct TextureCache +//{ +// ~TextureCache() +// { +// QList<QWeakPointer<SubTexture2D> > t = textures.values(); +// int count = 0; +// for (int i = 0; i < t.size(); ++i) { +// if (!t.at(i).isNull()) +// ++count; +// } +// qDebug("Textures left in the texture cache: %i", count); +// count = 0; +// for (int i = 0; i < AtlasTypeCount; ++i) { +// count += atlases[i].size(); +// qDeleteAll(atlases[i]); +// } +// qDebug("Number of texture atlases used: %i", count); +// } - struct Key { - qint64 imageKey; - uint clampToEdge : 1; - bool operator == (const Key &other) const - { - return imageKey == other.imageKey && clampToEdge == other.clampToEdge; - } - }; +// struct Key { +// qint64 imageKey; +// uint clampToEdge : 1; +// bool operator == (const Key &other) const +// { +// return imageKey == other.imageKey && clampToEdge == other.clampToEdge; +// } +// }; - enum AtlasType - { - StaticAtlasType, - DynamicAtlasType, - AtlasTypeCount - }; +// enum AtlasType +// { +// StaticAtlasType, +// DynamicAtlasType, +// AtlasTypeCount +// }; - typedef QHash<Key, QWeakPointer<SubTexture2D> > Hash; +// typedef QHash<Key, QWeakPointer<SubTexture2D> > Hash; - static SubTexture2DPtr insert(const TextureCache::Key &key, const QImage &image, bool clampToEdge, bool dynamic); +// static SubTexture2DPtr insert(const TextureCache::Key &key, const QImage &image, bool clampToEdge, bool dynamic); - // ### Replace with a QCache or something to avoid growing to infinity and beyond. - Hash textures; - QVector<TextureAtlasInterface *> atlases[AtlasTypeCount]; -} textureCache; +// // ### Replace with a QCache or something to avoid growing to infinity and beyond. +// Hash textures; +// QVector<TextureAtlasInterface *> atlases[AtlasTypeCount]; +//} textureCache; -uint qHash(const TextureCache::Key &key) -{ - return qHash(key.imageKey) ^ uint(key.clampToEdge << 31); -} +//uint qHash(const TextureCache::Key &key) +//{ +// return qHash(key.imageKey) ^ uint(key.clampToEdge << 31); +//} -SubTexture2DPtr TextureCache::insert(const TextureCache::Key &key, const QImage &image, bool clampToEdge, bool dynamic) -{ - AtlasType type = (dynamic ? DynamicAtlasType : StaticAtlasType); - uint flags = (dynamic ? TextureAtlasInterface::DynamicFlag : 0); +//SubTexture2DPtr TextureCache::insert(const TextureCache::Key &key, const QImage &image, bool clampToEdge, bool dynamic) +//{ +// AtlasType type = (dynamic ? DynamicAtlasType : StaticAtlasType); +// uint flags = (dynamic ? TextureAtlasInterface::DynamicFlag : 0); - // Make sure that there is at least one texture atlas. - if (textureCache.atlases[type].isEmpty()) - textureCache.atlases[type].append(qt_adaptation_layer()->createTextureAtlas(flags)); +// // Make sure that there is at least one texture atlas. +// if (textureCache.atlases[type].isEmpty()) +// textureCache.atlases[type].append(qt_adaptation_layer()->createTextureAtlas(flags)); - // The texture atlas is used for many small images. If the image is somewhat big, create a separate texture for it. - QSize size = textureCache.atlases[type].first()->texture()->size(); - if (image.width() > size.width() / 4 || image.height() > size.height() / 4) { - QGLTexture2DPtr texture(new QGLTexture2D); - texture->setImage(image); - texture->setBindOptions(QGLContext::InternalBindOption); - QGL::TextureWrap wrap = clampToEdge ? QGL::ClampToEdge : QGL::Repeat; - texture->setVerticalWrap(wrap); - texture->setHorizontalWrap(wrap); - SubTexture2DPtr subtex(new SubTexture2D(texture.constCast<const QGLTexture2D>())); - textureCache.textures.insert(key, subtex.toWeakRef()); - return subtex; - } +// // The texture atlas is used for many small images. If the image is somewhat big, create a separate texture for it. +// QSize size = textureCache.atlases[type].first()->texture()->size(); +// if (image.width() > size.width() / 4 || image.height() > size.height() / 4) { +// QGLTexture2DPtr texture(new QGLTexture2D); +// texture->setImage(image); +// texture->setBindOptions(QGLContext::InternalBindOption); +// QGL::TextureWrap wrap = clampToEdge ? QGL::ClampToEdge : QGL::Repeat; +// texture->setVerticalWrap(wrap); +// texture->setHorizontalWrap(wrap); +// SubTexture2DPtr subtex(new SubTexture2D(texture.constCast<const QGLTexture2D>())); +// textureCache.textures.insert(key, subtex.toWeakRef()); +// return subtex; +// } - // Look for an atlas with room for the image. - TextureAtlasInterface *atlas; - QRect allocated; - int i = 0; - do { - atlas = textureCache.atlases[type].at(i); - allocated = atlas->allocate(image, clampToEdge); - ++i; - } while (i < textureCache.atlases[type].size() && allocated.isNull()); +// // Look for an atlas with room for the image. +// TextureAtlasInterface *atlas; +// QRect allocated; +// int i = 0; +// do { +// atlas = textureCache.atlases[type].at(i); +// allocated = atlas->allocate(image, clampToEdge); +// ++i; +// } while (i < textureCache.atlases[type].size() && allocated.isNull()); - // No room in the atlases, make a new atlas. - if (allocated.isNull()) { - atlas = qt_adaptation_layer()->createTextureAtlas(flags); - textureCache.atlases[type].append(atlas); - allocated = atlas->allocate(image, clampToEdge); - } +// // No room in the atlases, make a new atlas. +// if (allocated.isNull()) { +// atlas = qt_adaptation_layer()->createTextureAtlas(flags); +// textureCache.atlases[type].append(atlas); +// allocated = atlas->allocate(image, clampToEdge); +// } - // ### Need to add out-of-memory handling. - Q_ASSERT(!allocated.isNull()); - SubTexture2DPtr subtex(new SubTexture2D(atlas, allocated)); - textureCache.textures.insert(key, subtex.toWeakRef()); - return subtex; -} +// // ### Need to add out-of-memory handling. +// Q_ASSERT(!allocated.isNull()); +// SubTexture2DPtr subtex(new SubTexture2D(atlas, allocated)); +// textureCache.textures.insert(key, subtex.toWeakRef()); +// return subtex; +//} -SubTexture2DPtr Utilities::getTextureForImage(const QImage &image, bool clampToEdge, bool dynamic) -{ - TextureCache::Key key = {image.cacheKey(), clampToEdge}; - TextureCache::Hash::const_iterator it = textureCache.textures.find(key); - SubTexture2DPtr texture; - if (it != textureCache.textures.end()) { - texture = it.value().toStrongRef(); - if (!texture.isNull()) - return texture; - } - return textureCache.insert(key, image, clampToEdge, dynamic); -} +//SubTexture2DPtr Utilities::getTextureForImage(const QImage &image, bool clampToEdge, bool dynamic) +//{ +// TextureCache::Key key = {image.cacheKey(), clampToEdge}; +// TextureCache::Hash::const_iterator it = textureCache.textures.find(key); +// SubTexture2DPtr texture; +// if (it != textureCache.textures.end()) { +// texture = it.value().toStrongRef(); +// if (!texture.isNull()) +// return texture; +// } +// return textureCache.insert(key, image, clampToEdge, dynamic); +//} -SubTexture2DPtr Utilities::getTextureForPixmap(const QPixmap &pixmap, bool clampToEdge, bool dynamic) -{ - TextureCache::Key key = {pixmap.cacheKey(), clampToEdge}; - TextureCache::Hash::const_iterator it = textureCache.textures.find(key); - SubTexture2DPtr texture; - if (it != textureCache.textures.end()) { - texture = it.value().toStrongRef(); - if (!texture.isNull()) - return texture; - } - QImage image = pixmap.toImage(); - return textureCache.insert(key, image, clampToEdge, dynamic); -} +//SubTexture2DPtr Utilities::getTextureForPixmap(const QPixmap &pixmap, bool clampToEdge, bool dynamic) +//{ +// TextureCache::Key key = {pixmap.cacheKey(), clampToEdge}; +// TextureCache::Hash::const_iterator it = textureCache.textures.find(key); +// SubTexture2DPtr texture; +// if (it != textureCache.textures.end()) { +// texture = it.value().toStrongRef(); +// if (!texture.isNull()) +// return texture; +// } +// QImage image = pixmap.toImage(); +// return textureCache.insert(key, image, clampToEdge, dynamic); +//} |