KSvg

imagetexturescache.cpp
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
11
12class ImageTexturesCachePrivate
13{
14public:
15 TexturesCache cache;
16};
17
18ImageTexturesCache::ImageTexturesCache()
19 : d(new ImageTexturesCachePrivate)
20{
21}
22
23ImageTexturesCache::~ImageTexturesCache()
24{
25}
26
28{
29 qint64 id = image.cacheKey();
30 QSharedPointer<QSGTexture> texture = d->cache.value(id).value(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(window);
36 if (textures.isEmpty()) {
37 d->cache.remove(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
56{
57 return loadTexture(window, image, QQuickWindow::CreateTextureOptions());
58}
QSharedPointer< QSGTexture > loadTexture(QQuickWindow *window, const QImage &image, QQuickWindow::CreateTextureOptions options)
bool isEmpty() const const
bool remove(const Key &key)
qint64 cacheKey() const const
QWeakPointer< T > toWeakRef() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 22 2024 12:07:57 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.