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 
47 #include <qtgui_tracepoints_p.h>
48 
49 QT_BEGIN_NAMESPACE
50 
51 class QOpenGLTextureCacheWrapper
52 {
53 public:
QOpenGLTextureCacheWrapper()54     QOpenGLTextureCacheWrapper()
55     {
56         QImagePixmapCleanupHooks::instance()->addPlatformPixmapModificationHook(cleanupTexturesForPixmapData);
57         QImagePixmapCleanupHooks::instance()->addPlatformPixmapDestructionHook(cleanupTexturesForPixmapData);
58         QImagePixmapCleanupHooks::instance()->addImageHook(cleanupTexturesForCacheKey);
59     }
60 
~QOpenGLTextureCacheWrapper()61     ~QOpenGLTextureCacheWrapper()
62     {
63         QImagePixmapCleanupHooks::instance()->removePlatformPixmapModificationHook(cleanupTexturesForPixmapData);
64         QImagePixmapCleanupHooks::instance()->removePlatformPixmapDestructionHook(cleanupTexturesForPixmapData);
65         QImagePixmapCleanupHooks::instance()->removeImageHook(cleanupTexturesForCacheKey);
66     }
67 
cacheForContext(QOpenGLContext * context)68     QOpenGLTextureCache *cacheForContext(QOpenGLContext *context) {
69         QMutexLocker lock(&m_mutex);
70         return m_resource.value<QOpenGLTextureCache>(context);
71     }
72 
73     static void cleanupTexturesForCacheKey(qint64 key);
74     static void cleanupTexturesForPixmapData(QPlatformPixmap *pmd);
75 
76 private:
77     QOpenGLMultiGroupSharedResource m_resource;
78     QMutex m_mutex;
79 };
80 
Q_GLOBAL_STATIC(QOpenGLTextureCacheWrapper,qt_texture_caches)81 Q_GLOBAL_STATIC(QOpenGLTextureCacheWrapper, qt_texture_caches)
82 
83 QOpenGLTextureCache *QOpenGLTextureCache::cacheForContext(QOpenGLContext *context)
84 {
85     return qt_texture_caches()->cacheForContext(context);
86 }
87 
cleanupTexturesForCacheKey(qint64 key)88 void QOpenGLTextureCacheWrapper::cleanupTexturesForCacheKey(qint64 key)
89 {
90     QList<QOpenGLSharedResource *> resources = qt_texture_caches()->m_resource.resources();
91     for (QList<QOpenGLSharedResource *>::iterator it = resources.begin(); it != resources.end(); ++it)
92         static_cast<QOpenGLTextureCache *>(*it)->invalidate(key);
93 }
94 
cleanupTexturesForPixmapData(QPlatformPixmap * pmd)95 void QOpenGLTextureCacheWrapper::cleanupTexturesForPixmapData(QPlatformPixmap *pmd)
96 {
97     cleanupTexturesForCacheKey(pmd->cacheKey());
98 }
99 
QOpenGLTextureCache(QOpenGLContext * ctx)100 QOpenGLTextureCache::QOpenGLTextureCache(QOpenGLContext *ctx)
101     : QOpenGLSharedResource(ctx->shareGroup())
102     , m_cache(256 * 1024) // 256 MB cache
103 {
104 }
105 
~QOpenGLTextureCache()106 QOpenGLTextureCache::~QOpenGLTextureCache()
107 {
108 }
109 
bindTexture(QOpenGLContext * context,const QPixmap & pixmap,QOpenGLTextureUploader::BindOptions options)110 GLuint QOpenGLTextureCache::bindTexture(QOpenGLContext *context, const QPixmap &pixmap, QOpenGLTextureUploader::BindOptions options)
111 {
112     if (pixmap.isNull())
113         return 0;
114     QMutexLocker locker(&m_mutex);
115     qint64 key = pixmap.cacheKey();
116 
117     // A QPainter is active on the image - take the safe route and replace the texture.
118     if (!pixmap.paintingActive()) {
119         QOpenGLCachedTexture *entry = m_cache.object(key);
120         if (entry && entry->options() == options) {
121             context->functions()->glBindTexture(GL_TEXTURE_2D, entry->id());
122             return entry->id();
123         }
124     }
125 
126     GLuint id = bindTexture(context, key, pixmap.toImage(), options);
127     if (id > 0)
128         QImagePixmapCleanupHooks::enableCleanupHooks(pixmap);
129 
130     return id;
131 }
132 
bindTexture(QOpenGLContext * context,const QImage & image,QOpenGLTextureUploader::BindOptions options)133 GLuint QOpenGLTextureCache::bindTexture(QOpenGLContext *context, const QImage &image, QOpenGLTextureUploader::BindOptions options)
134 {
135     if (image.isNull())
136         return 0;
137     QMutexLocker locker(&m_mutex);
138     qint64 key = image.cacheKey();
139 
140     // A QPainter is active on the image - take the safe route and replace the texture.
141     if (!image.paintingActive()) {
142         QOpenGLCachedTexture *entry = m_cache.object(key);
143         if (entry && entry->options() == options) {
144             context->functions()->glBindTexture(GL_TEXTURE_2D, entry->id());
145             return entry->id();
146         }
147     }
148 
149     QImage img = image;
150     if (!context->functions()->hasOpenGLFeature(QOpenGLFunctions::NPOTTextures))
151         options |= QOpenGLTextureUploader::PowerOfTwoBindOption;
152 
153     GLuint id = bindTexture(context, key, img, options);
154     if (id > 0)
155         QImagePixmapCleanupHooks::enableCleanupHooks(image);
156 
157     return id;
158 }
159 
bindTexture(QOpenGLContext * context,qint64 key,const QImage & image,QOpenGLTextureUploader::BindOptions options)160 GLuint QOpenGLTextureCache::bindTexture(QOpenGLContext *context, qint64 key, const QImage &image, QOpenGLTextureUploader::BindOptions options)
161 {
162     Q_TRACE_SCOPE(QOpenGLTextureCache_bindTexture, context, key, image, options);
163 
164     GLuint id;
165     QOpenGLFunctions *funcs = context->functions();
166     funcs->glGenTextures(1, &id);
167     funcs->glBindTexture(GL_TEXTURE_2D, id);
168 
169     int cost = QOpenGLTextureUploader::textureImage(GL_TEXTURE_2D, image, options);
170 
171     m_cache.insert(key, new QOpenGLCachedTexture(id, options, context), cost / 1024);
172 
173     return id;
174 }
175 
invalidate(qint64 key)176 void QOpenGLTextureCache::invalidate(qint64 key)
177 {
178     QMutexLocker locker(&m_mutex);
179     m_cache.remove(key);
180 }
181 
invalidateResource()182 void QOpenGLTextureCache::invalidateResource()
183 {
184     m_cache.clear();
185 }
186 
freeResource(QOpenGLContext *)187 void QOpenGLTextureCache::freeResource(QOpenGLContext *)
188 {
189     Q_ASSERT(false); // the texture cache lives until the context group disappears
190 }
191 
freeTexture(QOpenGLFunctions * funcs,GLuint id)192 static void freeTexture(QOpenGLFunctions *funcs, GLuint id)
193 {
194     funcs->glDeleteTextures(1, &id);
195 }
196 
QOpenGLCachedTexture(GLuint id,QOpenGLTextureUploader::BindOptions options,QOpenGLContext * context)197 QOpenGLCachedTexture::QOpenGLCachedTexture(GLuint id, QOpenGLTextureUploader::BindOptions options, QOpenGLContext *context) : m_options(options)
198 {
199     m_resource = new QOpenGLSharedResourceGuard(context, id, freeTexture);
200 }
201 
202 QT_END_NAMESPACE
203