From 4b9448db5e1d17bd0f94c84fceb5b69b3fee9167 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20H=C3=B6glund?= Date: Wed, 26 Nov 2014 21:34:27 +0100 Subject: [PATCH] Inline GLTexture::load(QImage) in the constructor This is the only place where it is called. There was also no need for this function to be virtual since it's not reimplemented anywhere. This also fixes the GLTexturePrivate being created only to be immediately deleted and replaced by a new GLTexturePrivate when the GLTexture(QImage) constructor was used. With this change the structure of the GLTexture effectively becomes immutable to the outside world; the contents of the texture images can change, but the texture images themselves cannot be reallocated. --- libkwineffects/kwingltexture.cpp | 103 ++++++++++++++----------------- libkwineffects/kwingltexture.h | 1 - 2 files changed, 45 insertions(+), 59 deletions(-) diff --git a/libkwineffects/kwingltexture.cpp b/libkwineffects/kwingltexture.cpp index f34d9db144..2cf07fdb78 100644 --- a/libkwineffects/kwingltexture.cpp +++ b/libkwineffects/kwingltexture.cpp @@ -67,19 +67,59 @@ GLTexture::GLTexture(const GLTexture& tex) GLTexture::GLTexture(const QImage& image, GLenum target) : d_ptr(new GLTexturePrivate()) { - load(image, target); + Q_D(GLTexture); + + if (image.isNull()) + return; + + d->m_target = target; + + if (d->m_target != GL_TEXTURE_RECTANGLE_ARB) { + d->m_scale.setWidth(1.0 / image.width()); + d->m_scale.setHeight(1.0 / image.height()); + d->m_canUseMipmaps = true; + } else { + d->m_scale.setWidth(1.0); + d->m_scale.setHeight(1.0); + d->m_canUseMipmaps = false; + } + + d->m_size = image.size(); + d->m_yInverted = true; + + d->updateMatrix(); + + glGenTextures(1, &d->m_texture); + bind(); + + if (!GLPlatform::instance()->isGLES()) { + const QImage im = image.convertToFormat(QImage::Format_ARGB32_Premultiplied); + glTexImage2D(d->m_target, 0, GL_RGBA8, im.width(), im.height(), 0, + GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, im.bits()); + } else { + if (d->s_supportsARGB32) { + const QImage im = image.convertToFormat(QImage::Format_ARGB32_Premultiplied); + glTexImage2D(d->m_target, 0, GL_BGRA_EXT, im.width(), im.height(), + 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE, im.bits()); + } else { + const QImage im = image.convertToFormat(QImage::Format_RGBA8888_Premultiplied); + glTexImage2D(d->m_target, 0, GL_RGBA, im.width(), im.height(), + 0, GL_RGBA, GL_UNSIGNED_BYTE, im.bits()); + } + } + + unbind(); + setFilter(GL_LINEAR); } GLTexture::GLTexture(const QPixmap& pixmap, GLenum target) - : d_ptr(new GLTexturePrivate()) + : GLTexture(pixmap.toImage(), target) { - load(pixmap.toImage(), target); } GLTexture::GLTexture(const QString& fileName) - : d_ptr(new GLTexturePrivate()) + : GLTexture(QImage(fileName)) { - load(QImage(fileName)); } GLTexture::GLTexture(int width, int height) @@ -197,59 +237,6 @@ QSize GLTexture::size() const return d->m_size; } -bool GLTexture::load(const QImage& image, GLenum target) -{ - // decrease the reference counter for the old texture - d_ptr = new GLTexturePrivate(); - - Q_D(GLTexture); - if (image.isNull()) - return false; - - d->m_target = target; - - if (d->m_target != GL_TEXTURE_RECTANGLE_ARB) { - d->m_scale.setWidth(1.0 / image.width()); - d->m_scale.setHeight(1.0 / image.height()); - d->m_canUseMipmaps = true; - } else { - d->m_scale.setWidth(1.0); - d->m_scale.setHeight(1.0); - d->m_canUseMipmaps = false; - } - - d->m_size = image.size(); - d->m_yInverted = true; - - d->updateMatrix(); - - if (isNull()) { - glGenTextures(1, &d->m_texture); - } - - bind(); - - if (!GLPlatform::instance()->isGLES()) { - const QImage im = image.convertToFormat(QImage::Format_ARGB32_Premultiplied); - glTexImage2D(d->m_target, 0, GL_RGBA8, im.width(), im.height(), 0, - GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, im.bits()); - } else { - if (d->s_supportsARGB32) { - const QImage im = image.convertToFormat(QImage::Format_ARGB32_Premultiplied); - glTexImage2D(d->m_target, 0, GL_BGRA_EXT, im.width(), im.height(), - 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE, im.bits()); - } else { - const QImage im = image.convertToFormat(QImage::Format_RGBA8888_Premultiplied); - glTexImage2D(d->m_target, 0, GL_RGBA, im.width(), im.height(), - 0, GL_RGBA, GL_UNSIGNED_BYTE, im.bits()); - } - } - - unbind(); - setFilter(GL_LINEAR); - return true; -} - void GLTexture::update(const QImage &image, const QPoint &offset, const QRect &src) { if (image.isNull() || isNull()) diff --git a/libkwineffects/kwingltexture.h b/libkwineffects/kwingltexture.h index 02f1c00bdb..34fd8844c2 100644 --- a/libkwineffects/kwingltexture.h +++ b/libkwineffects/kwingltexture.h @@ -82,7 +82,6 @@ public: */ QMatrix4x4 matrix(TextureCoordinateType type) const; - virtual bool load(const QImage& image, GLenum target = GL_TEXTURE_2D); void update(const QImage& image, const QPoint &offset = QPoint(0, 0), const QRect &src = QRect()); virtual void discard(); void bind();