Have textureId() return the correct value and document this behavior
authorGunnar Sletta <gunnar.sletta@nokia.com>
Tue, 4 Oct 2011 08:41:13 +0000 (10:41 +0200)
committerQt by Nokia <qt-info@nokia.com>
Tue, 4 Oct 2011 13:10:57 +0000 (15:10 +0200)
Change-Id: Ia75b5fc3b6c9f15bb15e8850295c33ba32a485f1
Reviewed-on: http://codereview.qt-project.org/5970
Reviewed-by: Qt Sanity Bot <qt_sanity_bot@ovi.com>
Reviewed-by: Kim M. Kalland <kim.kalland@nokia.com>
src/declarative/scenegraph/util/qsgtexture.cpp
src/declarative/scenegraph/util/qsgtexture_p.h

index bec325f..4e208c0 100644 (file)
@@ -210,6 +210,19 @@ bool QSGTexture::isAtlasTexture() const
     return false;
 }
 
+/*!
+    \fn int QSGTexture::textureId() const
+
+    Returns the OpenGL texture id for this texture.
+
+    The default value is 0, indicating that it is an invalid texture id.
+
+    The function should at all times return the correct texture id.
+
+    \warning This function can only be called from the rendering thread.
+ */
+
+
 
 /*!
     Returns the rectangle inside textureSize() that this texture
@@ -395,6 +408,22 @@ void QSGPlainTexture::setImage(const QImage &image)
     m_dirty_bind_options = true;
  }
 
+int QSGPlainTexture::textureId() const
+{
+    if (m_dirty_texture) {
+        if (m_image.isNull()) {
+            // The actual texture and id will be updated/deleted in a later bind()
+            // or ~QSGPlainTexture so just keep it minimal here.
+            return 0;
+        } else {
+            // Generate a texture id for use later and return it.
+            glGenTextures(1, &const_cast<QSGPlainTexture *>(this)->m_texture_id);
+            return m_texture_id;
+        }
+    }
+    return m_texture_id;
+}
+
 void QSGPlainTexture::setTextureId(int id)
 {
     if (m_texture_id && m_owns_texture)
@@ -430,10 +459,10 @@ void QSGPlainTexture::bind()
 
     m_dirty_texture = false;
 
-    if (m_texture_id && m_owns_texture)
-        glDeleteTextures(1, &m_texture_id);
 
     if (m_image.isNull()) {
+        if (m_texture_id && m_owns_texture)
+            glDeleteTextures(1, &m_texture_id);
         m_texture_id = 0;
         m_texture_size = QSize();
         m_has_mipmaps = false;
@@ -441,7 +470,8 @@ void QSGPlainTexture::bind()
         return;
     }
 
-    glGenTextures(1, &m_texture_id);
+    if (m_texture_id == 0)
+        glGenTextures(1, &m_texture_id);
     glBindTexture(GL_TEXTURE_2D, m_texture_id);
 
     // ### TODO: check for out-of-memory situations...
index 22812f8..460c0e8 100644 (file)
@@ -77,8 +77,7 @@ public:
     bool ownsTexture() const { return m_owns_texture; }
 
     void setTextureId(int id);
-    int textureId() const { return m_texture_id; }
-
+    int textureId() const;
     void setTextureSize(const QSize &size) { m_texture_size = size; }
     QSize textureSize() const { return m_texture_size; }