Kirigami2

managedtexturenode.cpp
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 
11 ManagedTextureNode::ManagedTextureNode()
12 {
13 }
14 
15 void ManagedTextureNode::setTexture(std::shared_ptr<QSGTexture> texture)
16 {
17  m_texture = texture;
18  QSGSimpleTextureNode::setTexture(texture.get());
19 }
20 
21 ImageTexturesCache::ImageTexturesCache()
22  : d(new ImageTexturesCachePrivate)
23 {
24 }
25 
26 ImageTexturesCache::~ImageTexturesCache()
27 {
28 }
29 
30 std::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(id).value(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(window);
39  if (textures.isEmpty()) {
40  d->cache.remove(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 
58 std::shared_ptr<QSGTexture> ImageTexturesCache::loadTexture(QQuickWindow *window, const QImage &image)
59 {
60  return loadTexture(window, image, {});
61 }
typedef CreateTextureOptions
QWidget * window(QObject *job)
qint64 cacheKey() const const
int remove(const Key &key)
bool isEmpty() const const
QSharedPointer< QSGTexture > loadTexture(QQuickWindow *window, const QImage &image, QQuickWindow::CreateTextureOptions options)
void setTexture(QSGTexture *texture)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Thu Feb 15 2024 03:59:21 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.