| 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 "texturecache.h" |
| 10 | |
| 11 | #include <QImage> |
| 12 | |
| 13 | std::shared_ptr<QSGTexture> TextureCache::loadTexture(QQuickWindow *window, const QImage &image, QQuickWindow::CreateTextureOptions options) |
| 14 | { |
| 15 | if (image.isNull()) { |
| 16 | return nullptr; |
| 17 | } |
| 18 | |
| 19 | auto id = qMakePair(value1: image.cacheKey(), value2&: window); |
| 20 | std::shared_ptr<QSGTexture> texture = s_cache.value(key: id).lock(); |
| 21 | |
| 22 | if (!texture) { |
| 23 | auto cleanAndDelete = [id](QSGTexture *texture) { |
| 24 | s_cache.remove(key: id); |
| 25 | delete texture; |
| 26 | }; |
| 27 | texture = std::shared_ptr<QSGTexture>(window->createTextureFromImage(image, options), cleanAndDelete); |
| 28 | s_cache[id] = texture; |
| 29 | } |
| 30 | |
| 31 | // if we have a cache in an atlas but our request cannot use an atlassed texture |
| 32 | // create a new texture and use that |
| 33 | // don't use removedFromAtlas() as that requires keeping a reference to the non atlased version |
| 34 | if (!(options & QQuickWindow::TextureCanUseAtlas) && texture->isAtlasTexture()) { |
| 35 | texture = std::shared_ptr<QSGTexture>(window->createTextureFromImage(image, options)); |
| 36 | } |
| 37 | |
| 38 | return texture; |
| 39 | } |
| 40 | |
| 41 | std::shared_ptr<QSGTexture> TextureCache::loadTexture(QQuickWindow *window, const QImage &image) |
| 42 | { |
| 43 | return loadTexture(window, image, options: {}); |
| 44 | } |
| 45 | |