Bug 1299928 - Part 5: Handle gamepad events in Vsync time; r=kip

MozReview-Commit-ID: Ndt3zajkli

--HG--
extra : rebase_source : 1e14a470ea3aede5c167f7699db0f3b8bdfdfa10
This commit is contained in:
Daosheng Mu 2016-10-07 17:00:45 +08:00
parent e01d3ca1b4
commit 1c5ae68a31
3 changed files with 72 additions and 2 deletions

View File

@ -185,12 +185,16 @@ VRManager::NotifyVsync(const TimeStamp& aVsyncTimestamp)
if (mLastRefreshTime.IsNull()) {
// This is the first vsync, must refresh VR displays
RefreshVRDisplays();
RefreshVRControllers();
mLastRefreshTime = TimeStamp::Now();
} else {
// We don't have to do this every frame, so check if we
// have refreshed recently.
TimeDuration duration = TimeStamp::Now() - mLastRefreshTime;
if (duration.ToMilliseconds() > kVRDisplayRefreshMaxDuration) {
RefreshVRDisplays();
RefreshVRControllers();
mLastRefreshTime = TimeStamp::Now();
}
}
}
@ -252,8 +256,6 @@ VRManager::RefreshVRDisplays(bool aMustDispatch)
if (displayInfoChanged || aMustDispatch) {
DispatchVRDisplayInfoUpdate();
}
mLastRefreshTime = TimeStamp::Now();
}
void
@ -325,6 +327,44 @@ VRManager::GetVRControllerInfo(nsTArray<VRControllerInfo>& aControllerInfo)
}
}
void
VRManager::RefreshVRControllers()
{
nsTArray<RefPtr<gfx::VRControllerHost>> controllers;
for (uint32_t i = 0; i < mControllerManagers.Length()
&& controllers.Length() == 0; ++i) {
mControllerManagers[i]->GetControllers(controllers);
}
bool controllerInfoChanged = false;
if (controllers.Length() != mVRControllers.Count()) {
// Catch cases where VR controllers has been removed
controllerInfoChanged = true;
}
for (const auto& controller : controllers) {
if (!GetController(controller->GetControllerInfo().GetControllerID())) {
// This is a new controller
controllerInfoChanged = true;
break;
}
}
if (controllerInfoChanged) {
mVRControllers.Clear();
for (const auto& controller : controllers) {
mVRControllers.Put(controller->GetControllerInfo().GetControllerID(),
controller);
}
}
for (uint32_t i = 0; i < mControllerManagers.Length(); ++i) {
mControllerManagers[i]->HandleInput();
}
}
void
VRManager::ScanForDevices()
{

View File

@ -60,6 +60,7 @@ private:
void Destroy();
void DispatchVRDisplayInfoUpdate();
void RefreshVRControllers();
typedef nsTHashtable<nsRefPtrHashKey<VRManagerParent>> VRManagerParentSet;
VRManagerParentSet mVRManagerParents;

View File

@ -510,6 +510,35 @@ VRControllerManagerOpenVR::Destroy()
mOpenVRInstalled = false;
}
void
VRControllerManagerOpenVR::HandleInput()
{
MOZ_ASSERT(mVRSystem);
// Process OpenVR controller state
for (vr::TrackedDeviceIndex_t trackedDevice = 0;
trackedDevice < vr::k_unMaxTrackedDeviceCount; trackedDevice++ ) {
vr::VRControllerState_t state;
if (mVRSystem->GetTrackedDeviceClass(trackedDevice)
!= vr::TrackedDeviceClass_Controller) {
continue;
}
if (mVRSystem->GetControllerState(trackedDevice, &state)) {
if (state.ulButtonPressed) {
// TODO: For Bug 1299929 after landing, convert the button mask to an ID button
// NewButtonEvent(1,
// 0,
// 0,
// true);
}
}
}
return;
}
void
VRControllerManagerOpenVR::GetControllers(nsTArray<RefPtr<VRControllerHost>>& aControllerResult)
{