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

source code of ksvg/src/declarativeimports/imagetexturescache.cpp