XRSpace.cpp (3077B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this file, 5 * You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #include "mozilla/dom/XRSpace.h" 8 9 #include "VRDisplayClient.h" 10 #include "mozilla/dom/XRRigidTransform.h" 11 12 namespace mozilla::dom { 13 14 NS_IMPL_CYCLE_COLLECTION_CLASS(XRSpace) 15 16 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(XRSpace, DOMEventTargetHelper) 17 NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mSession) 18 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END 19 20 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(XRSpace, DOMEventTargetHelper) 21 NS_IMPL_CYCLE_COLLECTION_UNLINK(mSession) 22 // Don't need NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER because 23 // DOMEventTargetHelper does it for us. 24 NS_IMPL_CYCLE_COLLECTION_UNLINK_END 25 26 NS_IMPL_ISUPPORTS_CYCLE_COLLECTION_INHERITED_0(XRSpace, DOMEventTargetHelper) 27 28 XRSpace::XRSpace(nsIGlobalObject* aParent, XRSession* aSession, 29 XRNativeOrigin* aNativeOrigin) 30 : DOMEventTargetHelper(aParent), 31 mSession(aSession), 32 mNativeOrigin(aNativeOrigin), 33 mOriginOffsetPosition(0.0f, 0.0f, 0.0f), 34 mOriginOffsetOrientation(0.0f, 0.0f, 0.0f, 1.0f) {} 35 36 JSObject* XRSpace::WrapObject(JSContext* aCx, 37 JS::Handle<JSObject*> aGivenProto) { 38 return XRSpace_Binding::Wrap(aCx, this, aGivenProto); 39 } 40 41 XRSession* XRSpace::GetSession() const { return mSession; } 42 43 gfx::QuaternionDouble XRSpace::GetEffectiveOriginOrientation() const { 44 gfx::QuaternionDouble orientation = 45 mNativeOrigin->GetOrientation() * mOriginOffsetOrientation; 46 return orientation; 47 } 48 49 gfx::PointDouble3D XRSpace::GetEffectiveOriginPosition() const { 50 gfx::PointDouble3D position; 51 position = mNativeOrigin->GetPosition(); 52 position = mOriginOffsetOrientation.RotatePoint(position); 53 position += mOriginOffsetPosition; 54 return position; 55 } 56 57 gfx::Matrix4x4Double XRSpace::GetEffectiveOriginTransform() const { 58 gfx::Matrix4x4Double transform; 59 transform.SetRotationFromQuaternion(GetEffectiveOriginOrientation()); 60 transform.PostTranslate(GetEffectiveOriginPosition()); 61 return transform; 62 } 63 64 bool XRSpace::IsPositionEmulated() const { 65 gfx::VRDisplayClient* display = mSession->GetDisplayClient(); 66 if (!display) { 67 // When there are no sensors, the position is considered emulated. 68 return true; 69 } 70 const gfx::VRDisplayInfo& displayInfo = display->GetDisplayInfo(); 71 if (displayInfo.GetCapabilities() & 72 gfx::VRDisplayCapabilityFlags::Cap_PositionEmulated) { 73 // Cap_PositionEmulated indicates that the position is always emulated. 74 return true; 75 } 76 const gfx::VRHMDSensorState& sensorState = display->GetSensorState(); 77 // When positional tracking is lost, the position is considered emulated. 78 return ((sensorState.flags & gfx::VRDisplayCapabilityFlags::Cap_Position) == 79 gfx::VRDisplayCapabilityFlags::Cap_None); 80 } 81 82 } // namespace mozilla::dom