1
/****************************************************************************
3
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4
** Contact: http://www.qt-project.org/legal
6
** This file is part of the QtGui module of the Qt Toolkit.
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 Digia. For licensing terms and
14
** conditions see http://qt.digia.com/licensing. For further information
15
** use the contact form at http://qt.digia.com/contact-us.
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 2.1 as published by the Free Software
20
** Foundation and appearing in the file LICENSE.LGPL included in the
21
** packaging of this file. Please review the following information to
22
** ensure the GNU Lesser General Public License version 2.1 requirements
23
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25
** In addition, as a special exception, Digia gives you certain additional
26
** rights. These rights are described in the Digia Qt LGPL Exception
27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29
** GNU General Public License Usage
30
** Alternatively, this file may be used under the terms of the GNU
31
** General Public License version 3.0 as published by the Free Software
32
** Foundation and appearing in the file LICENSE.GPL included in the
33
** packaging of this file. Please review the following information to
34
** ensure the GNU General Public License version 3.0 requirements will be
35
** met: http://www.gnu.org/copyleft/gpl.html.
40
****************************************************************************/
42
#include "qopengltexturecache_p.h"
43
#include <private/qopenglcontext_p.h>
44
#include <private/qimagepixmapcleanuphooks_p.h>
45
#include <qpa/qplatformpixmap.h>
49
class QOpenGLTextureCacheWrapper
52
QOpenGLTextureCacheWrapper()
54
QImagePixmapCleanupHooks::instance()->addPlatformPixmapModificationHook(cleanupTexturesForPixmapData);
55
QImagePixmapCleanupHooks::instance()->addPlatformPixmapDestructionHook(cleanupTexturesForPixmapData);
56
QImagePixmapCleanupHooks::instance()->addImageHook(cleanupTexturesForCacheKey);
59
~QOpenGLTextureCacheWrapper()
61
QImagePixmapCleanupHooks::instance()->removePlatformPixmapModificationHook(cleanupTexturesForPixmapData);
62
QImagePixmapCleanupHooks::instance()->removePlatformPixmapDestructionHook(cleanupTexturesForPixmapData);
63
QImagePixmapCleanupHooks::instance()->removeImageHook(cleanupTexturesForCacheKey);
66
QOpenGLTextureCache *cacheForContext(QOpenGLContext *context) {
67
QMutexLocker lock(&m_mutex);
68
return m_resource.value<QOpenGLTextureCache>(context);
71
static void cleanupTexturesForCacheKey(qint64 key);
72
static void cleanupTexturesForPixmapData(QPlatformPixmap *pmd);
75
QOpenGLMultiGroupSharedResource m_resource;
79
Q_GLOBAL_STATIC(QOpenGLTextureCacheWrapper, qt_texture_caches)
81
QOpenGLTextureCache *QOpenGLTextureCache::cacheForContext(QOpenGLContext *context)
83
return qt_texture_caches()->cacheForContext(context);
86
void QOpenGLTextureCacheWrapper::cleanupTexturesForCacheKey(qint64 key)
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);
93
void QOpenGLTextureCacheWrapper::cleanupTexturesForPixmapData(QPlatformPixmap *pmd)
95
cleanupTexturesForCacheKey(pmd->cacheKey());
98
QOpenGLTextureCache::QOpenGLTextureCache(QOpenGLContext *ctx)
99
: QOpenGLSharedResource(ctx->shareGroup())
100
, m_cache(64 * 1024) // 64 MB cache
104
QOpenGLTextureCache::~QOpenGLTextureCache()
108
GLuint QOpenGLTextureCache::bindTexture(QOpenGLContext *context, const QPixmap &pixmap)
112
QMutexLocker locker(&m_mutex);
113
qint64 key = pixmap.cacheKey();
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);
119
glBindTexture(GL_TEXTURE_2D, entry->id());
124
GLuint id = bindTexture(context, key, pixmap.toImage());
126
QImagePixmapCleanupHooks::enableCleanupHooks(pixmap);
131
GLuint QOpenGLTextureCache::bindTexture(QOpenGLContext *context, const QImage &image)
135
QMutexLocker locker(&m_mutex);
136
qint64 key = image.cacheKey();
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);
142
glBindTexture(GL_TEXTURE_2D, entry->id());
147
GLuint id = bindTexture(context, key, image);
149
QImagePixmapCleanupHooks::enableCleanupHooks(image);
154
static inline void qgl_byteSwapImage(QImage &img)
156
const int width = img.width();
157
const int height = img.height();
159
if (QSysInfo::ByteOrder == QSysInfo::LittleEndian)
161
for (int i = 0; i < height; ++i) {
162
uint *p = (uint *) img.scanLine(i);
163
for (int x = 0; x < width; ++x)
164
p[x] = ((p[x] << 16) & 0xff0000) | ((p[x] >> 16) & 0xff) | (p[x] & 0xff00ff00);
167
for (int i = 0; i < height; ++i) {
168
uint *p = (uint *) img.scanLine(i);
169
for (int x = 0; x < width; ++x)
170
p[x] = (p[x] << 8) | (p[x] >> 24);
175
GLuint QOpenGLTextureCache::bindTexture(QOpenGLContext *context, qint64 key, const QImage &image)
178
glGenTextures(1, &id);
179
glBindTexture(GL_TEXTURE_2D, id);
181
QImage tx = image.convertToFormat(QImage::Format_ARGB32_Premultiplied);
183
qgl_byteSwapImage(tx);
185
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tx.width(), tx.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, const_cast<const QImage &>(tx).bits());
187
int cost = tx.width() * tx.height() * 4 / 1024;
188
m_cache.insert(key, new QOpenGLCachedTexture(id, context), cost);
193
void QOpenGLTextureCache::invalidate(qint64 key)
195
QMutexLocker locker(&m_mutex);
199
void QOpenGLTextureCache::invalidateResource()
204
void QOpenGLTextureCache::freeResource(QOpenGLContext *)
206
Q_ASSERT(false); // the texture cache lives until the context group disappears
209
static void freeTexture(QOpenGLFunctions *, GLuint id)
211
glDeleteTextures(1, &id);
214
QOpenGLCachedTexture::QOpenGLCachedTexture(GLuint id, QOpenGLContext *context)
216
m_resource = new QOpenGLSharedResourceGuard(context, id, freeTexture);