KDeclarative

imagetexturescache.cpp
1 /*
2  SPDX-FileCopyrightText: 2014 Aleix Pol Gonzalez <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #include "imagetexturescache.h"
8 #include <QSGTexture>
9 
11 
12 class ImageTexturesCachePrivate
13 {
14 public:
15  TexturesCache cache;
16 };
17 
18 ImageTexturesCache::ImageTexturesCache()
19  : d(new ImageTexturesCachePrivate)
20 {
21 }
22 
23 ImageTexturesCache::~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  delete texture;
39  };
40  texture = QSharedPointer<QSGTexture>(window->createTextureFromImage(image, options), cleanAndDelete);
41  (d->cache)[id][window] = texture.toWeakRef();
42  }
43 
44  //if we have a cache in an atlas but our request cannot use an atlassed texture
45  //create a new texture and use that
46  //don't use removedFromAtlas() as that requires keeping a reference to the non atlased version
47  if (!(options & QQuickWindow::TextureCanUseAtlas) && texture->isAtlasTexture()) {
48  texture = QSharedPointer<QSGTexture>(window->createTextureFromImage(image, options));
49  }
50 
51  return texture;
52 }
53 
55 {
56  return loadTexture(window, image, QQuickWindow::CreateTextureOptions());
57 }
QSGTexture * createTextureFromImage(const QImage &image) const const
qint64 cacheKey() const const
typedef CreateTextureOptions
int remove(const Key &key)
QWeakPointer< T > toWeakRef() const const
bool isEmpty() const const
QSharedPointer< QSGTexture > loadTexture(QQuickWindow *window, const QImage &image, QQuickWindow::CreateTextureOptions options)
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Mon Jan 25 2021 22:44:28 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.