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 "nsWrapperCache.h"
9 #include "mozilla/dom/Element.h"
10 #include "mozilla/dom/ElementBinding.h"
11 #include "mozilla/dom/Promise.h"
12 #include "mozilla/dom/UserActivation.h"
13 #include "mozilla/dom/VRDisplay.h"
14 #include "mozilla/dom/VRDisplayBinding.h"
15 #include "mozilla/HoldDropJSObjects.h"
16 #include "mozilla/Base64.h"
17 #include "mozilla/ProfilerMarkers.h"
18 #include "mozilla/Services.h"
19 #include "mozilla/StaticPrefs_dom.h"
20 #include "mozilla/gfx/DataSurfaceHelpers.h"
21 #include "Navigator.h"
24 #include "VRDisplayClient.h"
25 #include "VRManagerChild.h"
26 #include "VRDisplayPresentation.h"
27 #include "nsIObserverService.h"
29 #include "nsISupportsPrimitives.h"
31 using namespace mozilla::gfx
;
33 namespace mozilla::dom
{
35 VRFieldOfView::VRFieldOfView(nsISupports
* aParent
, double aUpDegrees
,
36 double aRightDegrees
, double aDownDegrees
,
39 mUpDegrees(aUpDegrees
),
40 mRightDegrees(aRightDegrees
),
41 mDownDegrees(aDownDegrees
),
42 mLeftDegrees(aLeftDegrees
) {}
44 VRFieldOfView::VRFieldOfView(nsISupports
* aParent
,
45 const gfx::VRFieldOfView
& aSrc
)
47 mUpDegrees(aSrc
.upDegrees
),
48 mRightDegrees(aSrc
.rightDegrees
),
49 mDownDegrees(aSrc
.downDegrees
),
50 mLeftDegrees(aSrc
.leftDegrees
) {}
52 bool VRDisplayCapabilities::HasPosition() const {
53 return bool(mFlags
& gfx::VRDisplayCapabilityFlags::Cap_Position
) ||
54 bool(mFlags
& gfx::VRDisplayCapabilityFlags::Cap_PositionEmulated
);
57 bool VRDisplayCapabilities::HasOrientation() const {
58 return bool(mFlags
& gfx::VRDisplayCapabilityFlags::Cap_Orientation
);
61 bool VRDisplayCapabilities::HasExternalDisplay() const {
62 return bool(mFlags
& gfx::VRDisplayCapabilityFlags::Cap_External
);
65 bool VRDisplayCapabilities::CanPresent() const {
66 return bool(mFlags
& gfx::VRDisplayCapabilityFlags::Cap_Present
);
69 uint32_t VRDisplayCapabilities::MaxLayers() const {
70 return CanPresent() ? 1 : 0;
73 void VRDisplay::UpdateDisplayClient(
74 already_AddRefed
<gfx::VRDisplayClient
> aClient
) {
75 mClient
= std::move(aClient
);
79 bool VRDisplay::RefreshVRDisplays(uint64_t aWindowId
) {
80 gfx::VRManagerChild
* vm
= gfx::VRManagerChild::Get();
81 return vm
&& vm
->RefreshVRDisplaysWithCallback(aWindowId
);
85 void VRDisplay::UpdateVRDisplays(nsTArray
<RefPtr
<VRDisplay
>>& aDisplays
,
86 nsPIDOMWindowInner
* aWindow
) {
87 nsTArray
<RefPtr
<VRDisplay
>> displays
;
89 gfx::VRManagerChild
* vm
= gfx::VRManagerChild::Get();
90 nsTArray
<RefPtr
<gfx::VRDisplayClient
>> updatedDisplays
;
92 vm
->GetVRDisplays(updatedDisplays
);
93 for (size_t i
= 0; i
< updatedDisplays
.Length(); i
++) {
94 RefPtr
<gfx::VRDisplayClient
> display
= updatedDisplays
[i
];
95 bool isNewDisplay
= true;
96 for (size_t j
= 0; j
< aDisplays
.Length(); j
++) {
97 if (aDisplays
[j
]->GetClient()->GetDisplayInfo().GetDisplayID() ==
98 display
->GetDisplayInfo().GetDisplayID()) {
99 displays
.AppendElement(aDisplays
[j
]);
100 isNewDisplay
= false;
102 RefPtr
<gfx::VRDisplayClient
> ref
= display
;
103 aDisplays
[j
]->UpdateDisplayClient(do_AddRef(display
));
104 displays
.AppendElement(aDisplays
[j
]);
105 isNewDisplay
= false;
110 displays
.AppendElement(new VRDisplay(aWindow
, display
));
115 aDisplays
= std::move(displays
);
118 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(VRFieldOfView
, mParent
)
120 JSObject
* VRFieldOfView::WrapObject(JSContext
* aCx
,
121 JS::Handle
<JSObject
*> aGivenProto
) {
122 return VRFieldOfView_Binding::Wrap(aCx
, this, aGivenProto
);
125 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_WITH_JS_MEMBERS(VREyeParameters
,
129 VREyeParameters::VREyeParameters(nsISupports
* aParent
,
130 const gfx::Point3D
& aEyeTranslation
,
131 const gfx::VRFieldOfView
& aFOV
,
132 const gfx::IntSize
& aRenderSize
)
134 mEyeTranslation(aEyeTranslation
),
135 mRenderSize(aRenderSize
) {
136 mFOV
= new VRFieldOfView(aParent
, aFOV
);
137 mozilla::HoldJSObjects(this);
140 VREyeParameters::~VREyeParameters() { mozilla::DropJSObjects(this); }
142 VRFieldOfView
* VREyeParameters::FieldOfView() { return mFOV
; }
144 void VREyeParameters::GetOffset(JSContext
* aCx
,
145 JS::MutableHandle
<JSObject
*> aRetval
,
148 // Lazily create the Float32Array
150 dom::Float32Array::Create(aCx
, this, mEyeTranslation
.components
, aRv
);
155 aRetval
.set(mOffset
);
158 JSObject
* VREyeParameters::WrapObject(JSContext
* aCx
,
159 JS::Handle
<JSObject
*> aGivenProto
) {
160 return VREyeParameters_Binding::Wrap(aCx
, this, aGivenProto
);
163 VRStageParameters::VRStageParameters(
164 nsISupports
* aParent
, const gfx::Matrix4x4
& aSittingToStandingTransform
,
165 const gfx::Size
& aSize
)
167 mSittingToStandingTransform(aSittingToStandingTransform
),
168 mSittingToStandingTransformArray(nullptr),
170 mozilla::HoldJSObjects(this);
173 VRStageParameters::~VRStageParameters() { mozilla::DropJSObjects(this); }
175 JSObject
* VRStageParameters::WrapObject(JSContext
* aCx
,
176 JS::Handle
<JSObject
*> aGivenProto
) {
177 return VRStageParameters_Binding::Wrap(aCx
, this, aGivenProto
);
180 NS_IMPL_CYCLE_COLLECTION_CLASS(VRStageParameters
)
182 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(VRStageParameters
)
183 NS_IMPL_CYCLE_COLLECTION_UNLINK(mParent
)
184 NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER
185 tmp
->mSittingToStandingTransformArray
= nullptr;
186 NS_IMPL_CYCLE_COLLECTION_UNLINK_END
188 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(VRStageParameters
)
189 NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mParent
)
190 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
192 NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(VRStageParameters
)
193 NS_IMPL_CYCLE_COLLECTION_TRACE_PRESERVED_WRAPPER
194 NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(
195 mSittingToStandingTransformArray
)
196 NS_IMPL_CYCLE_COLLECTION_TRACE_END
198 void VRStageParameters::GetSittingToStandingTransform(
199 JSContext
* aCx
, JS::MutableHandle
<JSObject
*> aRetval
, ErrorResult
& aRv
) {
200 if (!mSittingToStandingTransformArray
) {
201 // Lazily create the Float32Array
202 mSittingToStandingTransformArray
= dom::Float32Array::Create(
203 aCx
, this, mSittingToStandingTransform
.components
, aRv
);
208 aRetval
.set(mSittingToStandingTransformArray
);
211 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(VRDisplayCapabilities
, mParent
)
213 JSObject
* VRDisplayCapabilities::WrapObject(JSContext
* aCx
,
214 JS::Handle
<JSObject
*> aGivenProto
) {
215 return VRDisplayCapabilities_Binding::Wrap(aCx
, this, aGivenProto
);
218 VRPose::VRPose(nsISupports
* aParent
, const gfx::VRHMDSensorState
& aState
)
219 : Pose(aParent
), mVRState(aState
) {
220 mozilla::HoldJSObjects(this);
223 VRPose::VRPose(nsISupports
* aParent
) : Pose(aParent
) {
224 mVRState
.inputFrameID
= 0;
225 mVRState
.timestamp
= 0.0;
226 mVRState
.flags
= gfx::VRDisplayCapabilityFlags::Cap_None
;
227 mozilla::HoldJSObjects(this);
230 VRPose::~VRPose() { mozilla::DropJSObjects(this); }
232 void VRPose::GetPosition(JSContext
* aCx
, JS::MutableHandle
<JSObject
*> aRetval
,
235 bool(mVRState
.flags
& gfx::VRDisplayCapabilityFlags::Cap_Position
) ||
236 bool(mVRState
.flags
&
237 gfx::VRDisplayCapabilityFlags::Cap_PositionEmulated
);
238 SetFloat32Array(aCx
, this, aRetval
, mPosition
,
239 valid
? mVRState
.pose
.position
: nullptr, 3, aRv
);
242 void VRPose::GetLinearVelocity(JSContext
* aCx
,
243 JS::MutableHandle
<JSObject
*> aRetval
,
246 bool(mVRState
.flags
& gfx::VRDisplayCapabilityFlags::Cap_Position
) ||
247 bool(mVRState
.flags
&
248 gfx::VRDisplayCapabilityFlags::Cap_PositionEmulated
);
249 SetFloat32Array(aCx
, this, aRetval
, mLinearVelocity
,
250 valid
? mVRState
.pose
.linearVelocity
: nullptr, 3, aRv
);
253 void VRPose::GetLinearAcceleration(JSContext
* aCx
,
254 JS::MutableHandle
<JSObject
*> aRetval
,
256 const bool valid
= bool(
257 mVRState
.flags
& gfx::VRDisplayCapabilityFlags::Cap_LinearAcceleration
);
258 SetFloat32Array(aCx
, this, aRetval
, mLinearAcceleration
,
259 valid
? mVRState
.pose
.linearAcceleration
: nullptr, 3, aRv
);
262 void VRPose::GetOrientation(JSContext
* aCx
,
263 JS::MutableHandle
<JSObject
*> aRetval
,
266 bool(mVRState
.flags
& gfx::VRDisplayCapabilityFlags::Cap_Orientation
);
267 SetFloat32Array(aCx
, this, aRetval
, mOrientation
,
268 valid
? mVRState
.pose
.orientation
: nullptr, 4, aRv
);
271 void VRPose::GetAngularVelocity(JSContext
* aCx
,
272 JS::MutableHandle
<JSObject
*> aRetval
,
275 bool(mVRState
.flags
& gfx::VRDisplayCapabilityFlags::Cap_Orientation
);
276 SetFloat32Array(aCx
, this, aRetval
, mAngularVelocity
,
277 valid
? mVRState
.pose
.angularVelocity
: nullptr, 3, aRv
);
280 void VRPose::GetAngularAcceleration(JSContext
* aCx
,
281 JS::MutableHandle
<JSObject
*> aRetval
,
283 const bool valid
= bool(
284 mVRState
.flags
& gfx::VRDisplayCapabilityFlags::Cap_AngularAcceleration
);
285 SetFloat32Array(aCx
, this, aRetval
, mAngularAcceleration
,
286 valid
? mVRState
.pose
.angularAcceleration
: nullptr, 3, aRv
);
289 void VRPose::Update(const gfx::VRHMDSensorState
& aState
) { mVRState
= aState
; }
291 JSObject
* VRPose::WrapObject(JSContext
* aCx
,
292 JS::Handle
<JSObject
*> aGivenProto
) {
293 return VRPose_Binding::Wrap(aCx
, this, aGivenProto
);
297 JSObject
* VRDisplay::WrapObject(JSContext
* aCx
,
298 JS::Handle
<JSObject
*> aGivenProto
) {
299 return VRDisplay_Binding::Wrap(aCx
, this, aGivenProto
);
302 VRDisplay::VRDisplay(nsPIDOMWindowInner
* aWindow
, gfx::VRDisplayClient
* aClient
)
303 : DOMEventTargetHelper(aWindow
),
305 mDepthNear(0.01f
) // Default value from WebVR Spec
307 mDepthFar(10000.0f
) // Default value from WebVR Spec
309 mVRNavigationEventDepth(0),
311 const gfx::VRDisplayInfo
& info
= aClient
->GetDisplayInfo();
312 mCapabilities
= new VRDisplayCapabilities(aWindow
, info
.GetCapabilities());
313 if (info
.GetCapabilities() &
314 gfx::VRDisplayCapabilityFlags::Cap_StageParameters
) {
315 mStageParameters
= new VRStageParameters(
316 aWindow
, info
.GetSittingToStandingTransform(), info
.GetStageSize());
318 mozilla::HoldJSObjects(this);
319 nsCOMPtr
<nsIObserverService
> obs
= services::GetObserverService();
320 if (MOZ_LIKELY(obs
)) {
321 obs
->AddObserver(this, "inner-window-destroyed", false);
325 VRDisplay::~VRDisplay() {
326 MOZ_ASSERT(mShutdown
);
327 mozilla::DropJSObjects(this);
330 void VRDisplay::LastRelease() {
331 // We don't want to wait for the CC to free up the presentation
332 // for use in other documents, so we do this in LastRelease().
336 already_AddRefed
<VREyeParameters
> VRDisplay::GetEyeParameters(VREye aEye
) {
337 gfx::VRDisplayState::Eye eye
= aEye
== VREye::Left
338 ? gfx::VRDisplayState::Eye_Left
339 : gfx::VRDisplayState::Eye_Right
;
340 RefPtr
<VREyeParameters
> params
= new VREyeParameters(
341 GetParentObject(), mClient
->GetDisplayInfo().GetEyeTranslation(eye
),
342 mClient
->GetDisplayInfo().GetEyeFOV(eye
),
343 mClient
->GetDisplayInfo().SuggestedEyeResolution());
344 return params
.forget();
347 VRDisplayCapabilities
* VRDisplay::Capabilities() { return mCapabilities
; }
349 VRStageParameters
* VRDisplay::GetStageParameters() { return mStageParameters
; }
351 uint32_t VRDisplay::DisplayId() const {
352 const gfx::VRDisplayInfo
& info
= mClient
->GetDisplayInfo();
353 return info
.GetDisplayID();
356 void VRDisplay::GetDisplayName(nsAString
& aDisplayName
) const {
357 const gfx::VRDisplayInfo
& info
= mClient
->GetDisplayInfo();
358 CopyUTF8toUTF16(MakeStringSpan(info
.GetDisplayName()), aDisplayName
);
361 void VRDisplay::UpdateFrameInfo() {
363 * The WebVR 1.1 spec Requires that VRDisplay.getPose and
364 * VRDisplay.getFrameData must return the same values until the next
365 * VRDisplay.submitFrame.
367 * mFrameInfo is marked dirty at the end of the frame or start of a new
368 * composition and lazily created here in order to receive mid-frame
369 * pose-prediction updates while still ensuring conformance to the WebVR spec
372 * If we are not presenting WebVR content, the frame will never end and we
373 * should return the latest frame data always.
377 if ((mFrameInfo
.IsDirty() && IsPresenting()) ||
378 mClient
->GetDisplayInfo().GetPresentingGroups() == 0) {
379 const gfx::VRHMDSensorState
& state
= mClient
->GetSensorState();
380 const gfx::VRDisplayInfo
& info
= mClient
->GetDisplayInfo();
381 mFrameInfo
.Update(info
, state
, mDepthNear
, mDepthFar
);
385 bool VRDisplay::GetFrameData(VRFrameData
& aFrameData
) {
387 if (!(mFrameInfo
.mVRState
.flags
&
388 gfx::VRDisplayCapabilityFlags::Cap_Orientation
)) {
389 // We must have at minimum Cap_Orientation for a valid pose.
392 aFrameData
.Update(mFrameInfo
);
396 already_AddRefed
<VRPose
> VRDisplay::GetPose() {
398 RefPtr
<VRPose
> obj
= new VRPose(GetParentObject(), mFrameInfo
.mVRState
);
403 void VRDisplay::ResetPose() {
404 // ResetPose is deprecated and unimplemented
405 // We must keep this stub function around as its referenced by
406 // VRDisplay.webidl. Not asserting here, as that could break existing web
410 void VRDisplay::StartVRNavigation() { mClient
->StartVRNavigation(); }
412 void VRDisplay::StartHandlingVRNavigationEvent() {
413 mHandlingVRNavigationEventStart
= TimeStamp::Now();
414 ++mVRNavigationEventDepth
;
415 TimeDuration timeout
=
416 TimeDuration::FromMilliseconds(StaticPrefs::dom_vr_navigation_timeout());
417 // A 0 or negative TimeDuration indicates that content may take
418 // as long as it wishes to respond to the event, as long as
419 // it happens before the event exits.
420 if (timeout
.ToMilliseconds() > 0) {
421 mClient
->StopVRNavigation(timeout
);
425 void VRDisplay::StopHandlingVRNavigationEvent() {
426 MOZ_ASSERT(mVRNavigationEventDepth
> 0);
427 --mVRNavigationEventDepth
;
428 if (mVRNavigationEventDepth
== 0) {
429 mClient
->StopVRNavigation(TimeDuration::FromMilliseconds(0));
433 bool VRDisplay::IsHandlingVRNavigationEvent() {
434 if (mVRNavigationEventDepth
== 0) {
437 if (mHandlingVRNavigationEventStart
.IsNull()) {
440 TimeDuration timeout
=
441 TimeDuration::FromMilliseconds(StaticPrefs::dom_vr_navigation_timeout());
442 return timeout
.ToMilliseconds() <= 0 ||
443 (TimeStamp::Now() - mHandlingVRNavigationEventStart
) <= timeout
;
446 void VRDisplay::OnPresentationGenerationChanged() { ExitPresentInternal(); }
448 already_AddRefed
<Promise
> VRDisplay::RequestPresent(
449 const nsTArray
<VRLayer
>& aLayers
, CallerType aCallerType
,
451 nsCOMPtr
<nsIGlobalObject
> global
= GetParentObject();
453 aRv
.Throw(NS_ERROR_FAILURE
);
457 RefPtr
<Promise
> promise
= Promise::Create(global
, aRv
);
458 NS_ENSURE_TRUE(!aRv
.Failed(), nullptr);
460 bool isChromePresentation
= aCallerType
== CallerType::System
;
461 uint32_t presentationGroup
=
462 isChromePresentation
? gfx::kVRGroupChrome
: gfx::kVRGroupContent
;
464 mClient
->SetXRAPIMode(gfx::VRAPIMode::WebVR
);
465 if (!UserActivation::IsHandlingUserInput() && !isChromePresentation
&&
466 !IsHandlingVRNavigationEvent() && StaticPrefs::dom_vr_require_gesture() &&
468 // The WebVR API states that if called outside of a user gesture, the
469 // promise must be rejected. We allow VR presentations to start within
470 // trusted events such as vrdisplayactivate, which triggers in response to
471 // HMD proximity sensors and when navigating within a VR presentation.
472 // This user gesture requirement is not enforced for chrome/system code.
473 promise
->MaybeRejectWithUndefined();
474 } else if (!IsPresenting() && IsAnyPresenting(presentationGroup
)) {
475 // Only one presentation allowed per VRDisplay on a
476 // first-come-first-serve basis.
477 // If this Javascript context is presenting, then we can replace our
478 // presentation with a new one containing new layers but we should never
479 // replace the presentation of another context.
480 // Simultaneous presentations in other groups are allowed in separate
481 // Javascript contexts to enable browser UI from chrome/system contexts.
482 // Eventually, this restriction will be loosened to enable multitasking
484 promise
->MaybeRejectWithUndefined();
487 mPresentation
->UpdateLayers(aLayers
);
489 mPresentation
= mClient
->BeginPresentation(aLayers
, presentationGroup
);
492 promise
->MaybeResolve(JS::UndefinedHandleValue
);
494 return promise
.forget();
498 VRDisplay::Observe(nsISupports
* aSubject
, const char* aTopic
,
499 const char16_t
* aData
) {
500 MOZ_ASSERT(NS_IsMainThread());
502 if (strcmp(aTopic
, "inner-window-destroyed") == 0) {
503 nsCOMPtr
<nsISupportsPRUint64
> wrapper
= do_QueryInterface(aSubject
);
504 NS_ENSURE_TRUE(wrapper
, NS_ERROR_FAILURE
);
507 nsresult rv
= wrapper
->GetData(&innerID
);
508 NS_ENSURE_SUCCESS(rv
, rv
);
510 if (!GetOwner() || GetOwner()->WindowID() == innerID
) {
517 // This should not happen.
518 return NS_ERROR_FAILURE
;
521 already_AddRefed
<Promise
> VRDisplay::ExitPresent(ErrorResult
& aRv
) {
522 nsCOMPtr
<nsIGlobalObject
> global
= GetParentObject();
524 aRv
.Throw(NS_ERROR_FAILURE
);
528 RefPtr
<Promise
> promise
= Promise::Create(global
, aRv
);
529 NS_ENSURE_TRUE(!aRv
.Failed(), nullptr);
531 if (!IsPresenting()) {
532 // We can not exit a presentation outside of the context that
533 // started the presentation.
534 promise
->MaybeRejectWithUndefined();
536 promise
->MaybeResolve(JS::UndefinedHandleValue
);
537 ExitPresentInternal();
540 return promise
.forget();
543 void VRDisplay::ExitPresentInternal() { mPresentation
= nullptr; }
545 void VRDisplay::Shutdown() {
547 ExitPresentInternal();
548 nsCOMPtr
<nsIObserverService
> obs
= services::GetObserverService();
549 if (MOZ_LIKELY(obs
)) {
550 obs
->RemoveObserver(this, "inner-window-destroyed");
554 void VRDisplay::GetLayers(nsTArray
<VRLayer
>& result
) {
556 mPresentation
->GetDOMLayers(result
);
558 result
= nsTArray
<VRLayer
>();
562 void VRDisplay::SubmitFrame() {
563 AUTO_PROFILER_TRACING_MARKER("VR", "SubmitFrameAtVRDisplay", OTHER
);
565 if (mClient
&& !mClient
->IsPresentationGenerationCurrent()) {
566 mPresentation
= nullptr;
567 mClient
->MakePresentationGenerationCurrent();
571 mPresentation
->SubmitFrame();
576 int32_t VRDisplay::RequestAnimationFrame(FrameRequestCallback
& aCallback
,
577 ErrorResult
& aError
) {
582 gfx::VRManagerChild
* vm
= gfx::VRManagerChild::Get();
585 aError
= vm
->ScheduleFrameRequestCallback(aCallback
, &handle
);
589 void VRDisplay::CancelAnimationFrame(int32_t aHandle
, ErrorResult
& aError
) {
590 gfx::VRManagerChild
* vm
= gfx::VRManagerChild::Get();
591 vm
->CancelFrameRequestCallback(aHandle
);
594 bool VRDisplay::IsPresenting() const {
595 // IsPresenting returns true only if this Javascript context is presenting
596 // and will return false if another context is presenting.
597 return mPresentation
!= nullptr;
600 bool VRDisplay::IsAnyPresenting(uint32_t aGroupMask
) const {
601 // IsAnyPresenting returns true if either this VRDisplay object or any other
602 // from anther Javascript context is presenting with a group matching
604 if (mPresentation
&& (mPresentation
->GetGroup() & aGroupMask
)) {
607 if (mClient
->GetDisplayInfo().GetPresentingGroups() & aGroupMask
) {
613 bool VRDisplay::IsConnected() const { return mClient
->GetIsConnected(); }
615 uint32_t VRDisplay::PresentingGroups() const {
616 return mClient
->GetDisplayInfo().GetPresentingGroups();
619 uint32_t VRDisplay::GroupMask() const {
620 return mClient
->GetDisplayInfo().GetGroupMask();
623 void VRDisplay::SetGroupMask(const uint32_t& aGroupMask
) {
624 mClient
->SetGroupMask(aGroupMask
);
627 NS_IMPL_CYCLE_COLLECTION_INHERITED(VRDisplay
, DOMEventTargetHelper
,
628 mCapabilities
, mStageParameters
)
630 NS_IMPL_ADDREF_INHERITED(VRDisplay
, DOMEventTargetHelper
)
631 NS_IMPL_RELEASE_INHERITED(VRDisplay
, DOMEventTargetHelper
)
633 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(VRDisplay
)
634 NS_INTERFACE_MAP_ENTRY(nsIObserver
)
635 NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports
, EventTarget
)
636 NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper
)
638 NS_IMPL_CYCLE_COLLECTION_CLASS(VRFrameData
)
640 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(VRFrameData
)
641 NS_IMPL_CYCLE_COLLECTION_UNLINK(mParent
, mPose
)
642 NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER
643 tmp
->mLeftProjectionMatrix
= nullptr;
644 tmp
->mLeftViewMatrix
= nullptr;
645 tmp
->mRightProjectionMatrix
= nullptr;
646 tmp
->mRightViewMatrix
= nullptr;
647 NS_IMPL_CYCLE_COLLECTION_UNLINK_END
649 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(VRFrameData
)
650 NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mParent
, mPose
)
651 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
653 NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(VRFrameData
)
654 NS_IMPL_CYCLE_COLLECTION_TRACE_PRESERVED_WRAPPER
655 NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mLeftProjectionMatrix
)
656 NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mLeftViewMatrix
)
657 NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mRightProjectionMatrix
)
658 NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mRightViewMatrix
)
659 NS_IMPL_CYCLE_COLLECTION_TRACE_END
661 VRFrameData::VRFrameData(nsISupports
* aParent
)
663 mLeftProjectionMatrix(nullptr),
664 mLeftViewMatrix(nullptr),
665 mRightProjectionMatrix(nullptr),
666 mRightViewMatrix(nullptr) {
667 mozilla::HoldJSObjects(this);
668 mPose
= new VRPose(aParent
);
671 VRFrameData::~VRFrameData() { mozilla::DropJSObjects(this); }
674 already_AddRefed
<VRFrameData
> VRFrameData::Constructor(
675 const GlobalObject
& aGlobal
) {
676 RefPtr
<VRFrameData
> obj
= new VRFrameData(aGlobal
.GetAsSupports());
680 JSObject
* VRFrameData::WrapObject(JSContext
* aCx
,
681 JS::Handle
<JSObject
*> aGivenProto
) {
682 return VRFrameData_Binding::Wrap(aCx
, this, aGivenProto
);
685 VRPose
* VRFrameData::Pose() { return mPose
; }
687 double VRFrameData::Timestamp() const {
688 // Converting from seconds to milliseconds
689 return mFrameInfo
.mVRState
.timestamp
* 1000.0f
;
692 void VRFrameData::GetLeftProjectionMatrix(JSContext
* aCx
,
693 JS::MutableHandle
<JSObject
*> aRetval
,
695 Pose::SetFloat32Array(aCx
, this, aRetval
, mLeftProjectionMatrix
,
696 mFrameInfo
.mLeftProjection
.components
, 16, aRv
);
699 void VRFrameData::GetLeftViewMatrix(JSContext
* aCx
,
700 JS::MutableHandle
<JSObject
*> aRetval
,
702 Pose::SetFloat32Array(aCx
, this, aRetval
, mLeftViewMatrix
,
703 mFrameInfo
.mLeftView
.components
, 16, aRv
);
706 void VRFrameData::GetRightProjectionMatrix(JSContext
* aCx
,
707 JS::MutableHandle
<JSObject
*> aRetval
,
709 Pose::SetFloat32Array(aCx
, this, aRetval
, mRightProjectionMatrix
,
710 mFrameInfo
.mRightProjection
.components
, 16, aRv
);
713 void VRFrameData::GetRightViewMatrix(JSContext
* aCx
,
714 JS::MutableHandle
<JSObject
*> aRetval
,
716 Pose::SetFloat32Array(aCx
, this, aRetval
, mRightViewMatrix
,
717 mFrameInfo
.mRightView
.components
, 16, aRv
);
720 void VRFrameData::Update(const VRFrameInfo
& aFrameInfo
) {
721 mFrameInfo
= aFrameInfo
;
722 mPose
->Update(mFrameInfo
.mVRState
);
725 void VRFrameInfo::Update(const gfx::VRDisplayInfo
& aInfo
,
726 const gfx::VRHMDSensorState
& aState
, float aDepthNear
,
729 if (mTimeStampOffset
== 0.0f
) {
731 * A mTimeStampOffset value of 0.0f indicates that this is the first
732 * iteration and an offset has not yet been set.
734 * Generate a value for mTimeStampOffset such that if aState.timestamp is
735 * monotonically increasing, aState.timestamp + mTimeStampOffset will never
736 * be a negative number and will start at a pseudo-random offset
737 * between 1000.0f and 11000.0f seconds.
739 * We use a pseudo random offset rather than 0.0f just to discourage users
740 * from making the assumption that the timestamp returned in the WebVR API
741 * has a base of 0, which is not necessarily true in all UA's.
744 float(rand()) / float(RAND_MAX
) * 10000.0f
+ 1000.0f
- aState
.timestamp
;
746 mVRState
.timestamp
= aState
.timestamp
+ mTimeStampOffset
;
748 // Avoid division by zero within ConstructProjectionMatrix
749 const float kEpsilon
= 0.00001f
;
750 if (fabs(aDepthFar
- aDepthNear
) < kEpsilon
) {
751 aDepthFar
= aDepthNear
+ kEpsilon
;
754 const gfx::VRFieldOfView leftFOV
=
755 aInfo
.mDisplayState
.eyeFOV
[gfx::VRDisplayState::Eye_Left
];
757 leftFOV
.ConstructProjectionMatrix(aDepthNear
, aDepthFar
, true);
758 const gfx::VRFieldOfView rightFOV
=
759 aInfo
.mDisplayState
.eyeFOV
[gfx::VRDisplayState::Eye_Right
];
761 rightFOV
.ConstructProjectionMatrix(aDepthNear
, aDepthFar
, true);
762 memcpy(mLeftView
.components
, aState
.leftViewMatrix
,
763 sizeof(aState
.leftViewMatrix
));
764 memcpy(mRightView
.components
, aState
.rightViewMatrix
,
765 sizeof(aState
.rightViewMatrix
));
768 VRFrameInfo::VRFrameInfo() : mTimeStampOffset(0.0f
) {
769 mVRState
.inputFrameID
= 0;
770 mVRState
.timestamp
= 0.0;
771 mVRState
.flags
= gfx::VRDisplayCapabilityFlags::Cap_None
;
774 bool VRFrameInfo::IsDirty() { return mVRState
.timestamp
== 0; }
776 void VRFrameInfo::Clear() { mVRState
.Clear(); }
778 } // namespace mozilla::dom