Warning, file /frameworks/kdeclarative/src/quickaddons/imagetexturescache.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

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