~ubuntu-branches/ubuntu/wily/qtbase-opensource-src/wily

« back to all changes in this revision

Viewing changes to src/gui/opengl/qopengltexturecache.cpp

  • Committer: Package Import Robot
  • Author(s): Timo Jyrinki
  • Date: 2013-02-05 12:46:17 UTC
  • Revision ID: package-import@ubuntu.com-20130205124617-c8jouts182j002fx
Tags: upstream-5.0.1+dfsg
ImportĀ upstreamĀ versionĀ 5.0.1+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
 
4
** Contact: http://www.qt-project.org/legal
 
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 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.
 
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 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.
 
24
**
 
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.
 
28
**
 
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.
 
36
**
 
37
**
 
38
** $QT_END_LICENSE$
 
39
**
 
40
****************************************************************************/
 
41
 
 
42
#include "qopengltexturecache_p.h"
 
43
#include <private/qopenglcontext_p.h>
 
44
#include <private/qimagepixmapcleanuphooks_p.h>
 
45
#include <qpa/qplatformpixmap.h>
 
46
 
 
47
QT_BEGIN_NAMESPACE
 
48
 
 
49
class QOpenGLTextureCacheWrapper
 
50
{
 
51
public:
 
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
 
 
74
private:
 
75
    QOpenGLMultiGroupSharedResource m_resource;
 
76
    QMutex m_mutex;
 
77
};
 
78
 
 
79
Q_GLOBAL_STATIC(QOpenGLTextureCacheWrapper, qt_texture_caches)
 
80
 
 
81
QOpenGLTextureCache *QOpenGLTextureCache::cacheForContext(QOpenGLContext *context)
 
82
{
 
83
    return qt_texture_caches()->cacheForContext(context);
 
84
}
 
85
 
 
86
void 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
 
 
93
void QOpenGLTextureCacheWrapper::cleanupTexturesForPixmapData(QPlatformPixmap *pmd)
 
94
{
 
95
    cleanupTexturesForCacheKey(pmd->cacheKey());
 
96
}
 
97
 
 
98
QOpenGLTextureCache::QOpenGLTextureCache(QOpenGLContext *ctx)
 
99
    : QOpenGLSharedResource(ctx->shareGroup())
 
100
    , m_cache(64 * 1024) // 64 MB cache
 
101
{
 
102
}
 
103
 
 
104
QOpenGLTextureCache::~QOpenGLTextureCache()
 
105
{
 
106
}
 
107
 
 
108
GLuint QOpenGLTextureCache::bindTexture(QOpenGLContext *context, const QPixmap &pixmap)
 
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) {
 
119
            glBindTexture(GL_TEXTURE_2D, entry->id());
 
120
            return entry->id();
 
121
        }
 
122
    }
 
123
 
 
124
    GLuint id = bindTexture(context, key, pixmap.toImage());
 
125
    if (id > 0)
 
126
        QImagePixmapCleanupHooks::enableCleanupHooks(pixmap);
 
127
 
 
128
    return id;
 
129
}
 
130
 
 
131
GLuint QOpenGLTextureCache::bindTexture(QOpenGLContext *context, const QImage &image)
 
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) {
 
142
            glBindTexture(GL_TEXTURE_2D, entry->id());
 
143
            return entry->id();
 
144
        }
 
145
    }
 
146
 
 
147
    GLuint id = bindTexture(context, key, image);
 
148
    if (id > 0)
 
149
        QImagePixmapCleanupHooks::enableCleanupHooks(image);
 
150
 
 
151
    return id;
 
152
}
 
153
 
 
154
static inline void qgl_byteSwapImage(QImage &img)
 
155
{
 
156
    const int width = img.width();
 
157
    const int height = img.height();
 
158
 
 
159
    if (QSysInfo::ByteOrder == QSysInfo::LittleEndian)
 
160
    {
 
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);
 
165
        }
 
166
    } else {
 
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);
 
171
        }
 
172
    }
 
173
}
 
174
 
 
175
GLuint QOpenGLTextureCache::bindTexture(QOpenGLContext *context, qint64 key, const QImage &image)
 
176
{
 
177
    GLuint id;
 
178
    glGenTextures(1, &id);
 
179
    glBindTexture(GL_TEXTURE_2D, id);
 
180
 
 
181
    QImage tx = image.convertToFormat(QImage::Format_ARGB32_Premultiplied);
 
182
 
 
183
    qgl_byteSwapImage(tx);
 
184
 
 
185
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tx.width(), tx.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, const_cast<const QImage &>(tx).bits());
 
186
 
 
187
    int cost = tx.width() * tx.height() * 4 / 1024;
 
188
    m_cache.insert(key, new QOpenGLCachedTexture(id, context), cost);
 
189
 
 
190
    return id;
 
191
}
 
192
 
 
193
void QOpenGLTextureCache::invalidate(qint64 key)
 
194
{
 
195
    QMutexLocker locker(&m_mutex);
 
196
    m_cache.remove(key);
 
197
}
 
198
 
 
199
void QOpenGLTextureCache::invalidateResource()
 
200
{
 
201
    m_cache.clear();
 
202
}
 
203
 
 
204
void QOpenGLTextureCache::freeResource(QOpenGLContext *)
 
205
{
 
206
    Q_ASSERT(false); // the texture cache lives until the context group disappears
 
207
}
 
208
 
 
209
static void freeTexture(QOpenGLFunctions *, GLuint id)
 
210
{
 
211
    glDeleteTextures(1, &id);
 
212
}
 
213
 
 
214
QOpenGLCachedTexture::QOpenGLCachedTexture(GLuint id, QOpenGLContext *context)
 
215
{
 
216
    m_resource = new QOpenGLSharedResourceGuard(context, id, freeTexture);
 
217
}
 
218
 
 
219
QT_END_NAMESPACE