Bug 1777562 [wpt PR 34663] - [FedCM] Rename FederatedCredential to IdentityCredential...
[gecko.git] / dom / vr / XRSpace.cpp
blob1f6449e8704ad7e0813784c83d5f11e440696578
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/. */
7 #include "mozilla/dom/XRSpace.h"
8 #include "mozilla/dom/XRRigidTransform.h"
9 #include "VRDisplayClient.h"
11 namespace mozilla::dom {
13 NS_IMPL_CYCLE_COLLECTION_CLASS(XRSpace)
15 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(XRSpace, DOMEventTargetHelper)
16 NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mSession)
17 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
19 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(XRSpace, DOMEventTargetHelper)
20 NS_IMPL_CYCLE_COLLECTION_UNLINK(mSession)
21 // Don't need NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER because
22 // DOMEventTargetHelper does it for us.
23 NS_IMPL_CYCLE_COLLECTION_UNLINK_END
25 NS_IMPL_ISUPPORTS_CYCLE_COLLECTION_INHERITED_0(XRSpace, DOMEventTargetHelper)
27 XRSpace::XRSpace(nsIGlobalObject* aParent, XRSession* aSession,
28 XRNativeOrigin* aNativeOrigin)
29 : DOMEventTargetHelper(aParent),
30 mSession(aSession),
31 mNativeOrigin(aNativeOrigin),
32 mOriginOffsetPosition(0.0f, 0.0f, 0.0f),
33 mOriginOffsetOrientation(0.0f, 0.0f, 0.0f, 1.0f) {}
35 JSObject* XRSpace::WrapObject(JSContext* aCx,
36 JS::Handle<JSObject*> aGivenProto) {
37 return XRSpace_Binding::Wrap(aCx, this, aGivenProto);
40 XRSession* XRSpace::GetSession() const { return mSession; }
42 gfx::QuaternionDouble XRSpace::GetEffectiveOriginOrientation() const {
43 gfx::QuaternionDouble orientation =
44 mNativeOrigin->GetOrientation() * mOriginOffsetOrientation;
45 return orientation;
48 gfx::PointDouble3D XRSpace::GetEffectiveOriginPosition() const {
49 gfx::PointDouble3D position;
50 position = mNativeOrigin->GetPosition();
51 position = mOriginOffsetOrientation.RotatePoint(position);
52 position += mOriginOffsetPosition;
53 return position;
56 gfx::Matrix4x4Double XRSpace::GetEffectiveOriginTransform() const {
57 gfx::Matrix4x4Double transform;
58 transform.SetRotationFromQuaternion(GetEffectiveOriginOrientation());
59 transform.PostTranslate(GetEffectiveOriginPosition());
60 return transform;
63 bool XRSpace::IsPositionEmulated() const {
64 gfx::VRDisplayClient* display = mSession->GetDisplayClient();
65 if (!display) {
66 // When there are no sensors, the position is considered emulated.
67 return true;
69 const gfx::VRDisplayInfo& displayInfo = display->GetDisplayInfo();
70 if (displayInfo.GetCapabilities() &
71 gfx::VRDisplayCapabilityFlags::Cap_PositionEmulated) {
72 // Cap_PositionEmulated indicates that the position is always emulated.
73 return true;
75 const gfx::VRHMDSensorState& sensorState = display->GetSensorState();
76 // When positional tracking is lost, the position is considered emulated.
77 return ((sensorState.flags & gfx::VRDisplayCapabilityFlags::Cap_Position) ==
78 gfx::VRDisplayCapabilityFlags::Cap_None);
81 } // namespace mozilla::dom