1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtGui module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#include "qopengltexturecache_p.h"
41#include "qopengltextureuploader_p.h"
42#include <qmath.h>
43#include <qopenglfunctions.h>
44#include <private/qimagepixmapcleanuphooks_p.h>
45#include <qpa/qplatformpixmap.h>
46
47QT_BEGIN_NAMESPACE
48
49class QOpenGLTextureCacheWrapper
50{
51public:
52 QOpenGLTextureCacheWrapper()
53 {
54 QImagePixmapCleanupHooks::instance()->addPlatformPixmapModificationHook(cleanupTexturesForPixmapData);
55 QImagePixmapCleanupHooks::instance()->addPlatformPixmapDestructionHook(cleanupTexturesForPixmapData);
56 QImagePixmapCleanupHooks::instance()->addImageHook(cleanupTexturesForCacheKey);
57 }
58
59 ~QOpenGLTextureCacheWrapper()
60 {
61 QImagePixmapCleanupHooks::instance()->removePlatformPixmapModificationHook(cleanupTexturesForPixmapData);
62 QImagePixmapCleanupHooks::instance()->removePlatformPixmapDestructionHook(cleanupTexturesForPixmapData);
63 QImagePixmapCleanupHooks::instance()->removeImageHook(cleanupTexturesForCacheKey);
64 }
65
66 QOpenGLTextureCache *cacheForContext(QOpenGLContext *context) {
67 QMutexLocker lock(&m_mutex);
68 return m_resource.value<QOpenGLTextureCache>(context);
69 }
70
71 static void cleanupTexturesForCacheKey(qint64 key);
72 static void cleanupTexturesForPixmapData(QPlatformPixmap *pmd);
73
74private:
75 QOpenGLMultiGroupSharedResource m_resource;
76 QMutex m_mutex;
77};
78
79Q_GLOBAL_STATIC(QOpenGLTextureCacheWrapper, qt_texture_caches)
80
81QOpenGLTextureCache *QOpenGLTextureCache::cacheForContext(QOpenGLContext *context)
82{
83 return qt_texture_caches()->cacheForContext(context);
84}
85
86void QOpenGLTextureCacheWrapper::cleanupTexturesForCacheKey(qint64 key)
87{
88 QList<QOpenGLSharedResource *> resources = qt_texture_caches()->m_resource.resources();
89 for (QList<QOpenGLSharedResource *>::iterator it = resources.begin(); it != resources.end(); ++it)
90 static_cast<QOpenGLTextureCache *>(*it)->invalidate(key);
91}
92
93void QOpenGLTextureCacheWrapper::cleanupTexturesForPixmapData(QPlatformPixmap *pmd)
94{
95 cleanupTexturesForCacheKey(pmd->cacheKey());
96}
97
98QOpenGLTextureCache::QOpenGLTextureCache(QOpenGLContext *ctx)
99 : QOpenGLSharedResource(ctx->shareGroup())
100 , m_cache(256 * 1024) // 256 MB cache
101{
102}
103
104QOpenGLTextureCache::~QOpenGLTextureCache()
105{
106}
107
108GLuint QOpenGLTextureCache::bindTexture(QOpenGLContext *context, const QPixmap &pixmap, QOpenGLTextureUploader::BindOptions options)
109{
110 if (pixmap.isNull())
111 return 0;
112 QMutexLocker locker(&m_mutex);
113 qint64 key = pixmap.cacheKey();
114
115 // A QPainter is active on the image - take the safe route and replace the texture.
116 if (!pixmap.paintingActive()) {
117 QOpenGLCachedTexture *entry = m_cache.object(key);
118 if (entry && entry->options() == options) {
119 context->functions()->glBindTexture(GL_TEXTURE_2D, entry->id());
120 return entry->id();
121 }
122 }
123
124 GLuint id = bindTexture(context, key, pixmap.toImage(), options);
125 if (id > 0)
126 QImagePixmapCleanupHooks::enableCleanupHooks(pixmap);
127
128 return id;
129}
130
131GLuint QOpenGLTextureCache::bindTexture(QOpenGLContext *context, const QImage &image, QOpenGLTextureUploader::BindOptions options)
132{
133 if (image.isNull())
134 return 0;
135 QMutexLocker locker(&m_mutex);
136 qint64 key = image.cacheKey();
137
138 // A QPainter is active on the image - take the safe route and replace the texture.
139 if (!image.paintingActive()) {
140 QOpenGLCachedTexture *entry = m_cache.object(key);
141 if (entry && entry->options() == options) {
142 context->functions()->glBindTexture(GL_TEXTURE_2D, entry->id());
143 return entry->id();
144 }
145 }
146
147 QImage img = image;
148 if (!context->functions()->hasOpenGLFeature(QOpenGLFunctions::NPOTTextures))
149 options |= QOpenGLTextureUploader::PowerOfTwoBindOption;
150
151 GLuint id = bindTexture(context, key, img, options);
152 if (id > 0)
153 QImagePixmapCleanupHooks::enableCleanupHooks(image);
154
155 return id;
156}
157
158GLuint QOpenGLTextureCache::bindTexture(QOpenGLContext *context, qint64 key, const QImage &image, QOpenGLTextureUploader::BindOptions options)
159{
160 GLuint id;
161 QOpenGLFunctions *funcs = context->functions();
162 funcs->glGenTextures(1, &id);
163 funcs->glBindTexture(GL_TEXTURE_2D, id);
164
165 int cost = QOpenGLTextureUploader::textureImage(GL_TEXTURE_2D, image, options);
166
167 m_cache.insert(key, new QOpenGLCachedTexture(id, options, context), cost / 1024);
168
169 return id;
170}
171
172void QOpenGLTextureCache::invalidate(qint64 key)
173{
174 QMutexLocker locker(&m_mutex);
175 m_cache.remove(key);
176}
177
178void QOpenGLTextureCache::invalidateResource()
179{
180 m_cache.clear();
181}
182
183void QOpenGLTextureCache::freeResource(QOpenGLContext *)
184{
185 Q_ASSERT(false); // the texture cache lives until the context group disappears
186}
187
188static void freeTexture(QOpenGLFunctions *funcs, GLuint id)
189{
190 funcs->glDeleteTextures(1, &id);
191}
192
193QOpenGLCachedTexture::QOpenGLCachedTexture(GLuint id, QOpenGLTextureUploader::BindOptions options, QOpenGLContext *context) : m_options(options)
194{
195 m_resource = new QOpenGLSharedResourceGuard(context, id, freeTexture);
196}
197
198QT_END_NAMESPACE
199