| 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 |
|
| 10 |
typedef QHash<qint64, QHash<QWindow *, QWeakPointer<QSGTexture>>> TexturesCache; |
| 11 |
|
| 12 |
struct ImageTexturesCachePrivate { |
| 13 |
TexturesCache cache; |
| 14 |
}; |
| 15 |
|
| 16 |
ImageTexturesCache::ImageTexturesCache() |
| 17 |
: d(new ImageTexturesCachePrivate) |
| 18 |
{ |
| 19 |
} |
| 20 |
|
| 21 |
ImageTexturesCache::~ImageTexturesCache() |
| 22 |
{ |
| 23 |
} |
| 24 |
|
| 25 |
QSharedPointer<QSGTexture> ImageTexturesCache::loadTexture(QQuickWindow *window, const QImage &image, QQuickWindow::CreateTextureOptions options) |
| 26 |
{ |
| 27 |
qint64 id = image.cacheKey(); |
| 28 |
QSharedPointer<QSGTexture> texture = d->cache.value(id).value(window).toStrongRef(); |
| 29 |
|
| 30 |
if (!texture) { |
| 31 |
auto cleanAndDelete = [this, window, id](QSGTexture *texture) { |
| 32 |
QHash<QWindow *, QWeakPointer<QSGTexture>> &textures = (d->cache)[id]; |
| 33 |
textures.remove(window); |
| 34 |
if (textures.isEmpty()) |
| 35 |
d->cache.remove(id); |
| 36 |
delete texture; |
| 37 |
}; |
| 38 |
texture = QSharedPointer<QSGTexture>(window->createTextureFromImage(image, options), cleanAndDelete); |
| 39 |
(d->cache)[id][window] = texture.toWeakRef(); |
| 40 |
} |
| 41 |
|
| 42 |
// if we have a cache in an atlas but our request cannot use an atlassed texture |
| 43 |
// create a new texture and use that |
| 44 |
// don't use removedFromAtlas() as that requires keeping a reference to the non atlased version |
| 45 |
if (!(options & QQuickWindow::TextureCanUseAtlas) && texture->isAtlasTexture()) { |
| 46 |
texture = QSharedPointer<QSGTexture>(window->createTextureFromImage(image, options)); |
| 47 |
} |
| 48 |
|
| 49 |
return texture; |
| 50 |
} |
| 51 |
|
| 52 |
QSharedPointer<QSGTexture> ImageTexturesCache::loadTexture(QQuickWindow *window, const QImage &image) |
| 53 |
{ |
| 54 |
return loadTexture(window, image, {}); |
| 55 |
} |