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

source code of kirigami/src/scenegraph/managedtexturenode.cpp