From cbee8fab666d1cc39f31ff931fa83e4bd1f2b99d Mon Sep 17 00:00:00 2001 From: Connor McLaughlin Date: Tue, 19 Jan 2021 00:27:33 +1000 Subject: [PATCH] CommonHostInterface: Throttle rumble updates to changes or every 100ms These updates appear to be synchronous, and limit how fast we can fast forward when controllers are connected via bluetooth. --- src/frontend-common/common_host_interface.cpp | 27 +++++++++++++++++-- src/frontend-common/common_host_interface.h | 1 + 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/frontend-common/common_host_interface.cpp b/src/frontend-common/common_host_interface.cpp index 54c630c5e..ace58049f 100644 --- a/src/frontend-common/common_host_interface.cpp +++ b/src/frontend-common/common_host_interface.cpp @@ -1075,21 +1075,44 @@ void CommonHostInterface::AddControllerRumble(u32 controller_index, u32 num_moto rumble.num_motors = std::min(num_motors, ControllerRumbleState::MAX_MOTORS); rumble.last_strength.fill(0.0f); rumble.update_callback = std::move(callback); + rumble.last_update_time = Common::Timer::GetValue(); m_controller_vibration_motors.push_back(std::move(rumble)); } void CommonHostInterface::UpdateControllerRumble() { + if (m_controller_vibration_motors.empty()) + return; + + // Rumble update frequency in milliseconds. + // We won't send an update to the controller unless this amount of time has passed, if the value has not changed. + // This is because the rumble update is synchronous, and with bluetooth latency can severely impact fast forward + // performance. + static constexpr float UPDATE_FREQUENCY = 1000.0f; + const u64 time = Common::Timer::GetValue(); + for (ControllerRumbleState& rumble : m_controller_vibration_motors) { Controller* controller = System::GetController(rumble.controller_index); if (!controller) continue; + bool changed = false; for (u32 i = 0; i < rumble.num_motors; i++) - rumble.last_strength[i] = controller->GetVibrationMotorStrength(i); + { + const float strength = controller->GetVibrationMotorStrength(i); + if (rumble.last_strength[i] != strength) + { + rumble.last_strength[i] = strength; + changed = true; + } + } - rumble.update_callback(rumble.last_strength.data(), rumble.num_motors); + if (changed || Common::Timer::ConvertValueToMilliseconds(time - rumble.last_update_time) >= UPDATE_FREQUENCY) + { + rumble.last_update_time = time; + rumble.update_callback(rumble.last_strength.data(), rumble.num_motors); + } } } diff --git a/src/frontend-common/common_host_interface.h b/src/frontend-common/common_host_interface.h index a33a882e9..e76c0a60d 100644 --- a/src/frontend-common/common_host_interface.h +++ b/src/frontend-common/common_host_interface.h @@ -396,6 +396,7 @@ private: u32 num_motors; std::array last_strength; ControllerRumbleCallback update_callback; + u64 last_update_time; }; std::vector m_controller_vibration_motors;