Scale mipmapped npot images when not supported
authorLaszlo Agocs <laszlo.agocs@theqtcompany.com>
Fri, 16 Jan 2015 10:01:58 +0000 (11:01 +0100)
committerLaszlo Agocs <laszlo.agocs@theqtcompany.com>
Mon, 19 Jan 2015 18:52:21 +0000 (19:52 +0100)
Task-number: QTBUG-43847
Task-number: QTBUG-40789
Change-Id: Iceacaa49bafffb31752a9fb26c896df570153fec
Reviewed-by: Gunnar Sletta <gunnar@sletta.org>
src/quick/scenegraph/util/qsgtexture.cpp

index fc50500..bfc9212 100644 (file)
@@ -35,6 +35,7 @@
 #include <qopenglfunctions.h>
 #include <QtQuick/private/qsgcontext_p.h>
 #include <qthread.h>
+#include <qmath.h>
 #include <private/qquickprofiler_p.h>
 #include <private/qqmlglobal_p.h>
 #include <QtGui/qguiapplication.h>
@@ -75,13 +76,11 @@ static bool qsg_leak_check = !qgetenv("QML_LEAK_CHECK").isEmpty();
 
 QT_BEGIN_NAMESPACE
 
-#ifndef QT_NO_DEBUG
 inline static bool isPowerOfTwo(int x)
 {
     // Assumption: x >= 1
     return x == (x & -x);
 }
-#endif
 
 QSGTexturePrivate::QSGTexturePrivate()
     : wrapChanged(false)
@@ -686,6 +685,16 @@ void QSGPlainTexture::bind()
         m_texture_size = tmp.size();
     }
 
+    // Scale to a power of two size if mipmapping is requested and the
+    // texture is npot and npot textures are not properly supported.
+    if (mipmapFiltering() != QSGTexture::None
+        && (!isPowerOfTwo(m_texture_size.width()) || !isPowerOfTwo(m_texture_size.height()))
+        && !funcs->hasOpenGLFeature(QOpenGLFunctions::NPOTTextures)) {
+        tmp = tmp.scaled(qNextPowerOfTwo(m_texture_size.width()), qNextPowerOfTwo(m_texture_size.height()),
+                         Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
+        m_texture_size = tmp.size();
+    }
+
     if (tmp.width() * 4 != tmp.bytesPerLine())
         tmp = tmp.copy();