From f65f28c52a36f247afb6f91dda7636ed3a79569b Mon Sep 17 00:00:00 2001 From: danteruiz Date: Tue, 30 Apr 2019 16:44:47 -0700 Subject: [PATCH] refresh rate controller --- interface/src/Application.cpp | 34 +++++++++++++---------- interface/src/RefreshRateManager.cpp | 40 +++++++++++++++++++--------- interface/src/RefreshRateManager.h | 8 ++++++ 3 files changed, 55 insertions(+), 27 deletions(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 9a2d320329..87b872d7b7 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -4064,9 +4064,6 @@ bool Application::event(QEvent* event) { case QEvent::KeyRelease: keyReleaseEvent(static_cast(event)); return true; - case QEvent::FocusIn: - focusInEvent(static_cast(event)); - return true; case QEvent::FocusOut: focusOutEvent(static_cast(event)); return true; @@ -4109,6 +4106,17 @@ bool Application::eventFilter(QObject* object, QEvent* event) { return true; } + auto eventType = event->type(); + if (eventType == QEvent::KeyPress || eventType == QEvent::KeyRelease || eventType == QEvent::MouseMove) { + RefreshRateManager& refreshRateManager = getRefreshRateManager(); + auto refreshRateRegime = refreshRateManager.getRefreshRateRegime(); + + if (refreshRateRegime == RefreshRateManager::RefreshRateRegime::RUNNING || + refreshRateRegime == RefreshRateManager::RefreshRateRegime::INACTIVE) { + getRefreshRateManager().resetInactiveTimer(); + } + } + if (event->type() == QEvent::Leave) { getApplicationCompositor().handleLeaveEvent(); } @@ -4420,13 +4428,6 @@ void Application::keyReleaseEvent(QKeyEvent* event) { } -void Application::focusInEvent(QFocusEvent* event) { - if (!_aboutToQuit && _startUpFinished) { - getRefreshRateManager().setRefreshRateRegime(RefreshRateManager::RefreshRateRegime::RUNNING); - } -} - - void Application::focusOutEvent(QFocusEvent* event) { auto inputPlugins = PluginManager::getInstance()->getInputPlugins(); foreach(auto inputPlugin, inputPlugins) { @@ -4434,10 +4435,6 @@ void Application::focusOutEvent(QFocusEvent* event) { inputPlugin->pluginFocusOutEvent(); } } - - if (!_aboutToQuit && _startUpFinished) { - getRefreshRateManager().setRefreshRateRegime(RefreshRateManager::RefreshRateRegime::UNFOCUS); - } // FIXME spacemouse code still needs cleanup #if 0 //SpacemouseDevice::getInstance().focusOutEvent(); @@ -8549,11 +8546,20 @@ void Application::activeChanged(Qt::ApplicationState state) { switch (state) { case Qt::ApplicationActive: _isForeground = true; + if (!_aboutToQuit && _startUpFinished) { + getRefreshRateManager().setRefreshRateRegime(RefreshRateManager::RefreshRateRegime::RUNNING); + } break; case Qt::ApplicationSuspended: + break; case Qt::ApplicationHidden: + break; case Qt::ApplicationInactive: + if (!_aboutToQuit && _startUpFinished) { + getRefreshRateManager().setRefreshRateRegime(RefreshRateManager::RefreshRateRegime::UNFOCUS); + } + break; default: _isForeground = false; break; diff --git a/interface/src/RefreshRateManager.cpp b/interface/src/RefreshRateManager.cpp index c4a35da1f6..d3e0b102ab 100644 --- a/interface/src/RefreshRateManager.cpp +++ b/interface/src/RefreshRateManager.cpp @@ -26,7 +26,7 @@ static const std::array REFRESH_RATE_REGIME_TO_STRING = - { { "Running", "Unfocus", "Minimized", "StartUp", "ShutDown" } }; + { { "Running", "Unfocus", "Minimized", "StartUp", "ShutDown", "Inactive" } }; static const std::array UX_MODE_TO_STRING = { { "Desktop", "HMD" } }; @@ -36,21 +36,20 @@ static const std::map REFRE { "Interactive", RefreshRateManager::RefreshRateProfile::INTERACTIVE }, { "Realtime", RefreshRateManager::RefreshRateProfile::REALTIME } }; -static const std::array RUNNING_REGIME_PROFILES = - { { 5, 20, 60 } }; +static const std::array ECO_PROFILE = + { { 5, 5, 2, 30, 30, 4 } }; -static const std::array UNFOCUS_REGIME_PROFILES = - { { 5, 5, 10 } }; +static const std::array INTERACTIVE_PROFILE = + { { 25, 5, 2, 30, 30, 20 } }; -static const std::array MINIMIZED_REGIME_PROFILE = - { { 2, 2, 2 } }; +static const std::array REALTIME_PROFILE = + { { 60, 10, 2, 30, 30, 30} }; -static const std::array START_AND_SHUTDOWN_REGIME_PROFILES = - { { 30, 30, 30 } }; +static const std::array, RefreshRateManager::RefreshRateProfile::PROFILE_NUM> REFRESH_RATE_PROFILES = + { { ECO_PROFILE, INTERACTIVE_PROFILE, REALTIME_PROFILE } }; -static const std::array, RefreshRateManager::RefreshRateRegime::REGIME_NUM> REFRESH_RATE_REGIMES = - { { RUNNING_REGIME_PROFILES, UNFOCUS_REGIME_PROFILES, MINIMIZED_REGIME_PROFILE, - START_AND_SHUTDOWN_REGIME_PROFILES, START_AND_SHUTDOWN_REGIME_PROFILES } }; + +static const int INACTIVE_TIMER_LIMIT = 3000; std::string RefreshRateManager::refreshRateProfileToString(RefreshRateManager::RefreshRateProfile refreshRateProfile) { @@ -71,6 +70,21 @@ std::string RefreshRateManager::uxModeToString(RefreshRateManager::RefreshRateMa RefreshRateManager::RefreshRateManager() { _refreshRateProfile = (RefreshRateManager::RefreshRateProfile) _refreshRateMode.get(); + _inactiveTimer->setInterval(INACTIVE_TIMER_LIMIT); + _inactiveTimer->setSingleShot(true); + QObject::connect(_inactiveTimer.get(), &QTimer::timeout, [&] { + if (_uxMode == RefreshRateManager::UXMode::DESKTOP && + getRefreshRateRegime() == RefreshRateManager::RefreshRateRegime::RUNNING) { + setRefreshRateRegime(RefreshRateManager::RefreshRateRegime::INACTIVE); + } + }); +} + +void RefreshRateManager::resetInactiveTimer() { + if (_uxMode == RefreshRateManager::UXMode::DESKTOP) { + _inactiveTimer->start(); + setRefreshRateRegime(RefreshRateManager::RefreshRateRegime::RUNNING); + } } void RefreshRateManager::setRefreshRateProfile(RefreshRateManager::RefreshRateProfile refreshRateProfile) { @@ -123,7 +137,7 @@ void RefreshRateManager::updateRefreshRateController() const { _refreshRateProfile == RefreshRateManager::RefreshRateProfile::INTERACTIVE) { targetRefreshRate = getInteractiveRefreshRate(); } else { - targetRefreshRate = REFRESH_RATE_REGIMES[_refreshRateRegime][_refreshRateProfile]; + targetRefreshRate = REFRESH_RATE_PROFILES[_refreshRateProfile][_refreshRateRegime]; } } else { targetRefreshRate = HMD_TARGET_RATE; diff --git a/interface/src/RefreshRateManager.h b/interface/src/RefreshRateManager.h index ee7debe3ef..f28941e982 100644 --- a/interface/src/RefreshRateManager.h +++ b/interface/src/RefreshRateManager.h @@ -15,6 +15,8 @@ #include #include +#include + #include #include @@ -33,6 +35,7 @@ public: MINIMIZED, STARTUP, SHUTDOWN, + INACTIVE, REGIME_NUM }; @@ -60,6 +63,8 @@ public: void setInteractiveRefreshRate(int refreshRate); int getInteractiveRefreshRate() const; + void resetInactiveTimer(); + static std::string refreshRateProfileToString(RefreshRateProfile refreshRateProfile); static RefreshRateProfile refreshRateProfileFromString(std::string refreshRateProfile); static std::string uxModeToString(UXMode uxMode); @@ -78,6 +83,9 @@ private: Setting::Handle _refreshRateMode { "refreshRateProfile", INTERACTIVE }; std::function _refreshRateOperator { nullptr }; + + + std::shared_ptr _inactiveTimer { std::make_shared() }; }; #endif