File indexing completed on 2024-04-28 03:55:59

0001 /*
0002  *  SPDX-FileCopyrightText: 2011 Marco Martin <mart@kde.org>
0003  *  SPDX-FileCopyrightText: 2014 Aleix Pol Gonzalez <aleixpol@blue-systems.com>
0004  *  SPDX-FileCopyrightText: 2020 Carson Black <uhhadd@gmail.com>
0005  *
0006  *  SPDX-License-Identifier: LGPL-2.0-or-later
0007  */
0008 
0009 #include "managedtexturenode.h"
0010 
0011 ManagedTextureNode::ManagedTextureNode()
0012 {
0013 }
0014 
0015 void ManagedTextureNode::setTexture(std::shared_ptr<QSGTexture> texture)
0016 {
0017     m_texture = texture;
0018     QSGSimpleTextureNode::setTexture(texture.get());
0019 }
0020 
0021 ImageTexturesCache::ImageTexturesCache()
0022     : d(new ImageTexturesCachePrivate)
0023 {
0024 }
0025 
0026 ImageTexturesCache::~ImageTexturesCache()
0027 {
0028 }
0029 
0030 std::shared_ptr<QSGTexture> ImageTexturesCache::loadTexture(QQuickWindow *window, const QImage &image, QQuickWindow::CreateTextureOptions options)
0031 {
0032     qint64 id = image.cacheKey();
0033     std::shared_ptr<QSGTexture> texture = d->cache.value(id).value(window).lock();
0034 
0035     if (!texture) {
0036         auto cleanAndDelete = [this, window, id](QSGTexture *texture) {
0037             QHash<QWindow *, std::weak_ptr<QSGTexture>> &textures = (d->cache)[id];
0038             textures.remove(window);
0039             if (textures.isEmpty()) {
0040                 d->cache.remove(id);
0041             }
0042             delete texture;
0043         };
0044         texture = std::shared_ptr<QSGTexture>(window->createTextureFromImage(image, options), cleanAndDelete);
0045         (d->cache)[id][window] = texture;
0046     }
0047 
0048     // if we have a cache in an atlas but our request cannot use an atlassed texture
0049     // create a new texture and use that
0050     // don't use removedFromAtlas() as that requires keeping a reference to the non atlased version
0051     if (!(options & QQuickWindow::TextureCanUseAtlas) && texture->isAtlasTexture()) {
0052         texture = std::shared_ptr<QSGTexture>(window->createTextureFromImage(image, options));
0053     }
0054 
0055     return texture;
0056 }
0057 
0058 std::shared_ptr<QSGTexture> ImageTexturesCache::loadTexture(QQuickWindow *window, const QImage &image)
0059 {
0060     return loadTexture(window, image, {});
0061 }