diff --git a/interface/src/GLCanvas.cpp b/interface/src/GLCanvas.cpp index 7bc79e56d8..b68c4fdacd 100644 --- a/interface/src/GLCanvas.cpp +++ b/interface/src/GLCanvas.cpp @@ -11,18 +11,26 @@ #include "GLCanvas.h" #include #include +#include -GLCanvas::GLCanvas() : QGLWidget(QGLFormat(QGL::NoDepthBuffer, QGL::NoStencilBuffer)) { +GLCanvas::GLCanvas() : QGLWidget(QGLFormat(QGL::NoDepthBuffer, QGL::NoStencilBuffer)), + _throttleRendering(false), + _idleRenderInterval(100) +{ } void GLCanvas::initializeGL() { Application::getInstance()->initializeGL(); setAttribute(Qt::WA_AcceptTouchEvents); setAcceptDrops(true); + connect(Application::getInstance(), SIGNAL(applicationStateChanged(Qt::ApplicationState)), this, SLOT(activeChanged(Qt::ApplicationState))); + connect(&_frameTimer, SIGNAL(timeout()), this, SLOT(throttleRender())); } void GLCanvas::paintGL() { - Application::getInstance()->paintGL(); + if (!_throttleRendering && !Application::getInstance()->getWindow()->isMinimized()) { + Application::getInstance()->paintGL(); + } } void GLCanvas::resizeGL(int width, int height) { @@ -49,6 +57,38 @@ void GLCanvas::mouseReleaseEvent(QMouseEvent* event) { Application::getInstance()->mouseReleaseEvent(event); } +void GLCanvas::activeChanged(Qt::ApplicationState state) { + switch (state) { + case Qt::ApplicationActive: + // If we're active, stop the frame timer and the throttle. + _frameTimer.stop(); + _throttleRendering = false; + break; + + case Qt::ApplicationSuspended: + case Qt::ApplicationHidden: + // If we're hidden or are about to suspend, don't render anything. + _throttleRendering = false; + _frameTimer.stop(); + break; + + default: + // Otherwise, throttle. + if (!_throttleRendering) { + _frameTimer.start(_idleRenderInterval); + _throttleRendering = true; + } + break; + } +} + +void GLCanvas::throttleRender() { + _frameTimer.start(_idleRenderInterval); + if (!Application::getInstance()->getWindow()->isMinimized()) { + Application::getInstance()->paintGL(); + } +} + int updateTime = 0; bool GLCanvas::event(QEvent* event) { switch (event->type()) { diff --git a/interface/src/GLCanvas.h b/interface/src/GLCanvas.h index 0f0cb5c7d0..e6dcc38977 100644 --- a/interface/src/GLCanvas.h +++ b/interface/src/GLCanvas.h @@ -10,13 +10,19 @@ #define __hifi__GLCanvas__ #include +#include /// customized canvas that simply forwards requests/events to the singleton application class GLCanvas : public QGLWidget { + Q_OBJECT public: GLCanvas(); protected: + QTimer _frameTimer; + bool _throttleRendering; + int _idleRenderInterval; + virtual void initializeGL(); virtual void paintGL(); virtual void resizeGL(int width, int height); @@ -34,6 +40,10 @@ protected: virtual void dragEnterEvent(QDragEnterEvent *event); virtual void dropEvent(QDropEvent* event); + +private slots: + void activeChanged(Qt::ApplicationState state); + void throttleRender(); }; #endif /* defined(__hifi__GLCanvas__) */