Bug 1852740: add tests for the `fetchpriority` attribute in Link headers. r=necko...
[gecko.git] / dom / vr / VRDisplay.cpp
blob7c788a4c578162a673af40d6afcea27c161ab453
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"
22 #include "gfxUtils.h"
23 #include "gfxVR.h"
24 #include "VRDisplayClient.h"
25 #include "VRManagerChild.h"
26 #include "VRDisplayPresentation.h"
27 #include "nsIObserverService.h"
28 #include "nsIFrame.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,
37 double aLeftDegrees)
38 : mParent(aParent),
39 mUpDegrees(aUpDegrees),
40 mRightDegrees(aRightDegrees),
41 mDownDegrees(aDownDegrees),
42 mLeftDegrees(aLeftDegrees) {}
44 VRFieldOfView::VRFieldOfView(nsISupports* aParent,
45 const gfx::VRFieldOfView& aSrc)
46 : mParent(aParent),
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);
78 /*static*/
79 bool VRDisplay::RefreshVRDisplays(uint64_t aWindowId) {
80 gfx::VRManagerChild* vm = gfx::VRManagerChild::Get();
81 return vm && vm->RefreshVRDisplaysWithCallback(aWindowId);
84 /*static*/
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;
91 if (vm) {
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;
101 } else {
102 RefPtr<gfx::VRDisplayClient> ref = display;
103 aDisplays[j]->UpdateDisplayClient(do_AddRef(display));
104 displays.AppendElement(aDisplays[j]);
105 isNewDisplay = false;
109 if (isNewDisplay) {
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,
126 (mParent, mFOV),
127 (mOffset))
129 VREyeParameters::VREyeParameters(nsISupports* aParent,
130 const gfx::Point3D& aEyeTranslation,
131 const gfx::VRFieldOfView& aFOV,
132 const gfx::IntSize& aRenderSize)
133 : mParent(aParent),
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,
146 ErrorResult& aRv) {
147 if (!mOffset) {
148 // Lazily create the Float32Array
149 mOffset =
150 dom::Float32Array::Create(aCx, this, 3, mEyeTranslation.components);
151 if (!mOffset) {
152 aRv.NoteJSContextException(aCx);
153 return;
156 aRetval.set(mOffset);
159 JSObject* VREyeParameters::WrapObject(JSContext* aCx,
160 JS::Handle<JSObject*> aGivenProto) {
161 return VREyeParameters_Binding::Wrap(aCx, this, aGivenProto);
164 VRStageParameters::VRStageParameters(
165 nsISupports* aParent, const gfx::Matrix4x4& aSittingToStandingTransform,
166 const gfx::Size& aSize)
167 : mParent(aParent),
168 mSittingToStandingTransform(aSittingToStandingTransform),
169 mSittingToStandingTransformArray(nullptr),
170 mSize(aSize) {
171 mozilla::HoldJSObjects(this);
174 VRStageParameters::~VRStageParameters() { mozilla::DropJSObjects(this); }
176 JSObject* VRStageParameters::WrapObject(JSContext* aCx,
177 JS::Handle<JSObject*> aGivenProto) {
178 return VRStageParameters_Binding::Wrap(aCx, this, aGivenProto);
181 NS_IMPL_CYCLE_COLLECTION_CLASS(VRStageParameters)
183 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(VRStageParameters)
184 NS_IMPL_CYCLE_COLLECTION_UNLINK(mParent)
185 NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER
186 tmp->mSittingToStandingTransformArray = nullptr;
187 NS_IMPL_CYCLE_COLLECTION_UNLINK_END
189 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(VRStageParameters)
190 NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mParent)
191 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
193 NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(VRStageParameters)
194 NS_IMPL_CYCLE_COLLECTION_TRACE_PRESERVED_WRAPPER
195 NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(
196 mSittingToStandingTransformArray)
197 NS_IMPL_CYCLE_COLLECTION_TRACE_END
199 void VRStageParameters::GetSittingToStandingTransform(
200 JSContext* aCx, JS::MutableHandle<JSObject*> aRetval, ErrorResult& aRv) {
201 if (!mSittingToStandingTransformArray) {
202 // Lazily create the Float32Array
203 mSittingToStandingTransformArray = dom::Float32Array::Create(
204 aCx, this, 16, mSittingToStandingTransform.components);
205 if (!mSittingToStandingTransformArray) {
206 aRv.NoteJSContextException(aCx);
207 return;
210 aRetval.set(mSittingToStandingTransformArray);
213 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(VRDisplayCapabilities, mParent)
215 JSObject* VRDisplayCapabilities::WrapObject(JSContext* aCx,
216 JS::Handle<JSObject*> aGivenProto) {
217 return VRDisplayCapabilities_Binding::Wrap(aCx, this, aGivenProto);
220 VRPose::VRPose(nsISupports* aParent, const gfx::VRHMDSensorState& aState)
221 : Pose(aParent), mVRState(aState) {
222 mozilla::HoldJSObjects(this);
225 VRPose::VRPose(nsISupports* aParent) : Pose(aParent) {
226 mVRState.inputFrameID = 0;
227 mVRState.timestamp = 0.0;
228 mVRState.flags = gfx::VRDisplayCapabilityFlags::Cap_None;
229 mozilla::HoldJSObjects(this);
232 VRPose::~VRPose() { mozilla::DropJSObjects(this); }
234 void VRPose::GetPosition(JSContext* aCx, JS::MutableHandle<JSObject*> aRetval,
235 ErrorResult& aRv) {
236 const bool valid =
237 bool(mVRState.flags & gfx::VRDisplayCapabilityFlags::Cap_Position) ||
238 bool(mVRState.flags &
239 gfx::VRDisplayCapabilityFlags::Cap_PositionEmulated);
240 SetFloat32Array(aCx, this, aRetval, mPosition,
241 valid ? mVRState.pose.position : nullptr, 3, aRv);
244 void VRPose::GetLinearVelocity(JSContext* aCx,
245 JS::MutableHandle<JSObject*> aRetval,
246 ErrorResult& aRv) {
247 const bool valid =
248 bool(mVRState.flags & gfx::VRDisplayCapabilityFlags::Cap_Position) ||
249 bool(mVRState.flags &
250 gfx::VRDisplayCapabilityFlags::Cap_PositionEmulated);
251 SetFloat32Array(aCx, this, aRetval, mLinearVelocity,
252 valid ? mVRState.pose.linearVelocity : nullptr, 3, aRv);
255 void VRPose::GetLinearAcceleration(JSContext* aCx,
256 JS::MutableHandle<JSObject*> aRetval,
257 ErrorResult& aRv) {
258 const bool valid = bool(
259 mVRState.flags & gfx::VRDisplayCapabilityFlags::Cap_LinearAcceleration);
260 SetFloat32Array(aCx, this, aRetval, mLinearAcceleration,
261 valid ? mVRState.pose.linearAcceleration : nullptr, 3, aRv);
264 void VRPose::GetOrientation(JSContext* aCx,
265 JS::MutableHandle<JSObject*> aRetval,
266 ErrorResult& aRv) {
267 const bool valid =
268 bool(mVRState.flags & gfx::VRDisplayCapabilityFlags::Cap_Orientation);
269 SetFloat32Array(aCx, this, aRetval, mOrientation,
270 valid ? mVRState.pose.orientation : nullptr, 4, aRv);
273 void VRPose::GetAngularVelocity(JSContext* aCx,
274 JS::MutableHandle<JSObject*> aRetval,
275 ErrorResult& aRv) {
276 const bool valid =
277 bool(mVRState.flags & gfx::VRDisplayCapabilityFlags::Cap_Orientation);
278 SetFloat32Array(aCx, this, aRetval, mAngularVelocity,
279 valid ? mVRState.pose.angularVelocity : nullptr, 3, aRv);
282 void VRPose::GetAngularAcceleration(JSContext* aCx,
283 JS::MutableHandle<JSObject*> aRetval,
284 ErrorResult& aRv) {
285 const bool valid = bool(
286 mVRState.flags & gfx::VRDisplayCapabilityFlags::Cap_AngularAcceleration);
287 SetFloat32Array(aCx, this, aRetval, mAngularAcceleration,
288 valid ? mVRState.pose.angularAcceleration : nullptr, 3, aRv);
291 void VRPose::Update(const gfx::VRHMDSensorState& aState) { mVRState = aState; }
293 JSObject* VRPose::WrapObject(JSContext* aCx,
294 JS::Handle<JSObject*> aGivenProto) {
295 return VRPose_Binding::Wrap(aCx, this, aGivenProto);
298 /* virtual */
299 JSObject* VRDisplay::WrapObject(JSContext* aCx,
300 JS::Handle<JSObject*> aGivenProto) {
301 return VRDisplay_Binding::Wrap(aCx, this, aGivenProto);
304 VRDisplay::VRDisplay(nsPIDOMWindowInner* aWindow, gfx::VRDisplayClient* aClient)
305 : DOMEventTargetHelper(aWindow),
306 mClient(aClient),
307 mDepthNear(0.01f) // Default value from WebVR Spec
309 mDepthFar(10000.0f) // Default value from WebVR Spec
311 mVRNavigationEventDepth(0),
312 mShutdown(false) {
313 const gfx::VRDisplayInfo& info = aClient->GetDisplayInfo();
314 mCapabilities = new VRDisplayCapabilities(aWindow, info.GetCapabilities());
315 if (info.GetCapabilities() &
316 gfx::VRDisplayCapabilityFlags::Cap_StageParameters) {
317 mStageParameters = new VRStageParameters(
318 aWindow, info.GetSittingToStandingTransform(), info.GetStageSize());
320 mozilla::HoldJSObjects(this);
321 nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
322 if (MOZ_LIKELY(obs)) {
323 obs->AddObserver(this, "inner-window-destroyed", false);
327 VRDisplay::~VRDisplay() {
328 MOZ_ASSERT(mShutdown);
329 mozilla::DropJSObjects(this);
332 void VRDisplay::LastRelease() {
333 // We don't want to wait for the CC to free up the presentation
334 // for use in other documents, so we do this in LastRelease().
335 Shutdown();
338 already_AddRefed<VREyeParameters> VRDisplay::GetEyeParameters(VREye aEye) {
339 gfx::VRDisplayState::Eye eye = aEye == VREye::Left
340 ? gfx::VRDisplayState::Eye_Left
341 : gfx::VRDisplayState::Eye_Right;
342 RefPtr<VREyeParameters> params = new VREyeParameters(
343 GetParentObject(), mClient->GetDisplayInfo().GetEyeTranslation(eye),
344 mClient->GetDisplayInfo().GetEyeFOV(eye),
345 mClient->GetDisplayInfo().SuggestedEyeResolution());
346 return params.forget();
349 VRDisplayCapabilities* VRDisplay::Capabilities() { return mCapabilities; }
351 VRStageParameters* VRDisplay::GetStageParameters() { return mStageParameters; }
353 uint32_t VRDisplay::DisplayId() const {
354 const gfx::VRDisplayInfo& info = mClient->GetDisplayInfo();
355 return info.GetDisplayID();
358 void VRDisplay::GetDisplayName(nsAString& aDisplayName) const {
359 const gfx::VRDisplayInfo& info = mClient->GetDisplayInfo();
360 CopyUTF8toUTF16(MakeStringSpan(info.GetDisplayName()), aDisplayName);
363 void VRDisplay::UpdateFrameInfo() {
365 * The WebVR 1.1 spec Requires that VRDisplay.getPose and
366 * VRDisplay.getFrameData must return the same values until the next
367 * VRDisplay.submitFrame.
369 * mFrameInfo is marked dirty at the end of the frame or start of a new
370 * composition and lazily created here in order to receive mid-frame
371 * pose-prediction updates while still ensuring conformance to the WebVR spec
372 * requirements.
374 * If we are not presenting WebVR content, the frame will never end and we
375 * should return the latest frame data always.
377 mFrameInfo.Clear();
379 if ((mFrameInfo.IsDirty() && IsPresenting()) ||
380 mClient->GetDisplayInfo().GetPresentingGroups() == 0) {
381 const gfx::VRHMDSensorState& state = mClient->GetSensorState();
382 const gfx::VRDisplayInfo& info = mClient->GetDisplayInfo();
383 mFrameInfo.Update(info, state, mDepthNear, mDepthFar);
387 bool VRDisplay::GetFrameData(VRFrameData& aFrameData) {
388 UpdateFrameInfo();
389 if (!(mFrameInfo.mVRState.flags &
390 gfx::VRDisplayCapabilityFlags::Cap_Orientation)) {
391 // We must have at minimum Cap_Orientation for a valid pose.
392 return false;
394 aFrameData.Update(mFrameInfo);
395 return true;
398 already_AddRefed<VRPose> VRDisplay::GetPose() {
399 UpdateFrameInfo();
400 RefPtr<VRPose> obj = new VRPose(GetParentObject(), mFrameInfo.mVRState);
402 return obj.forget();
405 void VRDisplay::ResetPose() {
406 // ResetPose is deprecated and unimplemented
407 // We must keep this stub function around as its referenced by
408 // VRDisplay.webidl. Not asserting here, as that could break existing web
409 // content.
412 void VRDisplay::StartVRNavigation() { mClient->StartVRNavigation(); }
414 void VRDisplay::StartHandlingVRNavigationEvent() {
415 mHandlingVRNavigationEventStart = TimeStamp::Now();
416 ++mVRNavigationEventDepth;
417 TimeDuration timeout =
418 TimeDuration::FromMilliseconds(StaticPrefs::dom_vr_navigation_timeout());
419 // A 0 or negative TimeDuration indicates that content may take
420 // as long as it wishes to respond to the event, as long as
421 // it happens before the event exits.
422 if (timeout.ToMilliseconds() > 0) {
423 mClient->StopVRNavigation(timeout);
427 void VRDisplay::StopHandlingVRNavigationEvent() {
428 MOZ_ASSERT(mVRNavigationEventDepth > 0);
429 --mVRNavigationEventDepth;
430 if (mVRNavigationEventDepth == 0) {
431 mClient->StopVRNavigation(TimeDuration::FromMilliseconds(0));
435 bool VRDisplay::IsHandlingVRNavigationEvent() {
436 if (mVRNavigationEventDepth == 0) {
437 return false;
439 if (mHandlingVRNavigationEventStart.IsNull()) {
440 return false;
442 TimeDuration timeout =
443 TimeDuration::FromMilliseconds(StaticPrefs::dom_vr_navigation_timeout());
444 return timeout.ToMilliseconds() <= 0 ||
445 (TimeStamp::Now() - mHandlingVRNavigationEventStart) <= timeout;
448 void VRDisplay::OnPresentationGenerationChanged() { ExitPresentInternal(); }
450 already_AddRefed<Promise> VRDisplay::RequestPresent(
451 const nsTArray<VRLayer>& aLayers, CallerType aCallerType,
452 ErrorResult& aRv) {
453 nsCOMPtr<nsIGlobalObject> global = GetParentObject();
454 if (!global) {
455 aRv.Throw(NS_ERROR_FAILURE);
456 return nullptr;
459 RefPtr<Promise> promise = Promise::Create(global, aRv);
460 NS_ENSURE_TRUE(!aRv.Failed(), nullptr);
462 bool isChromePresentation = aCallerType == CallerType::System;
463 uint32_t presentationGroup =
464 isChromePresentation ? gfx::kVRGroupChrome : gfx::kVRGroupContent;
466 mClient->SetXRAPIMode(gfx::VRAPIMode::WebVR);
467 if (!UserActivation::IsHandlingUserInput() && !isChromePresentation &&
468 !IsHandlingVRNavigationEvent() && StaticPrefs::dom_vr_require_gesture() &&
469 !IsPresenting()) {
470 // The WebVR API states that if called outside of a user gesture, the
471 // promise must be rejected. We allow VR presentations to start within
472 // trusted events such as vrdisplayactivate, which triggers in response to
473 // HMD proximity sensors and when navigating within a VR presentation.
474 // This user gesture requirement is not enforced for chrome/system code.
475 promise->MaybeRejectWithUndefined();
476 } else if (!IsPresenting() && IsAnyPresenting(presentationGroup)) {
477 // Only one presentation allowed per VRDisplay on a
478 // first-come-first-serve basis.
479 // If this Javascript context is presenting, then we can replace our
480 // presentation with a new one containing new layers but we should never
481 // replace the presentation of another context.
482 // Simultaneous presentations in other groups are allowed in separate
483 // Javascript contexts to enable browser UI from chrome/system contexts.
484 // Eventually, this restriction will be loosened to enable multitasking
485 // use cases.
486 promise->MaybeRejectWithUndefined();
487 } else {
488 if (mPresentation) {
489 mPresentation->UpdateLayers(aLayers);
490 } else {
491 mPresentation = mClient->BeginPresentation(aLayers, presentationGroup);
493 mFrameInfo.Clear();
494 promise->MaybeResolve(JS::UndefinedHandleValue);
496 return promise.forget();
499 NS_IMETHODIMP
500 VRDisplay::Observe(nsISupports* aSubject, const char* aTopic,
501 const char16_t* aData) {
502 MOZ_ASSERT(NS_IsMainThread());
504 if (strcmp(aTopic, "inner-window-destroyed") == 0) {
505 nsCOMPtr<nsISupportsPRUint64> wrapper = do_QueryInterface(aSubject);
506 NS_ENSURE_TRUE(wrapper, NS_ERROR_FAILURE);
508 uint64_t innerID;
509 nsresult rv = wrapper->GetData(&innerID);
510 NS_ENSURE_SUCCESS(rv, rv);
512 if (!GetOwner() || GetOwner()->WindowID() == innerID) {
513 Shutdown();
516 return NS_OK;
519 // This should not happen.
520 return NS_ERROR_FAILURE;
523 already_AddRefed<Promise> VRDisplay::ExitPresent(ErrorResult& aRv) {
524 nsCOMPtr<nsIGlobalObject> global = GetParentObject();
525 if (!global) {
526 aRv.Throw(NS_ERROR_FAILURE);
527 return nullptr;
530 RefPtr<Promise> promise = Promise::Create(global, aRv);
531 NS_ENSURE_TRUE(!aRv.Failed(), nullptr);
533 if (!IsPresenting()) {
534 // We can not exit a presentation outside of the context that
535 // started the presentation.
536 promise->MaybeRejectWithUndefined();
537 } else {
538 promise->MaybeResolve(JS::UndefinedHandleValue);
539 ExitPresentInternal();
542 return promise.forget();
545 void VRDisplay::ExitPresentInternal() { mPresentation = nullptr; }
547 void VRDisplay::Shutdown() {
548 mShutdown = true;
549 ExitPresentInternal();
550 nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
551 if (MOZ_LIKELY(obs)) {
552 obs->RemoveObserver(this, "inner-window-destroyed");
556 void VRDisplay::GetLayers(nsTArray<VRLayer>& result) {
557 if (mPresentation) {
558 mPresentation->GetDOMLayers(result);
559 } else {
560 result = nsTArray<VRLayer>();
564 void VRDisplay::SubmitFrame() {
565 AUTO_PROFILER_TRACING_MARKER("VR", "SubmitFrameAtVRDisplay", OTHER);
567 if (mClient && !mClient->IsPresentationGenerationCurrent()) {
568 mPresentation = nullptr;
569 mClient->MakePresentationGenerationCurrent();
572 if (mPresentation) {
573 mPresentation->SubmitFrame();
575 mFrameInfo.Clear();
578 int32_t VRDisplay::RequestAnimationFrame(FrameRequestCallback& aCallback,
579 ErrorResult& aError) {
580 if (mShutdown) {
581 return 0;
584 gfx::VRManagerChild* vm = gfx::VRManagerChild::Get();
586 int32_t handle;
587 aError = vm->ScheduleFrameRequestCallback(aCallback, &handle);
588 return handle;
591 void VRDisplay::CancelAnimationFrame(int32_t aHandle, ErrorResult& aError) {
592 gfx::VRManagerChild* vm = gfx::VRManagerChild::Get();
593 vm->CancelFrameRequestCallback(aHandle);
596 bool VRDisplay::IsPresenting() const {
597 // IsPresenting returns true only if this Javascript context is presenting
598 // and will return false if another context is presenting.
599 return mPresentation != nullptr;
602 bool VRDisplay::IsAnyPresenting(uint32_t aGroupMask) const {
603 // IsAnyPresenting returns true if either this VRDisplay object or any other
604 // from anther Javascript context is presenting with a group matching
605 // aGroupMask.
606 if (mPresentation && (mPresentation->GetGroup() & aGroupMask)) {
607 return true;
609 if (mClient->GetDisplayInfo().GetPresentingGroups() & aGroupMask) {
610 return true;
612 return false;
615 bool VRDisplay::IsConnected() const { return mClient->GetIsConnected(); }
617 uint32_t VRDisplay::PresentingGroups() const {
618 return mClient->GetDisplayInfo().GetPresentingGroups();
621 uint32_t VRDisplay::GroupMask() const {
622 return mClient->GetDisplayInfo().GetGroupMask();
625 void VRDisplay::SetGroupMask(const uint32_t& aGroupMask) {
626 mClient->SetGroupMask(aGroupMask);
629 NS_IMPL_CYCLE_COLLECTION_INHERITED(VRDisplay, DOMEventTargetHelper,
630 mCapabilities, mStageParameters)
632 NS_IMPL_ADDREF_INHERITED(VRDisplay, DOMEventTargetHelper)
633 NS_IMPL_RELEASE_INHERITED(VRDisplay, DOMEventTargetHelper)
635 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(VRDisplay)
636 NS_INTERFACE_MAP_ENTRY(nsIObserver)
637 NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, DOMEventTargetHelper)
638 NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper)
640 NS_IMPL_CYCLE_COLLECTION_CLASS(VRFrameData)
642 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(VRFrameData)
643 NS_IMPL_CYCLE_COLLECTION_UNLINK(mParent, mPose)
644 NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER
645 tmp->mLeftProjectionMatrix = nullptr;
646 tmp->mLeftViewMatrix = nullptr;
647 tmp->mRightProjectionMatrix = nullptr;
648 tmp->mRightViewMatrix = nullptr;
649 NS_IMPL_CYCLE_COLLECTION_UNLINK_END
651 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(VRFrameData)
652 NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mParent, mPose)
653 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
655 NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(VRFrameData)
656 NS_IMPL_CYCLE_COLLECTION_TRACE_PRESERVED_WRAPPER
657 NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mLeftProjectionMatrix)
658 NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mLeftViewMatrix)
659 NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mRightProjectionMatrix)
660 NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mRightViewMatrix)
661 NS_IMPL_CYCLE_COLLECTION_TRACE_END
663 VRFrameData::VRFrameData(nsISupports* aParent)
664 : mParent(aParent),
665 mLeftProjectionMatrix(nullptr),
666 mLeftViewMatrix(nullptr),
667 mRightProjectionMatrix(nullptr),
668 mRightViewMatrix(nullptr) {
669 mozilla::HoldJSObjects(this);
670 mPose = new VRPose(aParent);
673 VRFrameData::~VRFrameData() { mozilla::DropJSObjects(this); }
675 /* static */
676 already_AddRefed<VRFrameData> VRFrameData::Constructor(
677 const GlobalObject& aGlobal) {
678 RefPtr<VRFrameData> obj = new VRFrameData(aGlobal.GetAsSupports());
679 return obj.forget();
682 JSObject* VRFrameData::WrapObject(JSContext* aCx,
683 JS::Handle<JSObject*> aGivenProto) {
684 return VRFrameData_Binding::Wrap(aCx, this, aGivenProto);
687 VRPose* VRFrameData::Pose() { return mPose; }
689 double VRFrameData::Timestamp() const {
690 // Converting from seconds to milliseconds
691 return mFrameInfo.mVRState.timestamp * 1000.0f;
694 void VRFrameData::GetLeftProjectionMatrix(JSContext* aCx,
695 JS::MutableHandle<JSObject*> aRetval,
696 ErrorResult& aRv) {
697 Pose::SetFloat32Array(aCx, this, aRetval, mLeftProjectionMatrix,
698 mFrameInfo.mLeftProjection.components, 16, aRv);
701 void VRFrameData::GetLeftViewMatrix(JSContext* aCx,
702 JS::MutableHandle<JSObject*> aRetval,
703 ErrorResult& aRv) {
704 Pose::SetFloat32Array(aCx, this, aRetval, mLeftViewMatrix,
705 mFrameInfo.mLeftView.components, 16, aRv);
708 void VRFrameData::GetRightProjectionMatrix(JSContext* aCx,
709 JS::MutableHandle<JSObject*> aRetval,
710 ErrorResult& aRv) {
711 Pose::SetFloat32Array(aCx, this, aRetval, mRightProjectionMatrix,
712 mFrameInfo.mRightProjection.components, 16, aRv);
715 void VRFrameData::GetRightViewMatrix(JSContext* aCx,
716 JS::MutableHandle<JSObject*> aRetval,
717 ErrorResult& aRv) {
718 Pose::SetFloat32Array(aCx, this, aRetval, mRightViewMatrix,
719 mFrameInfo.mRightView.components, 16, aRv);
722 void VRFrameData::Update(const VRFrameInfo& aFrameInfo) {
723 mFrameInfo = aFrameInfo;
724 mPose->Update(mFrameInfo.mVRState);
727 void VRFrameInfo::Update(const gfx::VRDisplayInfo& aInfo,
728 const gfx::VRHMDSensorState& aState, float aDepthNear,
729 float aDepthFar) {
730 mVRState = aState;
731 if (mTimeStampOffset == 0.0f) {
733 * A mTimeStampOffset value of 0.0f indicates that this is the first
734 * iteration and an offset has not yet been set.
736 * Generate a value for mTimeStampOffset such that if aState.timestamp is
737 * monotonically increasing, aState.timestamp + mTimeStampOffset will never
738 * be a negative number and will start at a pseudo-random offset
739 * between 1000.0f and 11000.0f seconds.
741 * We use a pseudo random offset rather than 0.0f just to discourage users
742 * from making the assumption that the timestamp returned in the WebVR API
743 * has a base of 0, which is not necessarily true in all UA's.
745 mTimeStampOffset =
746 float(rand()) / float(RAND_MAX) * 10000.0f + 1000.0f - aState.timestamp;
748 mVRState.timestamp = aState.timestamp + mTimeStampOffset;
750 // Avoid division by zero within ConstructProjectionMatrix
751 const float kEpsilon = 0.00001f;
752 if (fabs(aDepthFar - aDepthNear) < kEpsilon) {
753 aDepthFar = aDepthNear + kEpsilon;
756 const gfx::VRFieldOfView leftFOV =
757 aInfo.mDisplayState.eyeFOV[gfx::VRDisplayState::Eye_Left];
758 mLeftProjection =
759 leftFOV.ConstructProjectionMatrix(aDepthNear, aDepthFar, true);
760 const gfx::VRFieldOfView rightFOV =
761 aInfo.mDisplayState.eyeFOV[gfx::VRDisplayState::Eye_Right];
762 mRightProjection =
763 rightFOV.ConstructProjectionMatrix(aDepthNear, aDepthFar, true);
764 memcpy(mLeftView.components, aState.leftViewMatrix,
765 sizeof(aState.leftViewMatrix));
766 memcpy(mRightView.components, aState.rightViewMatrix,
767 sizeof(aState.rightViewMatrix));
770 VRFrameInfo::VRFrameInfo() : mTimeStampOffset(0.0f) {
771 mVRState.inputFrameID = 0;
772 mVRState.timestamp = 0.0;
773 mVRState.flags = gfx::VRDisplayCapabilityFlags::Cap_None;
776 bool VRFrameInfo::IsDirty() { return mVRState.timestamp == 0; }
778 void VRFrameInfo::Clear() { mVRState.Clear(); }
780 } // namespace mozilla::dom