1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
|
/*
SPDX-FileCopyrightText: 2014 Aleix Pol Gonzalez <aleixpol@blue-systems.com>
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#include "imagetexturescache.h"
#include <QSGTexture>
typedef QHash<qint64, QHash<QWindow *, QWeakPointer<QSGTexture>>> TexturesCache;
struct ImageTexturesCachePrivate {
TexturesCache cache;
};
ImageTexturesCache::ImageTexturesCache()
: d(new ImageTexturesCachePrivate)
{
}
ImageTexturesCache::~ImageTexturesCache()
{
}
QSharedPointer<QSGTexture> ImageTexturesCache::loadTexture(QQuickWindow *window, const QImage &image, QQuickWindow::CreateTextureOptions options)
{
qint64 id = image.cacheKey();
QSharedPointer<QSGTexture> texture = d->cache.value(id).value(window).toStrongRef();
if (!texture) {
auto cleanAndDelete = [this, window, id](QSGTexture *texture) {
QHash<QWindow *, QWeakPointer<QSGTexture>> &textures = (d->cache)[id];
textures.remove(window);
if (textures.isEmpty())
d->cache.remove(id);
delete texture;
};
texture = QSharedPointer<QSGTexture>(window->createTextureFromImage(image, options), cleanAndDelete);
(d->cache)[id][window] = texture.toWeakRef();
}
// if we have a cache in an atlas but our request cannot use an atlassed texture
// create a new texture and use that
// don't use removedFromAtlas() as that requires keeping a reference to the non atlased version
if (!(options & QQuickWindow::TextureCanUseAtlas) && texture->isAtlasTexture()) {
texture = QSharedPointer<QSGTexture>(window->createTextureFromImage(image, options));
}
return texture;
}
QSharedPointer<QSGTexture> ImageTexturesCache::loadTexture(QQuickWindow *window, const QImage &image)
{
return loadTexture(window, image, {});
}
|