kwingltexture.cpp 13.7 KB
Newer Older
1
2
3
4
5
6
/********************************************************************
 KWin - the KDE window manager
 This file is part of the KDE project.

Copyright (C) 2006-2007 Rivo Laks <rivolaks@hot.ee>
Copyright (C) 2010, 2011 Martin Gräßlin <mgraesslin@kde.org>
7
Copyright (C) 2012 Philipp Knechtges <philipp-dev@knechtges.com>
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*********************************************************************/

#include "kwinconfig.h" // KWIN_HAVE_OPENGL

#include "kwinglplatform.h"
#include "kwineffects.h"
#include "kwinglutils_funcs.h"
#include "kwinglutils.h"

30
31
#include "kwingltexture_p.h"

32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <QPixmap>
#include <QImage>
#include <QVector2D>
#include <QVector3D>
#include <QVector4D>
#include <QMatrix4x4>

namespace KWin
{

//****************************************
// GLTexture
//****************************************

46
47
48
bool GLTexturePrivate::sNPOTTextureSupported = false;
bool GLTexturePrivate::sFramebufferObjectSupported = false;
bool GLTexturePrivate::sSaturationSupported = false;
49
50

GLTexture::GLTexture()
51
52
53
54
55
56
57
58
59
60
61
    : d_ptr(new GLTexturePrivate())
{
}

GLTexture::GLTexture(GLTexturePrivate& dd)
    : d_ptr(&dd)
{
}

GLTexture::GLTexture(const GLTexture& tex)
    : d_ptr(tex.d_ptr)
62
63
64
65
{
}

GLTexture::GLTexture(const QImage& image, GLenum target)
66
    : d_ptr(new GLTexturePrivate())
67
68
69
70
71
{
    load(image, target);
}

GLTexture::GLTexture(const QPixmap& pixmap, GLenum target)
72
    : d_ptr(new GLTexturePrivate())
73
74
75
76
77
{
    load(pixmap, target);
}

GLTexture::GLTexture(const QString& fileName)
78
     : d_ptr(new GLTexturePrivate())
79
80
81
82
83
{
    load(fileName);
}

GLTexture::GLTexture(int width, int height)
84
     : d_ptr(new GLTexturePrivate())
85
{
86
    Q_D(GLTexture);
87
    if (NPOTTextureSupported() || (isPowerOfTwo(width) && isPowerOfTwo(height))) {
88
89
90
91
92
        d->m_target = GL_TEXTURE_2D;
        d->m_scale.setWidth(1.0 / width);
        d->m_scale.setHeight(1.0 / height);
        d->m_size = QSize(width, height);
        d->m_canUseMipmaps = true;
93

94
        glGenTextures(1, &d->m_texture);
95
96
97
98
        bind();
#ifdef KWIN_HAVE_OPENGLES
        // format and internal format have to match in ES, GL_RGBA8 and GL_BGRA are not available
        // see http://www.khronos.org/opengles/sdk/docs/man/xhtml/glTexImage2D.xml
99
        glTexImage2D(d->m_target, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
100
#else
101
        glTexImage2D(d->m_target, 0, GL_RGBA8, width, height, 0, GL_BGRA, GL_UNSIGNED_BYTE, 0);
102
103
104
105
106
107
108
109
110
#endif
        unbind();
    }
}

GLTexture::~GLTexture()
{
}

111
GLTexture& GLTexture::operator = (const GLTexture& tex)
112
{
113
114
115
116
117
118
119
120
    d_ptr = tex.d_ptr;
    return *this;
}

GLTexturePrivate::GLTexturePrivate()
{
    m_texture = 0;
    m_target = 0;
121
    m_filter = GL_NEAREST;
122
    m_wrapMode = GL_REPEAT;
123
124
    m_yInverted = false;
    m_canUseMipmaps = false;
125
    m_markedDirty = false;
126
127
    m_unnormalizeActive = 0;
    m_normalizeActive = 0;
128
    m_vbo = 0;
129
    m_filterChanged = true;
130
    m_wrapModeChanged = false;
131
132
}

133
134
135
136
137
138
139
140
141
142
143
GLTexturePrivate::~GLTexturePrivate()
{
    if (m_vbo != 0) {
        delete m_vbo;
    }
    if (m_texture != 0) {
        glDeleteTextures(1, &m_texture);
    }
}

void GLTexturePrivate::initStatic()
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
{
#ifdef KWIN_HAVE_OPENGLES
    sNPOTTextureSupported = true;
    sFramebufferObjectSupported = true;
    sSaturationSupported = true;
#else
    sNPOTTextureSupported = hasGLExtension("GL_ARB_texture_non_power_of_two");
    sFramebufferObjectSupported = hasGLExtension("GL_EXT_framebuffer_object");
    sSaturationSupported = ((hasGLExtension("GL_ARB_texture_env_crossbar")
                             && hasGLExtension("GL_ARB_texture_env_dot3")) || hasGLVersion(1, 4))
                           && (glTextureUnitsCount >= 4) && glActiveTexture != NULL;
#endif
}

bool GLTexture::isNull() const
{
160
161
    Q_D(const GLTexture);
    return None == d->m_texture;
162
163
164
165
}

QSize GLTexture::size() const
{
166
167
    Q_D(const GLTexture);
    return d->m_size;
168
169
170
171
}

bool GLTexture::load(const QImage& image, GLenum target)
{
172
173
174
175
    // decrease the reference counter for the old texture
    d_ptr = new GLTexturePrivate();

    Q_D(GLTexture);
176
177
178
    if (image.isNull())
        return false;
    QImage img = image;
179
    d->m_target = target;
180
#ifndef KWIN_HAVE_OPENGLES
181
    if (d->m_target != GL_TEXTURE_RECTANGLE_ARB) {
182
183
184
185
186
187
188
#endif
        if (!NPOTTextureSupported()
                && (!isPowerOfTwo(image.width()) || !isPowerOfTwo(image.height()))) {
            // non-rectangular target requires POT texture
            img = img.scaled(nearestPowerOfTwo(image.width()),
                             nearestPowerOfTwo(image.height()));
        }
189
190
191
        d->m_scale.setWidth(1.0 / img.width());
        d->m_scale.setHeight(1.0 / img.height());
        d->m_canUseMipmaps = true;
192
193
#ifndef KWIN_HAVE_OPENGLES
    } else {
194
195
196
        d->m_scale.setWidth(1.0);
        d->m_scale.setHeight(1.0);
        d->m_canUseMipmaps = false;
197
198
    }
#endif
199
200
    d->m_size = img.size();
    d->m_yInverted = true;
201

202
    img = d->convertToGLFormat(img);
203

204
    if (isNull()) {
205
        glGenTextures(1, &d->m_texture);
206
    }
207
208
209
210
    bind();
#ifdef KWIN_HAVE_OPENGLES
    // format and internal format have to match in ES, GL_RGBA8 and GL_BGRA are not available
    // see http://www.khronos.org/opengles/sdk/docs/man/xhtml/glTexImage2D.xml
211
    glTexImage2D(d->m_target, 0, GL_RGBA, img.width(), img.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, img.bits());
212
#else
213
    glTexImage2D(d->m_target, 0, GL_RGBA8, img.width(), img.height(), 0,
214
215
216
                 GL_BGRA, GL_UNSIGNED_BYTE, img.bits());
#endif
    unbind();
217
    setFilter(GL_LINEAR);
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
    return true;
}

bool GLTexture::load(const QPixmap& pixmap, GLenum target)
{
    if (pixmap.isNull())
        return false;
    return load(pixmap.toImage(), target);
}

bool GLTexture::load(const QString& fileName)
{
    if (fileName.isEmpty())
        return false;
    return load(QImage(fileName));
}

void GLTexture::discard()
{
237
    d_ptr = new GLTexturePrivate();
238
239
}

240
void GLTexturePrivate::bind()
241
242
{
#ifndef KWIN_HAVE_OPENGLES
243
    glEnable(m_target);
244
#endif
245
    glBindTexture(m_target, m_texture);
246
247
}

248
249
250
251
void GLTexture::bind()
{
    Q_D(GLTexture);
    d->bind();
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
    if (d->m_markedDirty) {
        d->onDamage();
    }
    if (d->m_filterChanged) {
        if (d->m_filter == GL_LINEAR_MIPMAP_LINEAR) {
            // trilinear filtering requested, but is it possible?
            if (d->sNPOTTextureSupported
                    && d->sFramebufferObjectSupported
                    && d->m_canUseMipmaps) {
                glTexParameteri(d->m_target, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
                glTexParameteri(d->m_target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
                glGenerateMipmap(d->m_target);
            } else {
                // can't use trilinear, so use bilinear
                d->m_filter = GL_LINEAR;
                glTexParameteri(d->m_target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
                glTexParameteri(d->m_target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
            }
        } else if (d->m_filter == GL_LINEAR) {
            glTexParameteri(d->m_target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
            glTexParameteri(d->m_target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        } else {
            // if neither trilinear nor bilinear, default to fast filtering
            d->m_filter = GL_NEAREST;
            glTexParameteri(d->m_target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
            glTexParameteri(d->m_target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
        }
    }
    if (d->m_wrapModeChanged) {
        glTexParameteri(d->m_target, GL_TEXTURE_WRAP_S, d->m_wrapMode);
        glTexParameteri(d->m_target, GL_TEXTURE_WRAP_T, d->m_wrapMode);
    }
284
285
286
}

void GLTexturePrivate::unbind()
287
{
288
    glBindTexture(m_target, 0);
289
#ifndef KWIN_HAVE_OPENGLES
290
    glDisable(m_target);
291
292
293
#endif
}

294
295
296
297
298
299
void GLTexture::unbind()
{
    Q_D(GLTexture);
    d->unbind();
}

300
void GLTexture::render(QRegion region, const QRect& rect, bool hardwareClipping)
301
{
302
303
304
    Q_D(GLTexture);
    if (rect.size() != d->m_cachedSize) {
        d->m_cachedSize = rect.size();
305
306
        QRect r(rect);
        r.moveTo(0, 0);
307
308
        if (!d->m_vbo) {
            d->m_vbo = new GLVertexBuffer(KWin::GLVertexBuffer::Static);
309
310
311
312
313
314
315
316
        }
        const float verts[ 4 * 2 ] = {
            // NOTICE: r.x/y could be replaced by "0", but that would make it unreadable...
            r.x(), r.y(),
            r.x(), r.y() + rect.height(),
            r.x() + rect.width(), r.y(),
            r.x() + rect.width(), r.y() + rect.height()
        };
317
318
319
320
321
322
323
#ifdef KWIN_HAVE_OPENGLES
        const float texWidth = 1.0f;
        const float texHeight = 1.0f;
#else
        const float texWidth = (target() == GL_TEXTURE_RECTANGLE_ARB) ? width() : 1.0f;
        const float texHeight = (target() == GL_TEXTURE_RECTANGLE_ARB) ? height() : 1.0f;
#endif
324
        const float texcoords[ 4 * 2 ] = {
325
326
327
328
            0.0f, d->m_yInverted ? 0.0f : texHeight, // y needs to be swapped (normalized coords)
            0.0f, d->m_yInverted ? texHeight : 0.0f,
            texWidth, d->m_yInverted ? 0.0f : texHeight,
            texWidth, d->m_yInverted ? texHeight : 0.0f
329
        };
330
        d->m_vbo->setData(4, 2, verts, texcoords);
331
332
333
334
335
336
337
338
339
340
    }
    QMatrix4x4 translation;
    translation.translate(rect.x(), rect.y());
    if (ShaderManager::instance()->isShaderBound()) {
        GLShader *shader = ShaderManager::instance()->getBoundShader();
        shader->setUniform(GLShader::Offset, QVector2D(rect.x(), rect.y()));
        shader->setUniform(GLShader::WindowTransformation, translation);
    } else {
        pushMatrix(translation);
    }
341
    d->m_vbo->render(region, GL_TRIANGLE_STRIP, hardwareClipping);
342
343
344
345
346
347
348
349
350
351
    if (ShaderManager::instance()->isShaderBound()) {
        GLShader *shader = ShaderManager::instance()->getBoundShader();
        shader->setUniform(GLShader::WindowTransformation, QMatrix4x4());
    } else {
        popMatrix();
    }
}

GLuint GLTexture::texture() const
{
352
353
    Q_D(const GLTexture);
    return d->m_texture;
354
355
356
357
}

GLenum GLTexture::target() const
{
358
359
    Q_D(const GLTexture);
    return d->m_target;
360
361
362
363
}

GLenum GLTexture::filter() const
{
364
365
    Q_D(const GLTexture);
    return d->m_filter;
366
367
368
369
}

bool GLTexture::isDirty() const
{
370
    Q_D(const GLTexture);
371
    return d->m_markedDirty;
372
373
374
375
}

void GLTexture::setFilter(GLenum filter)
{
376
    Q_D(GLTexture);
377
378
379
380
    if (filter != d->m_filter) {
        d->m_filter = filter;
        d->m_filterChanged = true;
    }
381
382
383
384
}

void GLTexture::setWrapMode(GLenum mode)
{
385
    Q_D(GLTexture);
386
387
388
389
    if (mode != d->m_wrapMode) {
        d->m_wrapMode = mode;
        d->m_wrapModeChanged=true;
    }
390
391
}

392
void GLTexturePrivate::onDamage()
393
{
394
395
396
    if (m_filter == GL_LINEAR_MIPMAP_LINEAR && !m_filterChanged) {
        glGenerateMipmap(m_target);
    }
397
398
}

399
void GLTexture::setDirty()
400
{
401
402
    Q_D(GLTexture);
    d->m_markedDirty = true;
403
404
}

405
QImage GLTexturePrivate::convertToGLFormat(const QImage& img) const
406
{
407
408
    // Copied from Qt's QGLWidget::convertToGLFormat()
    QImage res;
409
#ifdef KWIN_HAVE_OPENGLES
410
411
    res = QImage(img.size(), QImage::Format_ARGB32);
    QImage imgARGB32 = img.convertToFormat(QImage::Format_ARGB32_Premultiplied);
412
413
414

    const int width = img.width();
    const int height = img.height();
415
416
417
418
419
420
421
422
423
424
    const uint32_t *p = (const uint32_t*) imgARGB32.scanLine(0);
    uint32_t *q = (uint32_t*) res.scanLine(0);

    if (QSysInfo::ByteOrder == QSysInfo::BigEndian) {
        for (int i = 0; i < height; ++i) {
            const uint32_t *end = p + width;
            while (p < end) {
                *q = (*p << 8) | ((*p >> 24) & 0xFF);
                p++;
                q++;
425
426
427
            }
        }
    } else {
428
429
430
431
432
433
434
        // GL_BGRA -> GL_RGBA
        for (int i = 0; i < height; ++i) {
            const uint32_t *end = p + width;
            while (p < end) {
                *q = ((*p << 16) & 0xff0000) | ((*p >> 16) & 0xff) | (*p & 0xff00ff00);
                p++;
                q++;
435
436
437
438
            }
        }
    }
#else
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
    if (QSysInfo::ByteOrder == QSysInfo::BigEndian) {
        res = QImage(img.size(), QImage::Format_ARGB32);
        QImage imgARGB32 = img.convertToFormat(QImage::Format_ARGB32_Premultiplied);

        const int width = img.width();
        const int height = img.height();
        const uint32_t *p = (const uint32_t*) imgARGB32.scanLine(0);
        uint32_t *q = (uint32_t*) res.scanLine(0);

        // swizzle
        for (int i = 0; i < height; ++i) {
            const uint32_t *end = p + width;
            while (p < end) {
                *q = ((*p << 24) & 0xff000000)
                     | ((*p >> 24) & 0x000000ff)
                     | ((*p << 8) & 0x00ff0000)
                     | ((*p >> 8) & 0x0000ff00);
                p++;
                q++;
            }
        }
    } else {
        res = img.convertToFormat(QImage::Format_ARGB32_Premultiplied);
    }
463
464
465
466
#endif
    return res;
}

Martin Flöser's avatar
Martin Flöser committed
467
468
bool GLTexture::isYInverted() const
{
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
    Q_D(const GLTexture);
    return d->m_yInverted;
}

void GLTexture::setYInverted(bool inverted)
{
    Q_D(GLTexture);
    d->m_yInverted = inverted;
}

int GLTexture::width() const
{
    Q_D(const GLTexture);
    return d->m_size.width();
}

int GLTexture::height() const
{
    Q_D(const GLTexture);
    return d->m_size.height();
}

bool GLTexture::NPOTTextureSupported()
{
    return GLTexturePrivate::sNPOTTextureSupported;
}

bool GLTexture::framebufferObjectSupported()
{
    return GLTexturePrivate::sFramebufferObjectSupported;
}

bool GLTexture::saturationSupported()
{
    return GLTexturePrivate::sSaturationSupported;
}

506
} // namespace KWin