2015-05-03 22:32:37 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2014-07-09 23:24:49 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include "nsWrapperCache.h"
|
|
|
|
|
|
|
|
#include "mozilla/dom/Element.h"
|
|
|
|
#include "mozilla/dom/ElementBinding.h"
|
2016-08-18 19:21:26 +03:00
|
|
|
#include "mozilla/dom/Promise.h"
|
2019-09-20 23:51:25 +03:00
|
|
|
#include "mozilla/dom/UserActivation.h"
|
2016-04-13 03:39:28 +03:00
|
|
|
#include "mozilla/dom/VRDisplay.h"
|
2019-09-20 14:05:12 +03:00
|
|
|
#include "mozilla/dom/VRDisplayBinding.h"
|
2019-09-20 23:51:25 +03:00
|
|
|
#include "mozilla/HoldDropJSObjects.h"
|
2017-05-23 12:13:15 +03:00
|
|
|
#include "mozilla/Base64.h"
|
2019-07-26 04:10:23 +03:00
|
|
|
#include "mozilla/StaticPrefs_dom.h"
|
2017-05-23 12:13:15 +03:00
|
|
|
#include "mozilla/gfx/DataSurfaceHelpers.h"
|
2015-09-18 00:23:13 +03:00
|
|
|
#include "Navigator.h"
|
2017-05-23 12:13:15 +03:00
|
|
|
#include "gfxUtils.h"
|
2014-07-09 23:24:49 +04:00
|
|
|
#include "gfxVR.h"
|
2016-02-25 02:54:50 +03:00
|
|
|
#include "VRDisplayClient.h"
|
2015-09-18 00:23:13 +03:00
|
|
|
#include "VRManagerChild.h"
|
2016-02-25 02:54:50 +03:00
|
|
|
#include "VRDisplayPresentation.h"
|
|
|
|
#include "nsIObserverService.h"
|
2014-07-09 23:24:49 +04:00
|
|
|
#include "nsIFrame.h"
|
2016-02-25 02:54:50 +03:00
|
|
|
#include "nsISupportsPrimitives.h"
|
2014-07-09 23:24:49 +04:00
|
|
|
|
|
|
|
using namespace mozilla::gfx;
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2016-02-25 02:54:50 +03:00
|
|
|
VRFieldOfView::VRFieldOfView(nsISupports* aParent, double aUpDegrees,
|
|
|
|
double aRightDegrees, double aDownDegrees,
|
|
|
|
double aLeftDegrees)
|
|
|
|
: mParent(aParent),
|
|
|
|
mUpDegrees(aUpDegrees),
|
|
|
|
mRightDegrees(aRightDegrees),
|
|
|
|
mDownDegrees(aDownDegrees),
|
|
|
|
mLeftDegrees(aLeftDegrees) {}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-02-25 02:54:50 +03:00
|
|
|
VRFieldOfView::VRFieldOfView(nsISupports* aParent,
|
|
|
|
const gfx::VRFieldOfView& aSrc)
|
|
|
|
: mParent(aParent),
|
|
|
|
mUpDegrees(aSrc.upDegrees),
|
|
|
|
mRightDegrees(aSrc.rightDegrees),
|
|
|
|
mDownDegrees(aSrc.downDegrees),
|
|
|
|
mLeftDegrees(aSrc.leftDegrees) {}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-02-25 02:54:50 +03:00
|
|
|
bool VRDisplayCapabilities::HasPosition() const {
|
2020-04-21 14:17:34 +03:00
|
|
|
return bool(mFlags & gfx::VRDisplayCapabilityFlags::Cap_Position) ||
|
|
|
|
bool(mFlags & gfx::VRDisplayCapabilityFlags::Cap_PositionEmulated);
|
2016-02-25 02:54:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool VRDisplayCapabilities::HasOrientation() const {
|
|
|
|
return bool(mFlags & gfx::VRDisplayCapabilityFlags::Cap_Orientation);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool VRDisplayCapabilities::HasExternalDisplay() const {
|
|
|
|
return bool(mFlags & gfx::VRDisplayCapabilityFlags::Cap_External);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool VRDisplayCapabilities::CanPresent() const {
|
|
|
|
return bool(mFlags & gfx::VRDisplayCapabilityFlags::Cap_Present);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t VRDisplayCapabilities::MaxLayers() const {
|
|
|
|
return CanPresent() ? 1 : 0;
|
|
|
|
}
|
|
|
|
|
2019-10-29 03:35:35 +03:00
|
|
|
void VRDisplay::UpdateDisplayClient(
|
|
|
|
already_AddRefed<gfx::VRDisplayClient> aClient) {
|
|
|
|
mClient = std::move(aClient);
|
|
|
|
}
|
|
|
|
|
2019-02-26 01:05:29 +03:00
|
|
|
/*static*/
|
|
|
|
bool VRDisplay::RefreshVRDisplays(uint64_t aWindowId) {
|
2015-09-18 00:23:13 +03:00
|
|
|
gfx::VRManagerChild* vm = gfx::VRManagerChild::Get();
|
2016-11-24 22:50:43 +03:00
|
|
|
return vm && vm->RefreshVRDisplaysWithCallback(aWindowId);
|
2015-09-18 00:23:13 +03:00
|
|
|
}
|
|
|
|
|
2019-02-26 01:05:29 +03:00
|
|
|
/*static*/
|
|
|
|
void VRDisplay::UpdateVRDisplays(nsTArray<RefPtr<VRDisplay>>& aDisplays,
|
|
|
|
nsPIDOMWindowInner* aWindow) {
|
2016-02-25 02:54:50 +03:00
|
|
|
nsTArray<RefPtr<VRDisplay>> displays;
|
2015-09-18 00:23:13 +03:00
|
|
|
|
|
|
|
gfx::VRManagerChild* vm = gfx::VRManagerChild::Get();
|
2016-02-25 02:54:50 +03:00
|
|
|
nsTArray<RefPtr<gfx::VRDisplayClient>> updatedDisplays;
|
2019-08-22 17:59:53 +03:00
|
|
|
if (vm) {
|
|
|
|
vm->GetVRDisplays(updatedDisplays);
|
2016-02-25 02:54:50 +03:00
|
|
|
for (size_t i = 0; i < updatedDisplays.Length(); i++) {
|
|
|
|
RefPtr<gfx::VRDisplayClient> display = updatedDisplays[i];
|
|
|
|
bool isNewDisplay = true;
|
|
|
|
for (size_t j = 0; j < aDisplays.Length(); j++) {
|
2018-01-05 01:27:03 +03:00
|
|
|
if (aDisplays[j]->GetClient()->GetDisplayInfo().GetDisplayID() ==
|
|
|
|
display->GetDisplayInfo().GetDisplayID()) {
|
2016-02-25 02:54:50 +03:00
|
|
|
displays.AppendElement(aDisplays[j]);
|
|
|
|
isNewDisplay = false;
|
2019-10-29 03:35:35 +03:00
|
|
|
} else {
|
|
|
|
RefPtr<gfx::VRDisplayClient> ref = display;
|
|
|
|
aDisplays[j]->UpdateDisplayClient(do_AddRef(display));
|
|
|
|
displays.AppendElement(aDisplays[j]);
|
|
|
|
isNewDisplay = false;
|
2015-09-18 00:23:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-25 02:54:50 +03:00
|
|
|
if (isNewDisplay) {
|
|
|
|
displays.AppendElement(new VRDisplay(aWindow, display));
|
2015-09-18 00:23:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-06 14:46:55 +03:00
|
|
|
aDisplays = std::move(displays);
|
2015-04-01 23:02:20 +03:00
|
|
|
}
|
|
|
|
|
2016-02-25 02:54:50 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(VRFieldOfView, mParent)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_ROOT_NATIVE(VRFieldOfView, AddRef)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNROOT_NATIVE(VRFieldOfView, Release)
|
2014-07-09 23:24:49 +04:00
|
|
|
|
2015-01-09 00:56:42 +03:00
|
|
|
JSObject* VRFieldOfView::WrapObject(JSContext* aCx,
|
2015-04-01 23:02:20 +03:00
|
|
|
JS::Handle<JSObject*> aGivenProto) {
|
2018-06-26 00:20:54 +03:00
|
|
|
return VRFieldOfView_Binding::Wrap(aCx, this, aGivenProto);
|
2015-04-01 23:02:20 +03:00
|
|
|
}
|
|
|
|
|
2016-08-17 23:39:54 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(VREyeParameters)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(VREyeParameters)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mParent, mFOV)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER
|
|
|
|
tmp->mOffset = nullptr;
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(VREyeParameters)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mParent, mFOV)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(VREyeParameters)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_PRESERVED_WRAPPER
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mOffset)
|
2016-02-25 02:54:50 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_END
|
|
|
|
|
2015-04-01 23:02:20 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_ROOT_NATIVE(VREyeParameters, AddRef)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNROOT_NATIVE(VREyeParameters, Release)
|
|
|
|
|
|
|
|
VREyeParameters::VREyeParameters(nsISupports* aParent,
|
|
|
|
const gfx::Point3D& aEyeTranslation,
|
2016-02-25 02:54:50 +03:00
|
|
|
const gfx::VRFieldOfView& aFOV,
|
|
|
|
const gfx::IntSize& aRenderSize)
|
2015-04-01 23:02:20 +03:00
|
|
|
: mParent(aParent),
|
2016-02-25 02:54:50 +03:00
|
|
|
mEyeTranslation(aEyeTranslation),
|
|
|
|
mRenderSize(aRenderSize) {
|
|
|
|
mFOV = new VRFieldOfView(aParent, aFOV);
|
2016-08-17 23:39:54 +03:00
|
|
|
mozilla::HoldJSObjects(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
VREyeParameters::~VREyeParameters() { mozilla::DropJSObjects(this); }
|
2015-04-01 23:02:20 +03:00
|
|
|
|
2016-02-25 02:54:50 +03:00
|
|
|
VRFieldOfView* VREyeParameters::FieldOfView() { return mFOV; }
|
2015-04-01 23:02:20 +03:00
|
|
|
|
2016-02-25 02:54:50 +03:00
|
|
|
void VREyeParameters::GetOffset(JSContext* aCx,
|
|
|
|
JS::MutableHandle<JSObject*> aRetval,
|
|
|
|
ErrorResult& aRv) {
|
|
|
|
if (!mOffset) {
|
|
|
|
// Lazily create the Float32Array
|
|
|
|
mOffset =
|
|
|
|
dom::Float32Array::Create(aCx, this, 3, mEyeTranslation.components);
|
|
|
|
if (!mOffset) {
|
|
|
|
aRv.NoteJSContextException(aCx);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
aRetval.set(mOffset);
|
2015-04-01 23:02:20 +03:00
|
|
|
}
|
|
|
|
|
2016-02-25 02:54:50 +03:00
|
|
|
JSObject* VREyeParameters::WrapObject(JSContext* aCx,
|
|
|
|
JS::Handle<JSObject*> aGivenProto) {
|
2018-06-26 00:20:54 +03:00
|
|
|
return VREyeParameters_Binding::Wrap(aCx, this, aGivenProto);
|
2015-04-01 23:02:20 +03:00
|
|
|
}
|
|
|
|
|
2016-08-17 23:39:54 +03:00
|
|
|
VRStageParameters::VRStageParameters(
|
|
|
|
nsISupports* aParent, const gfx::Matrix4x4& aSittingToStandingTransform,
|
|
|
|
const gfx::Size& aSize)
|
|
|
|
: mParent(aParent),
|
|
|
|
mSittingToStandingTransform(aSittingToStandingTransform),
|
|
|
|
mSittingToStandingTransformArray(nullptr),
|
|
|
|
mSize(aSize) {
|
|
|
|
mozilla::HoldJSObjects(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
VRStageParameters::~VRStageParameters() { mozilla::DropJSObjects(this); }
|
|
|
|
|
2016-02-25 02:54:50 +03:00
|
|
|
JSObject* VRStageParameters::WrapObject(JSContext* aCx,
|
|
|
|
JS::Handle<JSObject*> aGivenProto) {
|
2018-06-26 00:20:54 +03:00
|
|
|
return VRStageParameters_Binding::Wrap(aCx, this, aGivenProto);
|
2015-04-01 23:02:20 +03:00
|
|
|
}
|
|
|
|
|
2016-08-17 23:39:54 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(VRStageParameters)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(VRStageParameters)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mParent)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER
|
|
|
|
tmp->mSittingToStandingTransformArray = nullptr;
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(VRStageParameters)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mParent)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(VRStageParameters)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_PRESERVED_WRAPPER
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(
|
|
|
|
mSittingToStandingTransformArray)
|
2016-02-25 02:54:50 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_ROOT_NATIVE(VRStageParameters, AddRef)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNROOT_NATIVE(VRStageParameters, Release)
|
2015-04-01 23:02:20 +03:00
|
|
|
|
2016-02-25 02:54:50 +03:00
|
|
|
void VRStageParameters::GetSittingToStandingTransform(
|
|
|
|
JSContext* aCx, JS::MutableHandle<JSObject*> aRetval, ErrorResult& aRv) {
|
|
|
|
if (!mSittingToStandingTransformArray) {
|
|
|
|
// Lazily create the Float32Array
|
|
|
|
mSittingToStandingTransformArray = dom::Float32Array::Create(
|
|
|
|
aCx, this, 16, mSittingToStandingTransform.components);
|
|
|
|
if (!mSittingToStandingTransformArray) {
|
|
|
|
aRv.NoteJSContextException(aCx);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
aRetval.set(mSittingToStandingTransformArray);
|
2015-04-01 23:02:20 +03:00
|
|
|
}
|
|
|
|
|
2016-02-25 02:54:50 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(VRDisplayCapabilities, mParent)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_ROOT_NATIVE(VRDisplayCapabilities, AddRef)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNROOT_NATIVE(VRDisplayCapabilities, Release)
|
|
|
|
|
|
|
|
JSObject* VRDisplayCapabilities::WrapObject(JSContext* aCx,
|
|
|
|
JS::Handle<JSObject*> aGivenProto) {
|
2018-06-26 00:20:54 +03:00
|
|
|
return VRDisplayCapabilities_Binding::Wrap(aCx, this, aGivenProto);
|
2014-07-09 23:24:49 +04:00
|
|
|
}
|
|
|
|
|
2016-02-25 02:54:50 +03:00
|
|
|
VRPose::VRPose(nsISupports* aParent, const gfx::VRHMDSensorState& aState)
|
2014-07-09 23:24:49 +04:00
|
|
|
: Pose(aParent), mVRState(aState) {
|
2016-08-17 23:39:54 +03:00
|
|
|
mozilla::HoldJSObjects(this);
|
|
|
|
}
|
|
|
|
|
2016-10-01 02:43:33 +03:00
|
|
|
VRPose::VRPose(nsISupports* aParent) : Pose(aParent) {
|
2018-06-15 18:16:15 +03:00
|
|
|
mVRState.inputFrameID = 0;
|
|
|
|
mVRState.timestamp = 0.0;
|
|
|
|
mVRState.flags = gfx::VRDisplayCapabilityFlags::Cap_None;
|
2016-10-01 02:43:33 +03:00
|
|
|
mozilla::HoldJSObjects(this);
|
|
|
|
}
|
|
|
|
|
2016-08-17 23:39:54 +03:00
|
|
|
VRPose::~VRPose() { mozilla::DropJSObjects(this); }
|
2014-07-09 23:24:49 +04:00
|
|
|
|
2016-02-25 02:54:50 +03:00
|
|
|
void VRPose::GetPosition(JSContext* aCx, JS::MutableHandle<JSObject*> aRetval,
|
|
|
|
ErrorResult& aRv) {
|
2019-08-20 21:34:24 +03:00
|
|
|
const bool valid =
|
|
|
|
bool(mVRState.flags & gfx::VRDisplayCapabilityFlags::Cap_Position) ||
|
|
|
|
bool(mVRState.flags &
|
|
|
|
gfx::VRDisplayCapabilityFlags::Cap_PositionEmulated);
|
|
|
|
SetFloat32Array(aCx, this, aRetval, mPosition,
|
|
|
|
valid ? mVRState.pose.position : nullptr, 3, aRv);
|
2014-07-09 23:24:49 +04:00
|
|
|
}
|
|
|
|
|
2016-02-25 02:54:50 +03:00
|
|
|
void VRPose::GetLinearVelocity(JSContext* aCx,
|
|
|
|
JS::MutableHandle<JSObject*> aRetval,
|
|
|
|
ErrorResult& aRv) {
|
2019-08-20 21:34:24 +03:00
|
|
|
const bool valid =
|
2020-04-21 14:17:34 +03:00
|
|
|
bool(mVRState.flags & gfx::VRDisplayCapabilityFlags::Cap_Position) ||
|
|
|
|
bool(mVRState.flags &
|
|
|
|
gfx::VRDisplayCapabilityFlags::Cap_PositionEmulated);
|
2019-08-20 21:34:24 +03:00
|
|
|
SetFloat32Array(aCx, this, aRetval, mLinearVelocity,
|
|
|
|
valid ? mVRState.pose.linearVelocity : nullptr, 3, aRv);
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2016-02-25 02:54:50 +03:00
|
|
|
void VRPose::GetLinearAcceleration(JSContext* aCx,
|
2018-07-11 22:20:51 +03:00
|
|
|
JS::MutableHandle<JSObject*> aRetval,
|
|
|
|
ErrorResult& aRv) {
|
2019-08-20 21:34:24 +03:00
|
|
|
const bool valid = bool(
|
|
|
|
mVRState.flags & gfx::VRDisplayCapabilityFlags::Cap_LinearAcceleration);
|
|
|
|
SetFloat32Array(aCx, this, aRetval, mLinearAcceleration,
|
|
|
|
valid ? mVRState.pose.linearAcceleration : nullptr, 3, aRv);
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2016-02-25 02:54:50 +03:00
|
|
|
void VRPose::GetOrientation(JSContext* aCx,
|
2016-10-24 13:04:41 +03:00
|
|
|
JS::MutableHandle<JSObject*> aRetval,
|
|
|
|
ErrorResult& aRv) {
|
2019-08-20 21:34:24 +03:00
|
|
|
const bool valid =
|
|
|
|
bool(mVRState.flags & gfx::VRDisplayCapabilityFlags::Cap_Orientation);
|
|
|
|
SetFloat32Array(aCx, this, aRetval, mOrientation,
|
|
|
|
valid ? mVRState.pose.orientation : nullptr, 4, aRv);
|
2014-07-09 23:24:49 +04:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-07-09 23:24:49 +04:00
|
|
|
void VRPose::GetAngularVelocity(JSContext* aCx,
|
|
|
|
JS::MutableHandle<JSObject*> aRetval,
|
2016-02-25 02:54:50 +03:00
|
|
|
ErrorResult& aRv) {
|
2019-08-20 21:34:24 +03:00
|
|
|
const bool valid =
|
|
|
|
bool(mVRState.flags & gfx::VRDisplayCapabilityFlags::Cap_Orientation);
|
|
|
|
SetFloat32Array(aCx, this, aRetval, mAngularVelocity,
|
|
|
|
valid ? mVRState.pose.angularVelocity : nullptr, 3, aRv);
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2016-02-25 02:54:50 +03:00
|
|
|
void VRPose::GetAngularAcceleration(JSContext* aCx,
|
|
|
|
JS::MutableHandle<JSObject*> aRetval,
|
|
|
|
ErrorResult& aRv) {
|
2019-08-20 21:34:24 +03:00
|
|
|
const bool valid = bool(
|
|
|
|
mVRState.flags & gfx::VRDisplayCapabilityFlags::Cap_AngularAcceleration);
|
|
|
|
SetFloat32Array(aCx, this, aRetval, mAngularAcceleration,
|
|
|
|
valid ? mVRState.pose.angularAcceleration : nullptr, 3, aRv);
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2019-07-30 03:23:38 +03:00
|
|
|
void VRPose::Update(const gfx::VRHMDSensorState& aState) { mVRState = aState; }
|
|
|
|
|
2016-02-25 02:54:50 +03:00
|
|
|
JSObject* VRPose::WrapObject(JSContext* aCx,
|
|
|
|
JS::Handle<JSObject*> aGivenProto) {
|
2018-06-26 00:20:54 +03:00
|
|
|
return VRPose_Binding::Wrap(aCx, this, aGivenProto);
|
2014-07-09 23:24:49 +04:00
|
|
|
}
|
|
|
|
|
2019-02-26 01:05:29 +03:00
|
|
|
/* virtual */
|
|
|
|
JSObject* VRDisplay::WrapObject(JSContext* aCx,
|
|
|
|
JS::Handle<JSObject*> aGivenProto) {
|
2018-06-26 00:20:54 +03:00
|
|
|
return VRDisplay_Binding::Wrap(aCx, this, aGivenProto);
|
2016-02-25 02:54:50 +03:00
|
|
|
}
|
2014-07-09 23:24:49 +04:00
|
|
|
|
2016-02-25 02:54:50 +03:00
|
|
|
VRDisplay::VRDisplay(nsPIDOMWindowInner* aWindow, gfx::VRDisplayClient* aClient)
|
|
|
|
: DOMEventTargetHelper(aWindow),
|
|
|
|
mClient(aClient),
|
|
|
|
mDepthNear(0.01f) // Default value from WebVR Spec
|
|
|
|
,
|
|
|
|
mDepthFar(10000.0f) // Default value from WebVR Spec
|
2017-04-04 21:27:14 +03:00
|
|
|
,
|
|
|
|
mVRNavigationEventDepth(0),
|
2017-04-13 02:22:29 +03:00
|
|
|
mShutdown(false) {
|
2016-07-22 22:41:00 +03:00
|
|
|
const gfx::VRDisplayInfo& info = aClient->GetDisplayInfo();
|
|
|
|
mCapabilities = new VRDisplayCapabilities(aWindow, info.GetCapabilities());
|
|
|
|
if (info.GetCapabilities() &
|
|
|
|
gfx::VRDisplayCapabilityFlags::Cap_StageParameters) {
|
|
|
|
mStageParameters = new VRStageParameters(
|
|
|
|
aWindow, info.GetSittingToStandingTransform(), info.GetStageSize());
|
|
|
|
}
|
2016-08-17 23:39:54 +03:00
|
|
|
mozilla::HoldJSObjects(this);
|
2017-04-13 02:22:29 +03:00
|
|
|
nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
|
|
|
|
if (MOZ_LIKELY(obs)) {
|
|
|
|
obs->AddObserver(this, "inner-window-destroyed", false);
|
|
|
|
}
|
2016-02-25 02:54:50 +03:00
|
|
|
}
|
2014-07-09 23:24:49 +04:00
|
|
|
|
2016-02-25 02:54:50 +03:00
|
|
|
VRDisplay::~VRDisplay() {
|
2017-04-13 02:22:29 +03:00
|
|
|
MOZ_ASSERT(mShutdown);
|
2016-08-17 23:39:54 +03:00
|
|
|
mozilla::DropJSObjects(this);
|
2014-07-09 23:24:49 +04:00
|
|
|
}
|
|
|
|
|
2016-02-25 02:54:50 +03:00
|
|
|
void VRDisplay::LastRelease() {
|
|
|
|
// We don't want to wait for the CC to free up the presentation
|
|
|
|
// for use in other documents, so we do this in LastRelease().
|
2017-04-13 02:22:29 +03:00
|
|
|
Shutdown();
|
2014-07-09 23:24:49 +04:00
|
|
|
}
|
|
|
|
|
2016-02-25 02:54:50 +03:00
|
|
|
already_AddRefed<VREyeParameters> VRDisplay::GetEyeParameters(VREye aEye) {
|
2018-03-14 03:09:54 +03:00
|
|
|
gfx::VRDisplayState::Eye eye = aEye == VREye::Left
|
|
|
|
? gfx::VRDisplayState::Eye_Left
|
|
|
|
: gfx::VRDisplayState::Eye_Right;
|
2016-02-25 02:54:50 +03:00
|
|
|
RefPtr<VREyeParameters> params = new VREyeParameters(
|
|
|
|
GetParentObject(), mClient->GetDisplayInfo().GetEyeTranslation(eye),
|
|
|
|
mClient->GetDisplayInfo().GetEyeFOV(eye),
|
|
|
|
mClient->GetDisplayInfo().SuggestedEyeResolution());
|
|
|
|
return params.forget();
|
|
|
|
}
|
2015-04-01 23:02:20 +03:00
|
|
|
|
2016-02-25 02:54:50 +03:00
|
|
|
VRDisplayCapabilities* VRDisplay::Capabilities() { return mCapabilities; }
|
2015-04-01 23:02:20 +03:00
|
|
|
|
2016-02-25 02:54:50 +03:00
|
|
|
VRStageParameters* VRDisplay::GetStageParameters() { return mStageParameters; }
|
2015-04-07 20:26:44 +03:00
|
|
|
|
2019-10-29 03:42:52 +03:00
|
|
|
uint32_t VRDisplay::DisplayId() const {
|
|
|
|
const gfx::VRDisplayInfo& info = mClient->GetDisplayInfo();
|
|
|
|
return info.GetDisplayID();
|
|
|
|
}
|
|
|
|
|
|
|
|
void VRDisplay::GetDisplayName(nsAString& aDisplayName) const {
|
|
|
|
const gfx::VRDisplayInfo& info = mClient->GetDisplayInfo();
|
|
|
|
aDisplayName = NS_ConvertUTF8toUTF16(info.GetDisplayName());
|
|
|
|
}
|
|
|
|
|
2016-10-07 02:32:16 +03:00
|
|
|
void VRDisplay::UpdateFrameInfo() {
|
|
|
|
/**
|
|
|
|
* The WebVR 1.1 spec Requires that VRDisplay.getPose and
|
|
|
|
* VRDisplay.getFrameData must return the same values until the next
|
|
|
|
* VRDisplay.submitFrame.
|
|
|
|
*
|
|
|
|
* mFrameInfo is marked dirty at the end of the frame or start of a new
|
|
|
|
* composition and lazily created here in order to receive mid-frame
|
|
|
|
* pose-prediction updates while still ensuring conformance to the WebVR spec
|
|
|
|
* requirements.
|
|
|
|
*
|
|
|
|
* If we are not presenting WebVR content, the frame will never end and we
|
|
|
|
* should return the latest frame data always.
|
|
|
|
*/
|
2019-11-01 01:10:43 +03:00
|
|
|
mFrameInfo.Clear();
|
|
|
|
|
|
|
|
if ((mFrameInfo.IsDirty() && IsPresenting()) ||
|
|
|
|
mClient->GetDisplayInfo().GetPresentingGroups() == 0) {
|
2018-12-18 22:39:44 +03:00
|
|
|
const gfx::VRHMDSensorState& state = mClient->GetSensorState();
|
2016-10-07 02:32:16 +03:00
|
|
|
const gfx::VRDisplayInfo& info = mClient->GetDisplayInfo();
|
|
|
|
mFrameInfo.Update(info, state, mDepthNear, mDepthFar);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-01 02:43:33 +03:00
|
|
|
bool VRDisplay::GetFrameData(VRFrameData& aFrameData) {
|
2016-10-07 02:32:16 +03:00
|
|
|
UpdateFrameInfo();
|
2017-09-27 23:54:58 +03:00
|
|
|
if (!(mFrameInfo.mVRState.flags &
|
|
|
|
gfx::VRDisplayCapabilityFlags::Cap_Orientation)) {
|
|
|
|
// We must have at minimum Cap_Orientation for a valid pose.
|
|
|
|
return false;
|
|
|
|
}
|
2016-10-07 02:32:16 +03:00
|
|
|
aFrameData.Update(mFrameInfo);
|
2016-10-01 02:43:33 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-02-25 02:54:50 +03:00
|
|
|
already_AddRefed<VRPose> VRDisplay::GetPose() {
|
2016-10-07 02:32:16 +03:00
|
|
|
UpdateFrameInfo();
|
|
|
|
RefPtr<VRPose> obj = new VRPose(GetParentObject(), mFrameInfo.mVRState);
|
2014-07-09 23:24:49 +04:00
|
|
|
|
2016-02-25 02:54:50 +03:00
|
|
|
return obj.forget();
|
2015-09-18 00:23:13 +03:00
|
|
|
}
|
2014-07-09 23:24:49 +04:00
|
|
|
|
2019-06-29 00:19:54 +03:00
|
|
|
void VRDisplay::ResetPose() {
|
|
|
|
// ResetPose is deprecated and unimplemented
|
|
|
|
// We must keep this stub function around as its referenced by
|
|
|
|
// VRDisplay.webidl. Not asserting here, as that could break existing web
|
|
|
|
// content.
|
|
|
|
}
|
2014-07-09 23:24:49 +04:00
|
|
|
|
2018-07-31 04:41:25 +03:00
|
|
|
void VRDisplay::StartVRNavigation() { mClient->StartVRNavigation(); }
|
|
|
|
|
2017-04-04 21:27:14 +03:00
|
|
|
void VRDisplay::StartHandlingVRNavigationEvent() {
|
|
|
|
mHandlingVRNavigationEventStart = TimeStamp::Now();
|
|
|
|
++mVRNavigationEventDepth;
|
2018-07-31 04:41:25 +03:00
|
|
|
TimeDuration timeout =
|
2019-06-25 09:32:29 +03:00
|
|
|
TimeDuration::FromMilliseconds(StaticPrefs::dom_vr_navigation_timeout());
|
2018-07-31 04:41:25 +03:00
|
|
|
// A 0 or negative TimeDuration indicates that content may take
|
|
|
|
// as long as it wishes to respond to the event, as long as
|
|
|
|
// it happens before the event exits.
|
|
|
|
if (timeout.ToMilliseconds() > 0) {
|
|
|
|
mClient->StopVRNavigation(timeout);
|
|
|
|
}
|
2017-04-04 21:27:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void VRDisplay::StopHandlingVRNavigationEvent() {
|
|
|
|
MOZ_ASSERT(mVRNavigationEventDepth > 0);
|
|
|
|
--mVRNavigationEventDepth;
|
2018-07-31 04:41:25 +03:00
|
|
|
if (mVRNavigationEventDepth == 0) {
|
|
|
|
mClient->StopVRNavigation(TimeDuration::FromMilliseconds(0));
|
|
|
|
}
|
2017-04-04 21:27:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool VRDisplay::IsHandlingVRNavigationEvent() {
|
|
|
|
if (mVRNavigationEventDepth == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (mHandlingVRNavigationEventStart.IsNull()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
TimeDuration timeout =
|
2019-06-25 09:32:29 +03:00
|
|
|
TimeDuration::FromMilliseconds(StaticPrefs::dom_vr_navigation_timeout());
|
2018-07-31 04:41:25 +03:00
|
|
|
return timeout.ToMilliseconds() <= 0 ||
|
2017-04-04 21:27:14 +03:00
|
|
|
(TimeStamp::Now() - mHandlingVRNavigationEventStart) <= timeout;
|
|
|
|
}
|
|
|
|
|
2018-08-08 02:09:42 +03:00
|
|
|
void VRDisplay::OnPresentationGenerationChanged() { ExitPresentInternal(); }
|
|
|
|
|
2016-02-25 02:54:50 +03:00
|
|
|
already_AddRefed<Promise> VRDisplay::RequestPresent(
|
2017-03-08 03:04:12 +03:00
|
|
|
const nsTArray<VRLayer>& aLayers, CallerType aCallerType,
|
|
|
|
ErrorResult& aRv) {
|
2018-10-02 00:38:01 +03:00
|
|
|
nsCOMPtr<nsIGlobalObject> global = GetParentObject();
|
2016-02-25 02:54:50 +03:00
|
|
|
if (!global) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
2014-07-09 23:24:49 +04:00
|
|
|
}
|
|
|
|
|
2016-02-25 02:54:50 +03:00
|
|
|
RefPtr<Promise> promise = Promise::Create(global, aRv);
|
|
|
|
NS_ENSURE_TRUE(!aRv.Failed(), nullptr);
|
|
|
|
|
2017-05-09 02:01:36 +03:00
|
|
|
bool isChromePresentation = aCallerType == CallerType::System;
|
|
|
|
uint32_t presentationGroup =
|
|
|
|
isChromePresentation ? gfx::kVRGroupChrome : gfx::kVRGroupContent;
|
|
|
|
|
2020-04-08 06:23:47 +03:00
|
|
|
mClient->SetXRAPIMode(gfx::VRAPIMode::WebVR);
|
2019-09-20 23:51:25 +03:00
|
|
|
if (!UserActivation::IsHandlingUserInput() && !isChromePresentation &&
|
2019-06-25 09:32:29 +03:00
|
|
|
!IsHandlingVRNavigationEvent() && StaticPrefs::dom_vr_require_gesture() &&
|
2017-08-29 02:20:59 +03:00
|
|
|
!IsPresenting()) {
|
2017-03-08 03:04:12 +03:00
|
|
|
// The WebVR API states that if called outside of a user gesture, the
|
|
|
|
// promise must be rejected. We allow VR presentations to start within
|
|
|
|
// trusted events such as vrdisplayactivate, which triggers in response to
|
|
|
|
// HMD proximity sensors and when navigating within a VR presentation.
|
2017-05-09 02:01:36 +03:00
|
|
|
// This user gesture requirement is not enforced for chrome/system code.
|
2017-03-08 03:04:12 +03:00
|
|
|
promise->MaybeRejectWithUndefined();
|
2017-05-09 02:01:36 +03:00
|
|
|
} else if (!IsPresenting() && IsAnyPresenting(presentationGroup)) {
|
|
|
|
// Only one presentation allowed per VRDisplay on a
|
|
|
|
// first-come-first-serve basis.
|
2016-10-19 01:18:10 +03:00
|
|
|
// If this Javascript context is presenting, then we can replace our
|
|
|
|
// presentation with a new one containing new layers but we should never
|
|
|
|
// replace the presentation of another context.
|
2017-05-09 02:01:36 +03:00
|
|
|
// Simultaneous presentations in other groups are allowed in separate
|
|
|
|
// Javascript contexts to enable browser UI from chrome/system contexts.
|
|
|
|
// Eventually, this restriction will be loosened to enable multitasking
|
|
|
|
// use cases.
|
2016-02-25 02:54:50 +03:00
|
|
|
promise->MaybeRejectWithUndefined();
|
|
|
|
} else {
|
2017-08-29 02:20:59 +03:00
|
|
|
if (mPresentation) {
|
|
|
|
mPresentation->UpdateLayers(aLayers);
|
|
|
|
} else {
|
|
|
|
mPresentation = mClient->BeginPresentation(aLayers, presentationGroup);
|
|
|
|
}
|
2016-10-07 02:32:16 +03:00
|
|
|
mFrameInfo.Clear();
|
2017-04-13 02:22:29 +03:00
|
|
|
promise->MaybeResolve(JS::UndefinedHandleValue);
|
2014-07-09 23:24:49 +04:00
|
|
|
}
|
2016-02-25 02:54:50 +03:00
|
|
|
return promise.forget();
|
2015-09-18 00:23:13 +03:00
|
|
|
}
|
2015-04-01 23:02:20 +03:00
|
|
|
|
2016-08-17 23:39:54 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
VRDisplay::Observe(nsISupports* aSubject, const char* aTopic,
|
|
|
|
const char16_t* aData) {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
if (strcmp(aTopic, "inner-window-destroyed") == 0) {
|
|
|
|
nsCOMPtr<nsISupportsPRUint64> wrapper = do_QueryInterface(aSubject);
|
|
|
|
NS_ENSURE_TRUE(wrapper, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
uint64_t innerID;
|
|
|
|
nsresult rv = wrapper->GetData(&innerID);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (!GetOwner() || GetOwner()->WindowID() == innerID) {
|
2017-04-13 02:22:29 +03:00
|
|
|
Shutdown();
|
2016-08-17 23:39:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This should not happen.
|
|
|
|
return NS_ERROR_FAILURE;
|
2015-09-18 00:23:13 +03:00
|
|
|
}
|
2015-04-01 23:02:20 +03:00
|
|
|
|
2016-02-25 02:54:50 +03:00
|
|
|
already_AddRefed<Promise> VRDisplay::ExitPresent(ErrorResult& aRv) {
|
2018-10-02 00:38:01 +03:00
|
|
|
nsCOMPtr<nsIGlobalObject> global = GetParentObject();
|
2016-02-25 02:54:50 +03:00
|
|
|
if (!global) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2016-10-12 01:26:57 +03:00
|
|
|
|
2016-02-25 02:54:50 +03:00
|
|
|
RefPtr<Promise> promise = Promise::Create(global, aRv);
|
|
|
|
NS_ENSURE_TRUE(!aRv.Failed(), nullptr);
|
2014-07-09 23:24:49 +04:00
|
|
|
|
2016-10-12 01:26:57 +03:00
|
|
|
if (!IsPresenting()) {
|
|
|
|
// We can not exit a presentation outside of the context that
|
|
|
|
// started the presentation.
|
|
|
|
promise->MaybeRejectWithUndefined();
|
|
|
|
} else {
|
|
|
|
promise->MaybeResolve(JS::UndefinedHandleValue);
|
|
|
|
ExitPresentInternal();
|
|
|
|
}
|
|
|
|
|
2016-02-25 02:54:50 +03:00
|
|
|
return promise.forget();
|
|
|
|
}
|
2014-07-09 23:24:49 +04:00
|
|
|
|
2016-02-25 02:54:50 +03:00
|
|
|
void VRDisplay::ExitPresentInternal() { mPresentation = nullptr; }
|
2014-07-09 23:24:49 +04:00
|
|
|
|
2017-04-13 02:22:29 +03:00
|
|
|
void VRDisplay::Shutdown() {
|
|
|
|
mShutdown = true;
|
|
|
|
ExitPresentInternal();
|
|
|
|
nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
|
|
|
|
if (MOZ_LIKELY(obs)) {
|
|
|
|
obs->RemoveObserver(this, "inner-window-destroyed");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-25 02:54:50 +03:00
|
|
|
void VRDisplay::GetLayers(nsTArray<VRLayer>& result) {
|
|
|
|
if (mPresentation) {
|
|
|
|
mPresentation->GetDOMLayers(result);
|
|
|
|
} else {
|
|
|
|
result = nsTArray<VRLayer>();
|
|
|
|
}
|
2015-09-18 00:23:13 +03:00
|
|
|
}
|
2014-07-09 23:24:49 +04:00
|
|
|
|
2016-10-01 02:43:24 +03:00
|
|
|
void VRDisplay::SubmitFrame() {
|
2020-01-18 00:29:15 +03:00
|
|
|
AUTO_PROFILER_TRACING_MARKER("VR", "SubmitFrameAtVRDisplay", OTHER);
|
2017-09-06 11:08:21 +03:00
|
|
|
|
2017-11-01 03:25:40 +03:00
|
|
|
if (mClient && !mClient->IsPresentationGenerationCurrent()) {
|
|
|
|
mPresentation = nullptr;
|
|
|
|
mClient->MakePresentationGenerationCurrent();
|
|
|
|
}
|
|
|
|
|
2016-02-25 02:54:50 +03:00
|
|
|
if (mPresentation) {
|
2016-10-01 02:43:24 +03:00
|
|
|
mPresentation->SubmitFrame();
|
2016-02-25 02:54:50 +03:00
|
|
|
}
|
2016-10-07 02:32:16 +03:00
|
|
|
mFrameInfo.Clear();
|
2015-09-18 00:23:13 +03:00
|
|
|
}
|
2014-07-09 23:24:49 +04:00
|
|
|
|
2016-02-25 02:54:50 +03:00
|
|
|
int32_t VRDisplay::RequestAnimationFrame(FrameRequestCallback& aCallback,
|
|
|
|
ErrorResult& aError) {
|
2017-04-13 02:22:29 +03:00
|
|
|
if (mShutdown) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-02-25 02:54:50 +03:00
|
|
|
gfx::VRManagerChild* vm = gfx::VRManagerChild::Get();
|
2014-07-09 23:24:49 +04:00
|
|
|
|
2016-02-25 02:54:50 +03:00
|
|
|
int32_t handle;
|
|
|
|
aError = vm->ScheduleFrameRequestCallback(aCallback, &handle);
|
|
|
|
return handle;
|
2015-09-18 00:23:13 +03:00
|
|
|
}
|
2014-07-09 23:24:49 +04:00
|
|
|
|
2016-02-25 02:54:50 +03:00
|
|
|
void VRDisplay::CancelAnimationFrame(int32_t aHandle, ErrorResult& aError) {
|
|
|
|
gfx::VRManagerChild* vm = gfx::VRManagerChild::Get();
|
|
|
|
vm->CancelFrameRequestCallback(aHandle);
|
|
|
|
}
|
2014-07-09 23:24:49 +04:00
|
|
|
|
2016-02-25 02:54:50 +03:00
|
|
|
bool VRDisplay::IsPresenting() const {
|
2016-10-12 01:26:57 +03:00
|
|
|
// IsPresenting returns true only if this Javascript context is presenting
|
|
|
|
// and will return false if another context is presenting.
|
|
|
|
return mPresentation != nullptr;
|
2015-09-18 00:23:13 +03:00
|
|
|
}
|
|
|
|
|
2017-05-09 02:01:36 +03:00
|
|
|
bool VRDisplay::IsAnyPresenting(uint32_t aGroupMask) const {
|
|
|
|
// IsAnyPresenting returns true if either this VRDisplay object or any other
|
|
|
|
// from anther Javascript context is presenting with a group matching
|
|
|
|
// aGroupMask.
|
|
|
|
if (mPresentation && (mPresentation->GetGroup() & aGroupMask)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (mClient->GetDisplayInfo().GetPresentingGroups() & aGroupMask) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2016-10-19 01:18:10 +03:00
|
|
|
}
|
|
|
|
|
2016-02-25 02:54:50 +03:00
|
|
|
bool VRDisplay::IsConnected() const { return mClient->GetIsConnected(); }
|
2014-07-09 23:24:49 +04:00
|
|
|
|
2017-05-09 02:01:36 +03:00
|
|
|
uint32_t VRDisplay::PresentingGroups() const {
|
|
|
|
return mClient->GetDisplayInfo().GetPresentingGroups();
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t VRDisplay::GroupMask() const {
|
|
|
|
return mClient->GetDisplayInfo().GetGroupMask();
|
|
|
|
}
|
|
|
|
|
|
|
|
void VRDisplay::SetGroupMask(const uint32_t& aGroupMask) {
|
|
|
|
mClient->SetGroupMask(aGroupMask);
|
|
|
|
}
|
|
|
|
|
2016-07-22 22:41:00 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_INHERITED(VRDisplay, DOMEventTargetHelper,
|
|
|
|
mCapabilities, mStageParameters)
|
2016-02-25 02:54:50 +03:00
|
|
|
|
|
|
|
NS_IMPL_ADDREF_INHERITED(VRDisplay, DOMEventTargetHelper)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(VRDisplay, DOMEventTargetHelper)
|
|
|
|
|
2017-08-30 02:02:48 +03:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(VRDisplay)
|
2016-02-25 02:54:50 +03:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIObserver)
|
|
|
|
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, DOMEventTargetHelper)
|
|
|
|
NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper)
|
|
|
|
|
2016-10-01 02:43:33 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(VRFrameData)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(VRFrameData)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mParent, mPose)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER
|
|
|
|
tmp->mLeftProjectionMatrix = nullptr;
|
|
|
|
tmp->mLeftViewMatrix = nullptr;
|
|
|
|
tmp->mRightProjectionMatrix = nullptr;
|
|
|
|
tmp->mRightViewMatrix = nullptr;
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(VRFrameData)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mParent, mPose)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(VRFrameData)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_PRESERVED_WRAPPER
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mLeftProjectionMatrix)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mLeftViewMatrix)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mRightProjectionMatrix)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mRightViewMatrix)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_ROOT_NATIVE(VRFrameData, AddRef)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNROOT_NATIVE(VRFrameData, Release)
|
|
|
|
|
|
|
|
VRFrameData::VRFrameData(nsISupports* aParent)
|
|
|
|
: mParent(aParent),
|
|
|
|
mLeftProjectionMatrix(nullptr),
|
|
|
|
mLeftViewMatrix(nullptr),
|
|
|
|
mRightProjectionMatrix(nullptr),
|
|
|
|
mRightViewMatrix(nullptr) {
|
2016-11-17 17:37:25 +03:00
|
|
|
mozilla::HoldJSObjects(this);
|
2016-10-01 02:43:33 +03:00
|
|
|
mPose = new VRPose(aParent);
|
|
|
|
}
|
|
|
|
|
|
|
|
VRFrameData::~VRFrameData() { mozilla::DropJSObjects(this); }
|
|
|
|
|
2019-02-26 01:05:29 +03:00
|
|
|
/* static */
|
|
|
|
already_AddRefed<VRFrameData> VRFrameData::Constructor(
|
2019-09-12 14:01:17 +03:00
|
|
|
const GlobalObject& aGlobal) {
|
2016-10-01 02:43:33 +03:00
|
|
|
RefPtr<VRFrameData> obj = new VRFrameData(aGlobal.GetAsSupports());
|
|
|
|
return obj.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
JSObject* VRFrameData::WrapObject(JSContext* aCx,
|
|
|
|
JS::Handle<JSObject*> aGivenProto) {
|
2018-06-26 00:20:54 +03:00
|
|
|
return VRFrameData_Binding::Wrap(aCx, this, aGivenProto);
|
2016-10-01 02:43:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
VRPose* VRFrameData::Pose() { return mPose; }
|
|
|
|
|
|
|
|
double VRFrameData::Timestamp() const {
|
2016-10-07 02:32:16 +03:00
|
|
|
// Converting from seconds to milliseconds
|
|
|
|
return mFrameInfo.mVRState.timestamp * 1000.0f;
|
2016-10-01 02:43:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void VRFrameData::GetLeftProjectionMatrix(JSContext* aCx,
|
|
|
|
JS::MutableHandle<JSObject*> aRetval,
|
|
|
|
ErrorResult& aRv) {
|
2019-07-30 03:23:38 +03:00
|
|
|
Pose::SetFloat32Array(aCx, this, aRetval, mLeftProjectionMatrix,
|
2019-08-20 21:34:24 +03:00
|
|
|
mFrameInfo.mLeftProjection.components, 16, aRv);
|
2016-10-01 02:43:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void VRFrameData::GetLeftViewMatrix(JSContext* aCx,
|
|
|
|
JS::MutableHandle<JSObject*> aRetval,
|
|
|
|
ErrorResult& aRv) {
|
2019-07-30 03:23:38 +03:00
|
|
|
Pose::SetFloat32Array(aCx, this, aRetval, mLeftViewMatrix,
|
2019-08-20 21:34:24 +03:00
|
|
|
mFrameInfo.mLeftView.components, 16, aRv);
|
2016-10-01 02:43:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void VRFrameData::GetRightProjectionMatrix(JSContext* aCx,
|
|
|
|
JS::MutableHandle<JSObject*> aRetval,
|
|
|
|
ErrorResult& aRv) {
|
2019-07-30 03:23:38 +03:00
|
|
|
Pose::SetFloat32Array(aCx, this, aRetval, mRightProjectionMatrix,
|
2019-08-20 21:34:24 +03:00
|
|
|
mFrameInfo.mRightProjection.components, 16, aRv);
|
2016-10-01 02:43:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void VRFrameData::GetRightViewMatrix(JSContext* aCx,
|
|
|
|
JS::MutableHandle<JSObject*> aRetval,
|
|
|
|
ErrorResult& aRv) {
|
2019-07-30 03:23:38 +03:00
|
|
|
Pose::SetFloat32Array(aCx, this, aRetval, mRightViewMatrix,
|
2019-08-20 21:34:24 +03:00
|
|
|
mFrameInfo.mRightView.components, 16, aRv);
|
2016-10-01 02:43:33 +03:00
|
|
|
}
|
|
|
|
|
2016-10-07 02:32:16 +03:00
|
|
|
void VRFrameData::Update(const VRFrameInfo& aFrameInfo) {
|
|
|
|
mFrameInfo = aFrameInfo;
|
2019-07-30 03:23:38 +03:00
|
|
|
mPose->Update(mFrameInfo.mVRState);
|
2016-10-07 02:32:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void VRFrameInfo::Update(const gfx::VRDisplayInfo& aInfo,
|
|
|
|
const gfx::VRHMDSensorState& aState, float aDepthNear,
|
|
|
|
float aDepthFar) {
|
|
|
|
mVRState = aState;
|
2017-03-04 02:17:19 +03:00
|
|
|
if (mTimeStampOffset == 0.0f) {
|
|
|
|
/**
|
|
|
|
* A mTimeStampOffset value of 0.0f indicates that this is the first
|
|
|
|
* iteration and an offset has not yet been set.
|
|
|
|
*
|
|
|
|
* Generate a value for mTimeStampOffset such that if aState.timestamp is
|
|
|
|
* monotonically increasing, aState.timestamp + mTimeStampOffset will never
|
|
|
|
* be a negative number and will start at a pseudo-random offset
|
|
|
|
* between 1000.0f and 11000.0f seconds.
|
|
|
|
*
|
|
|
|
* We use a pseudo random offset rather than 0.0f just to discourage users
|
|
|
|
* from making the assumption that the timestamp returned in the WebVR API
|
|
|
|
* has a base of 0, which is not necessarily true in all UA's.
|
|
|
|
*/
|
|
|
|
mTimeStampOffset =
|
2019-08-31 00:55:18 +03:00
|
|
|
float(rand()) / float(RAND_MAX) * 10000.0f + 1000.0f - aState.timestamp;
|
2017-03-04 02:17:19 +03:00
|
|
|
}
|
|
|
|
mVRState.timestamp = aState.timestamp + mTimeStampOffset;
|
2016-10-01 02:43:33 +03:00
|
|
|
|
|
|
|
// Avoid division by zero within ConstructProjectionMatrix
|
|
|
|
const float kEpsilon = 0.00001f;
|
|
|
|
if (fabs(aDepthFar - aDepthNear) < kEpsilon) {
|
|
|
|
aDepthFar = aDepthNear + kEpsilon;
|
|
|
|
}
|
|
|
|
|
2018-03-14 03:09:54 +03:00
|
|
|
const gfx::VRFieldOfView leftFOV =
|
2019-01-10 00:57:36 +03:00
|
|
|
aInfo.mDisplayState.eyeFOV[gfx::VRDisplayState::Eye_Left];
|
2016-10-01 02:43:33 +03:00
|
|
|
mLeftProjection =
|
|
|
|
leftFOV.ConstructProjectionMatrix(aDepthNear, aDepthFar, true);
|
2018-03-14 03:09:54 +03:00
|
|
|
const gfx::VRFieldOfView rightFOV =
|
2019-01-10 00:57:36 +03:00
|
|
|
aInfo.mDisplayState.eyeFOV[gfx::VRDisplayState::Eye_Right];
|
2016-10-01 02:43:33 +03:00
|
|
|
mRightProjection =
|
|
|
|
rightFOV.ConstructProjectionMatrix(aDepthNear, aDepthFar, true);
|
2017-10-24 00:23:03 +03:00
|
|
|
memcpy(mLeftView.components, aState.leftViewMatrix,
|
|
|
|
sizeof(aState.leftViewMatrix));
|
|
|
|
memcpy(mRightView.components, aState.rightViewMatrix,
|
|
|
|
sizeof(aState.rightViewMatrix));
|
2016-10-07 02:32:16 +03:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-10-07 02:32:16 +03:00
|
|
|
VRFrameInfo::VRFrameInfo() : mTimeStampOffset(0.0f) {
|
2018-06-15 18:16:15 +03:00
|
|
|
mVRState.inputFrameID = 0;
|
|
|
|
mVRState.timestamp = 0.0;
|
|
|
|
mVRState.flags = gfx::VRDisplayCapabilityFlags::Cap_None;
|
2016-10-07 02:32:16 +03:00
|
|
|
}
|
2016-10-01 02:43:33 +03:00
|
|
|
|
2016-10-07 02:32:16 +03:00
|
|
|
bool VRFrameInfo::IsDirty() { return mVRState.timestamp == 0; }
|
|
|
|
|
|
|
|
void VRFrameInfo::Clear() { mVRState.Clear(); }
|
2016-10-01 02:43:33 +03:00
|
|
|
|
2014-07-09 23:24:49 +04:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|