Cache the textures created for the fast path

Create a cache that has pointers to all the textures that we've generated,
so in case we have one already created, we can re-use it.

REVIEW: 119425
This commit is contained in:
Aleix Pol 2014-07-25 12:41:53 +02:00
parent f6e4cb790e
commit bc425f1809
4 changed files with 23 additions and 11 deletions

View File

@ -37,6 +37,21 @@
namespace Plasma
{
typedef QHash<qint64, QWeakPointer<QSGTexture> > TexturesCache;
Q_GLOBAL_STATIC(TexturesCache, s_cache)
QSharedPointer<QSGTexture> loadTexture(QQuickWindow *window, const QImage &image)
{
qint64 id = image.cacheKey();
QSharedPointer<QSGTexture> texture = s_cache->value(id).toStrongRef();
if (!texture) {
auto cleanAndDelete = [id](QSGTexture* texture) { s_cache->remove(id); delete texture; };
texture = QSharedPointer<QSGTexture>(window->createTextureFromImage(image), cleanAndDelete);
s_cache->insert(id, texture.toWeakRef());
}
return texture;
}
class FrameNode : public QSGNode
{
public:
@ -118,10 +133,7 @@ public:
void updateTexture(const QSize &size, const QString &elementId)
{
QImage image = m_frameSvg->frameSvg()->image(size, elementId);
QSGTexture *texture = m_frameSvg->window()->createTextureFromImage(image);
setTexture(texture);
setTexture(loadTexture(m_frameSvg->window(), m_frameSvg->frameSvg()->image(size, elementId)));
}
void reposition(const QRect& frameGeometry, QSize& fullSize)
@ -475,7 +487,7 @@ QSGNode *FrameSvgItem::updatePaintNode(QSGNode *oldNode, QQuickItem::UpdatePaint
QImage image = m_frameSvg->framePixmap().toImage();
QSGTexture *texture = window()->createTextureFromImage(image);
texture->setFiltering(QSGTexture::Nearest);
textureNode->setTexture(texture);
textureNode->setTexture(QSharedPointer<QSGTexture>(texture));
textureNode->setRect(0, 0, width(), height());
m_textureChanged = false;

View File

@ -245,7 +245,7 @@ QSGNode* IconItem::updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *update
if (!textureNode || m_textureChanged) {
delete oldNode;
textureNode = new Plasma::SVGTextureNode;
textureNode->setTexture(window()->createTextureFromImage(m_iconPixmap.toImage()));
textureNode->setTexture(QSharedPointer<QSGTexture>(window()->createTextureFromImage(m_iconPixmap.toImage())));
m_sizeChanged = true;
m_textureChanged = false;
}

View File

@ -159,7 +159,7 @@ QSGNode *SvgItem::updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *updateP
m_svg.data()->setContainsMultipleImages(!m_elementID.isEmpty());
const QImage image = m_svg.data()->image(QSize(width(), height()), m_elementID);
QSGTexture *texture = window()->createTextureFromImage(image, QQuickWindow::TextureCanUseAtlas);
QSharedPointer<QSGTexture> texture(window()->createTextureFromImage(image, QQuickWindow::TextureCanUseAtlas));
if (m_smooth) {
texture->setFiltering(QSGTexture::Linear);
}

View File

@ -38,13 +38,13 @@ public:
* Set the current texture
* the object takes ownership of the texture
*/
void setTexture(QSGTexture *texture)
void setTexture(QSharedPointer<QSGTexture> texture)
{
m_texture.reset(texture);
QSGSimpleTextureNode::setTexture(texture);
m_texture = texture;
QSGSimpleTextureNode::setTexture(texture.data());
}
private:
QScopedPointer<QSGTexture> m_texture;
QSharedPointer<QSGTexture> m_texture;
};
}