diff --git a/interface/src/GLCanvas.cpp b/interface/src/GLCanvas.cpp index 7bc79e56d8..a098af7fbe 100644 --- a/interface/src/GLCanvas.cpp +++ b/interface/src/GLCanvas.cpp @@ -12,17 +12,21 @@ #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(), &Application::focusChanged, this, &GLCanvas::activeChanged); + connect(&_frameTimer, &QTimer::timeout, this, &GLCanvas::throttleRender); } void GLCanvas::paintGL() { - Application::getInstance()->paintGL(); + if (!_throttleRendering) { + Application::getInstance()->paintGL(); + } } void GLCanvas::resizeGL(int width, int height) { @@ -49,6 +53,27 @@ void GLCanvas::mouseReleaseEvent(QMouseEvent* event) { Application::getInstance()->mouseReleaseEvent(event); } +void GLCanvas::activeChanged() +{ + if (!isActiveWindow()) + { + if (!_throttleRendering) + { + _frameTimer.start(_idleRenderInterval); + _throttleRendering = true; + } + } else { + _frameTimer.stop(); + _throttleRendering = false; + } +} + +void GLCanvas::throttleRender() +{ + _frameTimer.start(_idleRenderInterval); + 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..7e4a4d0334 100644 --- a/interface/src/GLCanvas.h +++ b/interface/src/GLCanvas.h @@ -10,6 +10,7 @@ #define __hifi__GLCanvas__ #include +#include /// customized canvas that simply forwards requests/events to the singleton application class GLCanvas : public QGLWidget { @@ -17,6 +18,10 @@ public: GLCanvas(); protected: + QTimer _frameTimer; + bool _throttleRendering; + int _idleRenderInterval; + virtual void initializeGL(); virtual void paintGL(); virtual void resizeGL(int width, int height); @@ -34,6 +39,9 @@ protected: virtual void dragEnterEvent(QDragEnterEvent *event); virtual void dropEvent(QDropEvent* event); + + void activeChanged(); + void throttleRender(); }; #endif /* defined(__hifi__GLCanvas__) */