From ca9ba386823c47721f6e0c7e184b5230a2c967fd Mon Sep 17 00:00:00 2001 From: Tats Date: Sat, 26 Apr 2014 13:32:27 +0100 Subject: [PATCH] Performance improvement: copy texture bits to video card only if they have changed --- Mapper.cpp | 10 +++++++--- MediaImpl.cpp | 17 ++++++++++++----- MediaImpl.h | 3 ++- Paint.cpp | 5 +++-- Paint.h | 19 ++++++++++++++----- SourceGLCanvas.cpp | 7 +++++-- 6 files changed, 43 insertions(+), 18 deletions(-) diff --git a/Mapper.cpp b/Mapper.cpp index e20584c..9d682ea 100644 --- a/Mapper.cpp +++ b/Mapper.cpp @@ -307,9 +307,13 @@ void TextureMapper::draw(QPainter* painter) glEnable (GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, texture->getTextureId()); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, - texture->getWidth(), texture->getHeight(), 0, GL_RGBA, - GL_UNSIGNED_BYTE, texture->getBits()); + // Copy bits to texture iff necessary. + if (texture->bitsHaveChanged()) + { + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, + texture->getWidth(), texture->getHeight(), 0, GL_RGBA, + GL_UNSIGNED_BYTE, texture->getBits()); + } glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); diff --git a/MediaImpl.cpp b/MediaImpl.cpp index d63a6e2..aaccde2 100644 --- a/MediaImpl.cpp +++ b/MediaImpl.cpp @@ -404,13 +404,15 @@ bool MediaImpl::loadMovie(QString filename) } -void MediaImpl::runVideo() { +bool MediaImpl::runVideo() { // if (!_VIDEO_OUT->connected()) // return; if (!_preRun()) - return; + return false; + + bool bitsChanged = false; if (_videoNewBufferCounter > 0) { @@ -420,14 +422,19 @@ void MediaImpl::runVideo() { _setFinished(true); // _FINISH_OUT->type()->setValue(1.0f); // _VIDEO_OUT->sleeping(true); - } else -// _VIDEO_OUT->sleeping(false); - + } + else + { + bitsChanged = true; + // _VIDEO_OUT->sleeping(false); + } _videoNewBufferCounter--; } _postRun(); + + return bitsChanged; } diff --git a/MediaImpl.h b/MediaImpl.h index 5b006ec..e673e72 100644 --- a/MediaImpl.h +++ b/MediaImpl.h @@ -53,7 +53,8 @@ public: int getHeight() const; const uchar* getBits() const; - void runVideo(); + /// Returns true if the image has changed. + bool runVideo(); // void runAudio(); protected: diff --git a/Paint.cpp b/Paint.cpp index bed9291..04ae499 100644 --- a/Paint.cpp +++ b/Paint.cpp @@ -76,10 +76,11 @@ int Media::getHeight() const } void Media::update() { - impl_->runVideo(); + if (impl_->runVideo()) + bitsChanged = true; } -const uchar* Media::getBits() const +const uchar* Media::_getBits() const { return this->impl_->getBits(); } diff --git a/Paint.h b/Paint.h index a80bc1d..fc7633f 100644 --- a/Paint.h +++ b/Paint.h @@ -103,12 +103,14 @@ protected: GLuint textureId; GLfloat x; GLfloat y; + mutable bool bitsChanged; Texture(uid id=NULL_UID) : Paint(id), textureId(0), x(0), - y(0) + y(0), + bitsChanged(true) {} virtual ~Texture() { if (textureId != 0) @@ -123,7 +125,14 @@ public: } virtual int getWidth() const = 0; virtual int getHeight() const = 0; - virtual const uchar* getBits() const = 0; + virtual const uchar* getBits() const { + bitsChanged = false; + return _getBits(); + } + virtual const uchar* _getBits() const = 0; + + /// Returns true iff bits have changed since last call to getBits(). + bool bitsHaveChanged() const { return bitsChanged; } virtual void setPosition(GLfloat xPos, GLfloat yPos) { x = xPos; @@ -147,7 +156,6 @@ public: Texture(id), uri(uri_) { - image = QGLWidget::convertToGLFormat(QImage(uri)); } virtual ~Image() {} @@ -155,13 +163,14 @@ public: const QString getUri() const { return uri; } virtual void build() { + image = QGLWidget::convertToGLFormat(QImage(uri)); } virtual QString getType() const { return "image"; } virtual int getWidth() const { return image.width(); } virtual int getHeight() const { return image.height(); } - virtual const uchar* getBits() const { return image.bits(); } + virtual const uchar* _getBits() const { return image.bits(); } }; class MediaImpl; // forward declaration @@ -188,7 +197,7 @@ public: } virtual int getWidth() const; virtual int getHeight() const; - virtual const uchar* getBits() const; + virtual const uchar* _getBits() const; /** * Checks whether or not video is supported on this platform. */ diff --git a/SourceGLCanvas.cpp b/SourceGLCanvas.cpp index 1ac96e3..ce2152d 100644 --- a/SourceGLCanvas.cpp +++ b/SourceGLCanvas.cpp @@ -112,8 +112,11 @@ void SourceGLCanvas::_drawTexture(QPainter* painter, Paint::ptr paint, QMapgetTextureId()); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texture->getWidth(), texture->getHeight(), 0, GL_RGBA, - GL_UNSIGNED_BYTE, texture->getBits()); + if (texture->bitsHaveChanged()) + { + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texture->getWidth(), texture->getHeight(), 0, GL_RGBA, + GL_UNSIGNED_BYTE, texture->getBits()); + } //std::cout << texture->getX() << "x" << texture->getY() << " : " << texture->getWidth() << "x" << texture->getHeight() << " " << texture->getTextureId() << std::endl;