From 3857c2ac2fa7ef07b044aebfcc0056d6bf9fb0b3 Mon Sep 17 00:00:00 2001 From: Vasily Date: Fri, 5 Jun 2020 19:04:06 +0200 Subject: [PATCH] Refactor update rates of tab controllers Previous way of controlling when controllers were run was split among the different controllers, meaning that third parties would require knowledge of internal state of controllers in order to be on the same update schedule as them. This refactor moves the logic into a seperate class. Additionally, it also stops the use of the `adjustUpdateRate` function on all tab controllers, since it broke the prime-number-ability of less collisions. --- build_scripts/qt/sources.pri | 4 + src/alarm_clock/vr_alarm.cpp | 7 + src/overlaycontroller.cpp | 3 +- src/overlaycontroller.h | 19 +- src/tabcontrollers/AudioTabController.cpp | 72 +++-- src/tabcontrollers/AudioTabController.h | 2 - src/tabcontrollers/ChaperoneTabController.cpp | 15 +- src/tabcontrollers/ChaperoneTabController.h | 2 - .../MoveCenterTabController.cpp | 2 - src/tabcontrollers/MoveCenterTabController.h | 2 - src/tabcontrollers/SettingsTabController.cpp | 17 +- src/tabcontrollers/SettingsTabController.h | 4 - src/tabcontrollers/SteamVRTabController.cpp | 273 +++++++++--------- src/tabcontrollers/SteamVRTabController.h | 4 - src/tabcontrollers/UtilitiesTabController.cpp | 135 ++++----- src/tabcontrollers/UtilitiesTabController.h | 3 - src/tabcontrollers/VideoTabController.cpp | 169 ++++++----- src/utils/update_rate.cpp | 57 ++++ src/utils/update_rate.h | 28 ++ 19 files changed, 423 insertions(+), 395 deletions(-) create mode 100644 src/utils/update_rate.cpp create mode 100644 src/utils/update_rate.h diff --git a/build_scripts/qt/sources.pri b/build_scripts/qt/sources.pri index a2e35e6..f56a721 100644 --- a/build_scripts/qt/sources.pri +++ b/build_scripts/qt/sources.pri @@ -23,6 +23,7 @@ SOURCES += src/main.cpp\ src/settings/settings.cpp \ src/settings/settings_object.cpp \ src/alarm_clock/vr_alarm.cpp \ + src/utils/update_rate.cpp \ @@ -64,6 +65,9 @@ HEADERS += src/overlaycontroller.h \ src/settings/settings_object.h \ src/settings/internal/settings_object_data.h \ src/alarm_clock/vr_alarm.h \ + src/settings/internal/settings_object_data.h \ + src/settings/internal/settings_object_data.h \ + src/utils/update_rate.h \ win32 { diff --git a/src/alarm_clock/vr_alarm.cpp b/src/alarm_clock/vr_alarm.cpp index 852650c..35e9696 100644 --- a/src/alarm_clock/vr_alarm.cpp +++ b/src/alarm_clock/vr_alarm.cpp @@ -2,6 +2,7 @@ #include "../tabcontrollers/UtilitiesTabController.h" #include "vr_alarm.h" #include "../openvr/ovr_overlay_wrapper.h" +#include "../utils/update_rate.h" namespace alarm_clock { @@ -36,6 +37,12 @@ VrAlarm::VrAlarm() void VrAlarm::eventLoopTick() { + if ( updateRate.shouldSubjectNotRun( + UpdateSubject::UtilitiesTabController ) ) + { + return; + } + if ( alarmEnabled() && m_alarm.isValid() ) { checkAlarmStatus(); diff --git a/src/overlaycontroller.cpp b/src/overlaycontroller.cpp index a634f79..d8aa04d 100644 --- a/src/overlaycontroller.cpp +++ b/src/overlaycontroller.cpp @@ -150,7 +150,6 @@ OverlayController::OverlayController( bool desktopMode, m_moveCenterTabController.initStage1(); m_audioTabController.initStage1(); m_settingsTabController.initStage1(); - m_utilitiesTabController.initStage1(); m_videoTabController.initStage1(); m_rotationTabController.initStage1(); @@ -958,6 +957,8 @@ void OverlayController::OnTimeoutPumpEvents() m_vsyncTooLateCounter++; } } + + updateRate.incrementCounter(); } void OverlayController::mainEventLoop() diff --git a/src/overlaycontroller.h b/src/overlaycontroller.h index cb2bc20..4908f4f 100644 --- a/src/overlaycontroller.h +++ b/src/overlaycontroller.h @@ -44,6 +44,8 @@ #include "alarm_clock/vr_alarm.h" +#include "utils/update_rate.h" + namespace application_strings { constexpr auto applicationOrganizationName = "AdvancedSettings-Team"; @@ -61,23 +63,6 @@ constexpr const char* applicationVersionString = APPLICATION_VERSION; // application namespace namespace advsettings { -// These counters set timing for refreshing settings changes in tab ui -// SettingsUpdateCounter values are set as prime numbers to reduce overlap -// of simultaneous settings updates. -// Actual rates of updates are counter * vsync (~11ms) -// Values chosen based on update speed priority -// Avoid setting values to the same numbers. -constexpr int k_audioSettingsUpdateCounter = 89; -constexpr int k_chaperoneSettingsUpdateCounter = 101; -constexpr int k_moveCenterSettingsUpdateCounter = 83; -constexpr int k_settingsTabSettingsUpdateCounter = 157; -constexpr int k_steamVrSettingsUpdateCounter = 97; -constexpr int k_utilitiesSettingsUpdateCounter = 19; - -// Dashboard Updates These can be faster as they should only be running while -// Dashboard is active. -constexpr int k_videoDashboardUpdateCounter = 47; - // k_nonVsyncTickRate determines number of ms we wait to force the next event // loop tick when vsync is too late due to dropped frames. constexpr int k_nonVsyncTickRate = 20; diff --git a/src/tabcontrollers/AudioTabController.cpp b/src/tabcontrollers/AudioTabController.cpp index 94b4a92..f979379 100644 --- a/src/tabcontrollers/AudioTabController.cpp +++ b/src/tabcontrollers/AudioTabController.cpp @@ -4,6 +4,7 @@ #include "../overlaycontroller.h" #include "../settings/settings.h" #include "../settings/settings_object.h" +#include "../utils/update_rate.h" #ifdef _WIN32 # include "audiomanager/AudioManagerWindows.h" #elif __linux__ @@ -26,8 +27,6 @@ void AudioTabController::initStage1() audioManager.reset( new AudioManagerDummy() ); #endif audioManager->init( this ); - m_audioSettingsUpdateCounter - = utils::adjustUpdateRate( k_audioSettingsUpdateCounter ); initOverride(); m_playbackDevices = audioManager->getPlaybackDevices(); m_recordingDevices = audioManager->getRecordingDevices(); @@ -190,51 +189,48 @@ bool AudioTabController::audioProfileDefault() const void AudioTabController::eventLoopTick() { + if ( updateRate.shouldSubjectNotRun( UpdateSubject::AudioTabController ) ) + { + return; + } + if ( !eventLoopMutex.try_lock() ) { return; } - if ( settingsUpdateCounter >= m_audioSettingsUpdateCounter ) + vr::EVRSettingsError vrSettingsError; + char mirrorDeviceId[1024]; + vr::VRSettings()->GetString( vr::k_pch_audio_Section, + vr::k_pch_audio_PlaybackMirrorDevice_String, + mirrorDeviceId, + 1024, + &vrSettingsError ); + if ( vrSettingsError != vr::VRSettingsError_None ) { - vr::EVRSettingsError vrSettingsError; - char mirrorDeviceId[1024]; - vr::VRSettings()->GetString( - vr::k_pch_audio_Section, - vr::k_pch_audio_PlaybackMirrorDevice_String, - mirrorDeviceId, - 1024, - &vrSettingsError ); - if ( vrSettingsError != vr::VRSettingsError_None ) - { - LOG( WARNING ) << "Could not read \"" - << vr::k_pch_audio_PlaybackMirrorDevice_String - << "\" setting: " - << vr::VRSettings()->GetSettingsErrorNameFromEnum( - vrSettingsError ); - } - if ( lastMirrorDevId.compare( mirrorDeviceId ) != 0 ) - { - audioManager->setMirrorDevice( mirrorDeviceId ); - findMirrorDeviceIndex( audioManager->getMirrorDevId() ); - lastMirrorDevId = mirrorDeviceId; - } - if ( m_mirrorDeviceIndex >= 0 ) - { - setMirrorVolume( audioManager->getMirrorVolume() ); - setMirrorMuted( audioManager->getMirrorMuted() ); - } - if ( m_recordingDeviceIndex >= 0 ) - { - setMicVolume( audioManager->getMicVolume() ); - setMicMuted( audioManager->getMicMuted() ); - } - settingsUpdateCounter = 0; + LOG( WARNING ) << "Could not read \"" + << vr::k_pch_audio_PlaybackMirrorDevice_String + << "\" setting: " + << vr::VRSettings()->GetSettingsErrorNameFromEnum( + vrSettingsError ); } - else + if ( lastMirrorDevId.compare( mirrorDeviceId ) != 0 ) { - settingsUpdateCounter++; + audioManager->setMirrorDevice( mirrorDeviceId ); + findMirrorDeviceIndex( audioManager->getMirrorDevId() ); + lastMirrorDevId = mirrorDeviceId; } + if ( m_mirrorDeviceIndex >= 0 ) + { + setMirrorVolume( audioManager->getMirrorVolume() ); + setMirrorMuted( audioManager->getMirrorMuted() ); + } + if ( m_recordingDeviceIndex >= 0 ) + { + setMicVolume( audioManager->getMicVolume() ); + setMicMuted( audioManager->getMicMuted() ); + } + eventLoopMutex.unlock(); } diff --git a/src/tabcontrollers/AudioTabController.h b/src/tabcontrollers/AudioTabController.h index 4fcb4bd..7729a0f 100644 --- a/src/tabcontrollers/AudioTabController.h +++ b/src/tabcontrollers/AudioTabController.h @@ -131,8 +131,6 @@ private: bool m_isPlaybackOverride = false; bool m_isRecordingOverride = false; - unsigned int m_audioSettingsUpdateCounter = 90; - int m_defaultProfileIndex = -1; unsigned settingsUpdateCounter = 0; diff --git a/src/tabcontrollers/ChaperoneTabController.cpp b/src/tabcontrollers/ChaperoneTabController.cpp index 22e5b4e..405a2f5 100644 --- a/src/tabcontrollers/ChaperoneTabController.cpp +++ b/src/tabcontrollers/ChaperoneTabController.cpp @@ -4,6 +4,7 @@ #include "../settings/settings.h" #include "../utils/Matrix.h" #include "../quaternion/quaternion.h" +#include "../utils/update_rate.h" #include // application namespace @@ -18,8 +19,6 @@ void ChaperoneTabController::initStage1() } reloadChaperoneProfiles(); - m_chaperoneSettingsUpdateCounter - = utils::adjustUpdateRate( k_chaperoneSettingsUpdateCounter ); initCenterMarkerOverlay(); eventLoopTick( m_trackingUniverse, nullptr ); } @@ -42,15 +41,13 @@ void ChaperoneTabController::initStage2( OverlayController* var_parent ) void ChaperoneTabController::dashboardLoopTick() { - if ( settingsUpdateCounter >= m_chaperoneSettingsUpdateCounter ) + if ( updateRate.shouldSubjectNotRun( + UpdateSubject::ChaperoneTabController ) ) { - updateChaperoneSettings(); - settingsUpdateCounter = 0; - } - else - { - settingsUpdateCounter++; + return; } + + updateChaperoneSettings(); } ChaperoneTabController::~ChaperoneTabController() diff --git a/src/tabcontrollers/ChaperoneTabController.h b/src/tabcontrollers/ChaperoneTabController.h index 96e89bb..231d04c 100644 --- a/src/tabcontrollers/ChaperoneTabController.h +++ b/src/tabcontrollers/ChaperoneTabController.h @@ -363,8 +363,6 @@ private: int m_updateTicksChaperoneReload = 0; - unsigned int m_chaperoneSettingsUpdateCounter = 101; - vr::VRActionHandle_t m_rightActionHandle; vr::VRActionHandle_t m_leftActionHandle; vr::VRInputValueHandle_t m_leftInputHandle; diff --git a/src/tabcontrollers/MoveCenterTabController.cpp b/src/tabcontrollers/MoveCenterTabController.cpp index 1a67b14..e037319 100644 --- a/src/tabcontrollers/MoveCenterTabController.cpp +++ b/src/tabcontrollers/MoveCenterTabController.cpp @@ -39,8 +39,6 @@ namespace advsettings void MoveCenterTabController::initStage1() { reloadOffsetProfiles(); - m_moveCenterSettingsUpdateCounter - = utils::adjustUpdateRate( k_moveCenterSettingsUpdateCounter ); m_lastDragUpdateTimePoint = std::chrono::steady_clock::now(); m_lastGravityUpdateTimePoint = std::chrono::steady_clock::now(); } diff --git a/src/tabcontrollers/MoveCenterTabController.h b/src/tabcontrollers/MoveCenterTabController.h index 3d5ab9b..5e7844e 100644 --- a/src/tabcontrollers/MoveCenterTabController.h +++ b/src/tabcontrollers/MoveCenterTabController.h @@ -204,8 +204,6 @@ private: unsigned m_dragComfortFrameSkipCounter = 0; unsigned m_turnComfortFrameSkipCounter = 0; - unsigned int m_moveCenterSettingsUpdateCounter = 83; - // Matrix used For Center Marker vr::HmdMatrix34_t m_offsetmatrix = utils::k_forwardUpMatrix; diff --git a/src/tabcontrollers/SettingsTabController.cpp b/src/tabcontrollers/SettingsTabController.cpp index 5e3c5a4..9864513 100644 --- a/src/tabcontrollers/SettingsTabController.cpp +++ b/src/tabcontrollers/SettingsTabController.cpp @@ -1,6 +1,7 @@ #include "SettingsTabController.h" #include #include "../overlaycontroller.h" +#include "../utils/update_rate.h" // application namespace namespace advsettings @@ -9,8 +10,6 @@ void SettingsTabController::initStage1() { m_autoStartEnabled = vr::VRApplications()->GetApplicationAutoLaunch( application_strings::applicationKey ); - m_settingsTabSettingsUpdateCounter - = utils::adjustUpdateRate( k_settingsTabSettingsUpdateCounter ); } void SettingsTabController::initStage2( OverlayController* var_parent ) @@ -22,16 +21,14 @@ void SettingsTabController::initStage2( OverlayController* var_parent ) void SettingsTabController::dashboardLoopTick() { - if ( settingsUpdateCounter >= m_settingsTabSettingsUpdateCounter ) + if ( updateRate.shouldSubjectNotRun( + UpdateSubject::SettingsTabController ) ) { - setAutoStartEnabled( vr::VRApplications()->GetApplicationAutoLaunch( - application_strings::applicationKey ) ); - settingsUpdateCounter = 0; - } - else - { - settingsUpdateCounter++; + return; } + + setAutoStartEnabled( vr::VRApplications()->GetApplicationAutoLaunch( + application_strings::applicationKey ) ); } bool SettingsTabController::autoStartEnabled() const diff --git a/src/tabcontrollers/SettingsTabController.h b/src/tabcontrollers/SettingsTabController.h index d1ff682..e22a08d 100644 --- a/src/tabcontrollers/SettingsTabController.h +++ b/src/tabcontrollers/SettingsTabController.h @@ -26,13 +26,9 @@ class SettingsTabController : public QObject private: OverlayController* parent; - unsigned settingsUpdateCounter = 0; - bool m_autoStartEnabled = false; bool m_nativeChaperoneToggle = false; - unsigned int m_settingsTabSettingsUpdateCounter = 157; - public: void initStage1(); void initStage2( OverlayController* parent ); diff --git a/src/tabcontrollers/SteamVRTabController.cpp b/src/tabcontrollers/SteamVRTabController.cpp index 19395d1..7ccf7d3 100644 --- a/src/tabcontrollers/SteamVRTabController.cpp +++ b/src/tabcontrollers/SteamVRTabController.cpp @@ -1,14 +1,13 @@ #include "SteamVRTabController.h" #include #include "../overlaycontroller.h" +#include "../utils/update_rate.h" // application namespace namespace advsettings { void SteamVRTabController::initStage1() { - m_steamVrSettingsUpdateCounter - = utils::adjustUpdateRate( k_steamVrSettingsUpdateCounter ); dashboardLoopTick(); } @@ -19,149 +18,141 @@ void SteamVRTabController::initStage2( OverlayController* var_parent ) void SteamVRTabController::dashboardLoopTick() { - if ( settingsUpdateCounter >= m_steamVrSettingsUpdateCounter ) + if ( updateRate.shouldSubjectNotRun( UpdateSubject::SteamVrTabController ) ) { - vr::EVRSettingsError vrSettingsError; - - // Checks and synchs performance graph - auto pg = vr::VRSettings()->GetBool( vr::k_pch_Perf_Section, - vr::k_pch_Perf_PerfGraphInHMD_Bool, - &vrSettingsError ); - if ( vrSettingsError != vr::VRSettingsError_None ) - { - LOG( WARNING ) << "Could not read \"" - << vr::k_pch_Perf_PerfGraphInHMD_Bool - << "\" setting: " - << vr::VRSettings()->GetSettingsErrorNameFromEnum( - vrSettingsError ); - } - setPerformanceGraph( pg ); - - // synch systembutton - auto sb = vr::VRSettings()->GetBool( - vr::k_pch_SteamVR_Section, - vr::k_pch_SteamVR_SendSystemButtonToAllApps_Bool, - &vrSettingsError ); - if ( vrSettingsError != vr::VRSettingsError_None ) - { - LOG( WARNING ) << "Could not read \"" - << vr::k_pch_SteamVR_SendSystemButtonToAllApps_Bool - << "\" setting: " - << vr::VRSettings()->GetSettingsErrorNameFromEnum( - vrSettingsError ); - } - setSystemButton( sb ); - - // synch nofadetogrid - auto nf = vr::VRSettings()->GetBool( vr::k_pch_SteamVR_Section, - vr::k_pch_SteamVR_DoNotFadeToGrid, - &vrSettingsError ); - if ( vrSettingsError != vr::VRSettingsError_None ) - { - LOG( WARNING ) << "Could not read \"" - << vr::k_pch_SteamVR_DoNotFadeToGrid - << "\" setting: " - << vr::VRSettings()->GetSettingsErrorNameFromEnum( - vrSettingsError ); - } - setNoFadeToGrid( nf ); - - // synch multipleDriver - auto md = vr::VRSettings()->GetBool( - vr::k_pch_SteamVR_Section, - vr::k_pch_SteamVR_ActivateMultipleDrivers_Bool, - &vrSettingsError ); - if ( vrSettingsError != vr::VRSettingsError_None ) - { - LOG( WARNING ) << "Could not read \"" - << vr::k_pch_SteamVR_ActivateMultipleDrivers_Bool - << "\" setting: " - << vr::VRSettings()->GetSettingsErrorNameFromEnum( - vrSettingsError ); - } - setMultipleDriver( md ); - - // synch dnd - auto donotd = vr::VRSettings()->GetBool( - vr::k_pch_Notifications_Section, - vr::k_pch_Notifications_DoNotDisturb_Bool, - &vrSettingsError ); - if ( vrSettingsError != vr::VRSettingsError_None ) - { - LOG( WARNING ) << "Could not read \"" - << vr::k_pch_Notifications_DoNotDisturb_Bool - << "\" setting: " - << vr::VRSettings()->GetSettingsErrorNameFromEnum( - vrSettingsError ); - } - setDND( donotd ); - - // synch camera - auto ca = vr::VRSettings()->GetBool( vr::k_pch_Camera_Section, - vr::k_pch_Camera_EnableCamera_Bool, - &vrSettingsError ); - if ( vrSettingsError != vr::VRSettingsError_None ) - { - LOG( WARNING ) << "Could not read \"" - << vr::k_pch_Camera_EnableCamera_Bool - << "\" setting: " - << vr::VRSettings()->GetSettingsErrorNameFromEnum( - vrSettingsError ); - } - setCameraActive( ca ); - - // synch camera room - auto cr = vr::VRSettings()->GetBool( - vr::k_pch_Camera_Section, - vr::k_pch_Camera_EnableCameraForRoomView_Bool, - &vrSettingsError ); - if ( vrSettingsError != vr::VRSettingsError_None ) - { - LOG( WARNING ) << "Could not read \"" - << vr::k_pch_Camera_EnableCameraForRoomView_Bool - << "\" setting: " - << vr::VRSettings()->GetSettingsErrorNameFromEnum( - vrSettingsError ); - } - setCameraRoom( cr ); - - // synch camera dashboard - auto cd = vr::VRSettings()->GetBool( - vr::k_pch_Camera_Section, - vr::k_pch_Camera_EnableCameraInDashboard_Bool, - &vrSettingsError ); - if ( vrSettingsError != vr::VRSettingsError_None ) - { - LOG( WARNING ) << "Could not read \"" - << vr::k_pch_Camera_EnableCameraInDashboard_Bool - << "\" setting: " - << vr::VRSettings()->GetSettingsErrorNameFromEnum( - vrSettingsError ); - } - setCameraDashboard( cd ); - - // synch camera bounds - auto cb = vr::VRSettings()->GetBool( - vr::k_pch_Camera_Section, - vr::k_pch_Camera_EnableCameraForCollisionBounds_Bool, - &vrSettingsError ); - if ( vrSettingsError != vr::VRSettingsError_None ) - { - LOG( WARNING ) - << "Could not read \"" - << vr::k_pch_Camera_EnableCameraForCollisionBounds_Bool - << "\" setting: " - << vr::VRSettings()->GetSettingsErrorNameFromEnum( - vrSettingsError ); - } - setCameraBounds( cb ); - - settingsUpdateCounter = 0; + return; } - else + + vr::EVRSettingsError vrSettingsError; + + // Checks and synchs performance graph + auto pg = vr::VRSettings()->GetBool( vr::k_pch_Perf_Section, + vr::k_pch_Perf_PerfGraphInHMD_Bool, + &vrSettingsError ); + if ( vrSettingsError != vr::VRSettingsError_None ) { - settingsUpdateCounter++; + LOG( WARNING ) << "Could not read \"" + << vr::k_pch_Perf_PerfGraphInHMD_Bool << "\" setting: " + << vr::VRSettings()->GetSettingsErrorNameFromEnum( + vrSettingsError ); } + setPerformanceGraph( pg ); + + // synch systembutton + auto sb = vr::VRSettings()->GetBool( + vr::k_pch_SteamVR_Section, + vr::k_pch_SteamVR_SendSystemButtonToAllApps_Bool, + &vrSettingsError ); + if ( vrSettingsError != vr::VRSettingsError_None ) + { + LOG( WARNING ) << "Could not read \"" + << vr::k_pch_SteamVR_SendSystemButtonToAllApps_Bool + << "\" setting: " + << vr::VRSettings()->GetSettingsErrorNameFromEnum( + vrSettingsError ); + } + setSystemButton( sb ); + + // synch nofadetogrid + auto nf = vr::VRSettings()->GetBool( vr::k_pch_SteamVR_Section, + vr::k_pch_SteamVR_DoNotFadeToGrid, + &vrSettingsError ); + if ( vrSettingsError != vr::VRSettingsError_None ) + { + LOG( WARNING ) << "Could not read \"" + << vr::k_pch_SteamVR_DoNotFadeToGrid << "\" setting: " + << vr::VRSettings()->GetSettingsErrorNameFromEnum( + vrSettingsError ); + } + setNoFadeToGrid( nf ); + + // synch multipleDriver + auto md = vr::VRSettings()->GetBool( + vr::k_pch_SteamVR_Section, + vr::k_pch_SteamVR_ActivateMultipleDrivers_Bool, + &vrSettingsError ); + if ( vrSettingsError != vr::VRSettingsError_None ) + { + LOG( WARNING ) << "Could not read \"" + << vr::k_pch_SteamVR_ActivateMultipleDrivers_Bool + << "\" setting: " + << vr::VRSettings()->GetSettingsErrorNameFromEnum( + vrSettingsError ); + } + setMultipleDriver( md ); + + // synch dnd + auto donotd + = vr::VRSettings()->GetBool( vr::k_pch_Notifications_Section, + vr::k_pch_Notifications_DoNotDisturb_Bool, + &vrSettingsError ); + if ( vrSettingsError != vr::VRSettingsError_None ) + { + LOG( WARNING ) << "Could not read \"" + << vr::k_pch_Notifications_DoNotDisturb_Bool + << "\" setting: " + << vr::VRSettings()->GetSettingsErrorNameFromEnum( + vrSettingsError ); + } + setDND( donotd ); + + // synch camera + auto ca = vr::VRSettings()->GetBool( vr::k_pch_Camera_Section, + vr::k_pch_Camera_EnableCamera_Bool, + &vrSettingsError ); + if ( vrSettingsError != vr::VRSettingsError_None ) + { + LOG( WARNING ) << "Could not read \"" + << vr::k_pch_Camera_EnableCamera_Bool << "\" setting: " + << vr::VRSettings()->GetSettingsErrorNameFromEnum( + vrSettingsError ); + } + setCameraActive( ca ); + + // synch camera room + auto cr = vr::VRSettings()->GetBool( + vr::k_pch_Camera_Section, + vr::k_pch_Camera_EnableCameraForRoomView_Bool, + &vrSettingsError ); + if ( vrSettingsError != vr::VRSettingsError_None ) + { + LOG( WARNING ) << "Could not read \"" + << vr::k_pch_Camera_EnableCameraForRoomView_Bool + << "\" setting: " + << vr::VRSettings()->GetSettingsErrorNameFromEnum( + vrSettingsError ); + } + setCameraRoom( cr ); + + // synch camera dashboard + auto cd = vr::VRSettings()->GetBool( + vr::k_pch_Camera_Section, + vr::k_pch_Camera_EnableCameraInDashboard_Bool, + &vrSettingsError ); + if ( vrSettingsError != vr::VRSettingsError_None ) + { + LOG( WARNING ) << "Could not read \"" + << vr::k_pch_Camera_EnableCameraInDashboard_Bool + << "\" setting: " + << vr::VRSettings()->GetSettingsErrorNameFromEnum( + vrSettingsError ); + } + setCameraDashboard( cd ); + + // synch camera bounds + auto cb = vr::VRSettings()->GetBool( + vr::k_pch_Camera_Section, + vr::k_pch_Camera_EnableCameraForCollisionBounds_Bool, + &vrSettingsError ); + if ( vrSettingsError != vr::VRSettingsError_None ) + { + LOG( WARNING ) << "Could not read \"" + << vr::k_pch_Camera_EnableCameraForCollisionBounds_Bool + << "\" setting: " + << vr::VRSettings()->GetSettingsErrorNameFromEnum( + vrSettingsError ); + } + setCameraBounds( cb ); } bool SteamVRTabController::performanceGraph() const diff --git a/src/tabcontrollers/SteamVRTabController.h b/src/tabcontrollers/SteamVRTabController.h index f47474b..4010e96 100644 --- a/src/tabcontrollers/SteamVRTabController.h +++ b/src/tabcontrollers/SteamVRTabController.h @@ -49,10 +49,6 @@ private: bool m_cameraRoom = false; bool m_cameraDashboard = false; - unsigned settingsUpdateCounter = 0; - - unsigned int m_steamVrSettingsUpdateCounter = 97; - public: void initStage1(); void initStage2( OverlayController* parent ); diff --git a/src/tabcontrollers/UtilitiesTabController.cpp b/src/tabcontrollers/UtilitiesTabController.cpp index 393dc7e..a8602af 100644 --- a/src/tabcontrollers/UtilitiesTabController.cpp +++ b/src/tabcontrollers/UtilitiesTabController.cpp @@ -4,18 +4,13 @@ #include "../overlaycontroller.h" #include "../keyboard_input/input_sender.h" #include "../settings/settings.h" +#include "../utils/update_rate.h" #include #include // application namespace namespace advsettings { -void UtilitiesTabController::initStage1() -{ - m_utilitiesSettingsUpdateCounter - = utils::adjustUpdateRate( k_utilitiesSettingsUpdateCounter ); -} - void UtilitiesTabController::initStage2( OverlayController* var_parent ) { this->m_parent = var_parent; @@ -270,87 +265,81 @@ vr::VROverlayHandle_t createBatteryOverlay( vr::TrackedDeviceIndex_t index ) void UtilitiesTabController::eventLoopTick() { - if ( settingsUpdateCounter >= m_utilitiesSettingsUpdateCounter ) + if ( updateRate.shouldSubjectNotRun( + UpdateSubject::UtilitiesTabController ) ) { - // attach battery overlay to all tracked devices that aren't a - // controller or hmd - for ( vr::TrackedDeviceIndex_t i = 0; i < vr::k_unMaxTrackedDeviceCount; - i++ ) + return; + } + + // attach battery overlay to all tracked devices that aren't a + // controller or hmd + for ( vr::TrackedDeviceIndex_t i = 0; i < vr::k_unMaxTrackedDeviceCount; + i++ ) + { + vr::ETrackedDeviceClass deviceClass + = vr::VRSystem()->GetTrackedDeviceClass( i ); + if ( deviceClass + == vr::ETrackedDeviceClass::TrackedDeviceClass_GenericTracker ) { - vr::ETrackedDeviceClass deviceClass - = vr::VRSystem()->GetTrackedDeviceClass( i ); - if ( deviceClass - == vr::ETrackedDeviceClass::TrackedDeviceClass_GenericTracker ) + if ( m_batteryOverlayHandles[i] == 0 ) { - if ( m_batteryOverlayHandles[i] == 0 ) + LOG( INFO ) << "Creating battery overlay for device " << i; + m_batteryOverlayHandles[i] = createBatteryOverlay( i ); + m_batteryVisible[i] = true; + } + + bool shouldShow = vr::VROverlay()->IsDashboardVisible(); + + if ( shouldShow != m_batteryVisible[i] ) + { + if ( shouldShow ) { - LOG( INFO ) << "Creating battery overlay for device " << i; - m_batteryOverlayHandles[i] = createBatteryOverlay( i ); - m_batteryVisible[i] = true; + vr::VROverlay()->ShowOverlay( m_batteryOverlayHandles[i] ); } - - bool shouldShow = vr::VROverlay()->IsDashboardVisible(); - - if ( shouldShow != m_batteryVisible[i] ) + else { - if ( shouldShow ) + vr::VROverlay()->HideOverlay( m_batteryOverlayHandles[i] ); + } + m_batteryVisible[i] = shouldShow; + } + + bool hasBatteryStatus + = vr::VRSystem()->GetBoolTrackedDeviceProperty( + i, + vr::ETrackedDeviceProperty:: + Prop_DeviceProvidesBatteryStatus_Bool ); + if ( hasBatteryStatus ) + { + float battery = vr::VRSystem()->GetFloatTrackedDeviceProperty( + i, + vr::ETrackedDeviceProperty:: + Prop_DeviceBatteryPercentage_Float ); + int batteryState = static_cast( + ceil( static_cast( battery * 5 ) ) ); + + if ( batteryState != m_batteryState[i] ) + { + LOG( INFO ) + << "Updating battery overlay for device " << i << " to " + << batteryState << "(" << battery << ")" + << QString::number( m_batteryOverlayHandles[i] ); + QString batteryIconPath + = getBatteryIconPath( batteryState ); + if ( QFile::exists( batteryIconPath ) ) { - vr::VROverlay()->ShowOverlay( - m_batteryOverlayHandles[i] ); + vr::VROverlay()->SetOverlayFromFile( + m_batteryOverlayHandles[i], + batteryIconPath.toStdString().c_str() ); } else { - vr::VROverlay()->HideOverlay( - m_batteryOverlayHandles[i] ); - } - m_batteryVisible[i] = shouldShow; - } - - bool hasBatteryStatus - = vr::VRSystem()->GetBoolTrackedDeviceProperty( - i, - vr::ETrackedDeviceProperty:: - Prop_DeviceProvidesBatteryStatus_Bool ); - if ( hasBatteryStatus ) - { - float battery - = vr::VRSystem()->GetFloatTrackedDeviceProperty( - i, - vr::ETrackedDeviceProperty:: - Prop_DeviceBatteryPercentage_Float ); - int batteryState = static_cast( - ceil( static_cast( battery * 5 ) ) ); - - if ( batteryState != m_batteryState[i] ) - { - LOG( INFO ) - << "Updating battery overlay for device " << i - << " to " << batteryState << "(" << battery << ")" - << QString::number( m_batteryOverlayHandles[i] ); - QString batteryIconPath - = getBatteryIconPath( batteryState ); - if ( QFile::exists( batteryIconPath ) ) - { - vr::VROverlay()->SetOverlayFromFile( - m_batteryOverlayHandles[i], - batteryIconPath.toStdString().c_str() ); - } - else - { - LOG( ERROR ) << "Could not find battery icon \"" - << batteryIconPath << "\""; - } - m_batteryState[i] = batteryState; + LOG( ERROR ) << "Could not find battery icon \"" + << batteryIconPath << "\""; } + m_batteryState[i] = batteryState; } } } - - settingsUpdateCounter = 0; - } - else - { - settingsUpdateCounter++; } } diff --git a/src/tabcontrollers/UtilitiesTabController.h b/src/tabcontrollers/UtilitiesTabController.h index 4b0affd..d21b18f 100644 --- a/src/tabcontrollers/UtilitiesTabController.h +++ b/src/tabcontrollers/UtilitiesTabController.h @@ -32,10 +32,7 @@ private: int m_batteryState[vr::k_unMaxTrackedDeviceCount]; bool m_batteryVisible[vr::k_unMaxTrackedDeviceCount]; - unsigned int m_utilitiesSettingsUpdateCounter = 19; - public: - void initStage1(); void initStage2( OverlayController* var_parent ); void eventLoopTick(); diff --git a/src/tabcontrollers/VideoTabController.cpp b/src/tabcontrollers/VideoTabController.cpp index 374be47..fecb515 100644 --- a/src/tabcontrollers/VideoTabController.cpp +++ b/src/tabcontrollers/VideoTabController.cpp @@ -3,6 +3,7 @@ #include #include "../settings/settings.h" #include "../overlaycontroller.h" +#include "../utils/update_rate.h" #include namespace advsettings @@ -15,9 +16,6 @@ void VideoTabController::initStage1() synchGain( true ); resetGain(); - m_videoDashboardUpdateCounter - = utils::adjustUpdateRate( k_videoDashboardUpdateCounter ); - initBrightnessOverlay(); initColorOverlay(); initMotionSmoothing(); @@ -141,101 +139,98 @@ void VideoTabController::eventLoopTick() {} void VideoTabController::dashboardLoopTick() { - if ( settingsUpdateCounter >= m_videoDashboardUpdateCounter ) + if ( updateRate.shouldSubjectNotRun( UpdateSubject::VideoDashboard ) ) { - vr::EVRSettingsError vrSettingsError; + return; + } - // checks supersampling override and resynchs if necessry - // also prints error if can't find. - auto sso = vr::VRSettings()->GetBool( - vr::k_pch_SteamVR_Section, - vr::k_pch_SteamVR_SupersampleManualOverride_Bool, - &vrSettingsError ); - if ( vrSettingsError != vr::VRSettingsError_None ) - { - LOG( WARNING ) << "Could not read \"" - << vr::k_pch_SteamVR_SupersampleManualOverride_Bool - << "\" setting: " - << vr::VRSettings()->GetSettingsErrorNameFromEnum( - vrSettingsError ); - } + vr::EVRSettingsError vrSettingsError; - setAllowSupersampleOverride( sso ); - // checks supersampling and re-synchs if necessary - auto ss = vr::VRSettings()->GetFloat( - vr::k_pch_SteamVR_Section, - vr::k_pch_SteamVR_SupersampleScale_Float, - &vrSettingsError ); - if ( vrSettingsError != vr::VRSettingsError_None ) - { - LOG( WARNING ) << "Could not read \"" - << vr::k_pch_SteamVR_SupersampleScale_Float - << "\" setting: " - << vr::VRSettings()->GetSettingsErrorNameFromEnum( - vrSettingsError ); - if ( m_superSampling != 1.0f ) - { - LOG( DEBUG ) << "OpenVR returns an error and we have a custom " - "supersampling value: " - << m_superSampling; - setSuperSampling( 1.0 ); - } - } - else if ( fabs( static_cast( m_superSampling - ss ) ) > 0.05 ) - { - LOG( INFO ) << "OpenVR reports a changed supersampling value: " - << m_superSampling << " => " << ss; - setSuperSampling( ss ); - } - // checks if Supersampling filter is on and changes if it has been - // changed elsewhere + // checks supersampling override and resynchs if necessry + // also prints error if can't find. + auto sso = vr::VRSettings()->GetBool( + vr::k_pch_SteamVR_Section, + vr::k_pch_SteamVR_SupersampleManualOverride_Bool, + &vrSettingsError ); + if ( vrSettingsError != vr::VRSettingsError_None ) + { + LOG( WARNING ) << "Could not read \"" + << vr::k_pch_SteamVR_SupersampleManualOverride_Bool + << "\" setting: " + << vr::VRSettings()->GetSettingsErrorNameFromEnum( + vrSettingsError ); + } - auto sf = vr::VRSettings()->GetBool( - vr::k_pch_SteamVR_Section, - vr::k_pch_SteamVR_AllowSupersampleFiltering_Bool, - &vrSettingsError ); - if ( vrSettingsError != vr::VRSettingsError_None ) + setAllowSupersampleOverride( sso ); + // checks supersampling and re-synchs if necessary + auto ss + = vr::VRSettings()->GetFloat( vr::k_pch_SteamVR_Section, + vr::k_pch_SteamVR_SupersampleScale_Float, + &vrSettingsError ); + if ( vrSettingsError != vr::VRSettingsError_None ) + { + LOG( WARNING ) << "Could not read \"" + << vr::k_pch_SteamVR_SupersampleScale_Float + << "\" setting: " + << vr::VRSettings()->GetSettingsErrorNameFromEnum( + vrSettingsError ); + if ( m_superSampling != 1.0f ) { - LOG( WARNING ) << "Could not read \"" - << vr::k_pch_SteamVR_AllowSupersampleFiltering_Bool - << "\" setting: " - << vr::VRSettings()->GetSettingsErrorNameFromEnum( - vrSettingsError ); + LOG( DEBUG ) << "OpenVR returns an error and we have a custom " + "supersampling value: " + << m_superSampling; + setSuperSampling( 1.0 ); } - setAllowSupersampleFiltering( sf ); + } + else if ( fabs( static_cast( m_superSampling - ss ) ) > 0.05 ) + { + LOG( INFO ) << "OpenVR reports a changed supersampling value: " + << m_superSampling << " => " << ss; + setSuperSampling( ss ); + } + // checks if Supersampling filter is on and changes if it has been + // changed elsewhere - // Checks if Motion smoothing setting can be read and synchs adv - // settings to steamvr/openvr - auto ms - = vr::VRSettings()->GetBool( vr::k_pch_SteamVR_Section, + auto sf = vr::VRSettings()->GetBool( + vr::k_pch_SteamVR_Section, + vr::k_pch_SteamVR_AllowSupersampleFiltering_Bool, + &vrSettingsError ); + if ( vrSettingsError != vr::VRSettingsError_None ) + { + LOG( WARNING ) << "Could not read \"" + << vr::k_pch_SteamVR_AllowSupersampleFiltering_Bool + << "\" setting: " + << vr::VRSettings()->GetSettingsErrorNameFromEnum( + vrSettingsError ); + } + setAllowSupersampleFiltering( sf ); + + // Checks if Motion smoothing setting can be read and synchs adv + // settings to steamvr/openvr + auto ms = vr::VRSettings()->GetBool( vr::k_pch_SteamVR_Section, vr::k_pch_SteamVR_MotionSmoothing_Bool, &vrSettingsError ); - if ( vrSettingsError != vr::VRSettingsError_None ) - { - LOG( WARNING ) << "Could not read \"" - << vr::k_pch_SteamVR_MotionSmoothing_Bool - << "\" setting: " - << vr::VRSettings()->GetSettingsErrorNameFromEnum( - vrSettingsError ); - } - setMotionSmoothing( ms ); - - // Synch's our saved Values of gain to the SteamVR's version - // This will allow other apps to modify Gain. - - bool valueAdjust = true; - if ( isOverlayMethodActive() ) - { - valueAdjust = false; - } - synchGain( valueAdjust ); - - settingsUpdateCounter = 0; - } - else + if ( vrSettingsError != vr::VRSettingsError_None ) { - settingsUpdateCounter++; + LOG( WARNING ) << "Could not read \"" + << vr::k_pch_SteamVR_MotionSmoothing_Bool + << "\" setting: " + << vr::VRSettings()->GetSettingsErrorNameFromEnum( + vrSettingsError ); } + setMotionSmoothing( ms ); + + // Synch's our saved Values of gain to the SteamVR's version + // This will allow other apps to modify Gain. + + bool valueAdjust = true; + if ( isOverlayMethodActive() ) + { + valueAdjust = false; + } + synchGain( valueAdjust ); + + settingsUpdateCounter = 0; } void VideoTabController::reloadVideoConfig() diff --git a/src/utils/update_rate.cpp b/src/utils/update_rate.cpp new file mode 100644 index 0000000..60735d4 --- /dev/null +++ b/src/utils/update_rate.cpp @@ -0,0 +1,57 @@ +#include "update_rate.h" + +UpdateRate updateRate{}; + +/** + @brief getSubjectRate returns the amount of frames before a subject is run + again. + @param Subject + @return Amount of frames before subject is run again. + + So a return value of 19 would mean that every 19 frames, the subject is run. + Divide the expected framerate with the return value to get the amount of + times per second the subject is run. + */ +constexpr unsigned int getSubjectRate( const UpdateSubject subject ) +{ + /* + Return values are primes in order to reduce the amount of times where + multiple subjects are run on the same frame. + */ + switch ( subject ) + { + case UpdateSubject::UtilitiesTabController: + return 19; + case UpdateSubject::VideoDashboard: + return 47; + case UpdateSubject::AudioTabController: + return 89; + case UpdateSubject::SteamVrTabController: + return 97; + case UpdateSubject::ChaperoneTabController: + return 101; + case UpdateSubject::SettingsTabController: + return 157; + } + + return 0; +} + +bool UpdateRate::shouldSubjectRun( const UpdateSubject subject ) noexcept +{ + const auto subjectTarget = getSubjectRate( subject ); + + const auto shouldRun = ( m_counter % subjectTarget ) == 0; + + return shouldRun; +} + +bool UpdateRate::shouldSubjectNotRun( const UpdateSubject subject ) noexcept +{ + return !shouldSubjectRun( subject ); +} + +void UpdateRate::incrementCounter() noexcept +{ + m_counter++; +} diff --git a/src/utils/update_rate.h b/src/utils/update_rate.h new file mode 100644 index 0000000..cdfa97e --- /dev/null +++ b/src/utils/update_rate.h @@ -0,0 +1,28 @@ +#pragma once + +enum class UpdateSubject +{ + AudioTabController, + ChaperoneTabController, + SettingsTabController, + SteamVrTabController, + UtilitiesTabController, + VideoDashboard, +}; + +class UpdateRate +{ +public: + [[nodiscard]] bool shouldSubjectRun( const UpdateSubject subject ) noexcept; + [[nodiscard]] bool + shouldSubjectNotRun( const UpdateSubject subject ) noexcept; + void incrementCounter() noexcept; + +private: + // counter is deliberately set as unsigned so that the program can continue + // functioning if the counter should go above INT_MAX (or UINT_MAX in this + // case) + unsigned int m_counter; +}; + +extern UpdateRate updateRate;