mirror of
https://github.com/servo/servo.git
synced 2025-08-02 20:20:14 +01:00
Fill in XR.requestSession
This commit is contained in:
parent
520bb23048
commit
d5911816e1
5 changed files with 104 additions and 25 deletions
|
@ -4,6 +4,7 @@
|
||||||
|
|
||||||
use crate::dom::bindings::codegen::Bindings::NavigatorBinding;
|
use crate::dom::bindings::codegen::Bindings::NavigatorBinding;
|
||||||
use crate::dom::bindings::codegen::Bindings::NavigatorBinding::NavigatorMethods;
|
use crate::dom::bindings::codegen::Bindings::NavigatorBinding::NavigatorMethods;
|
||||||
|
use crate::dom::bindings::error::Error;
|
||||||
use crate::dom::bindings::reflector::{reflect_dom_object, DomObject, Reflector};
|
use crate::dom::bindings::reflector::{reflect_dom_object, DomObject, Reflector};
|
||||||
use crate::dom::bindings::root::{DomRoot, MutNullableDom};
|
use crate::dom::bindings::root::{DomRoot, MutNullableDom};
|
||||||
use crate::dom::bindings::str::DOMString;
|
use crate::dom::bindings::str::DOMString;
|
||||||
|
@ -148,7 +149,13 @@ impl NavigatorMethods for Navigator {
|
||||||
// https://w3c.github.io/webvr/spec/1.1/#navigator-getvrdisplays-attribute
|
// https://w3c.github.io/webvr/spec/1.1/#navigator-getvrdisplays-attribute
|
||||||
#[allow(unrooted_must_root)]
|
#[allow(unrooted_must_root)]
|
||||||
fn GetVRDisplays(&self) -> Rc<Promise> {
|
fn GetVRDisplays(&self) -> Rc<Promise> {
|
||||||
self.Xr().get_displays()
|
let promise = Promise::new(&self.global());
|
||||||
|
let displays = self.Xr().get_displays();
|
||||||
|
match displays {
|
||||||
|
Ok(displays) => promise.resolve_native(&displays),
|
||||||
|
Err(e) => promise.reject_error(Error::Security),
|
||||||
|
}
|
||||||
|
promise
|
||||||
}
|
}
|
||||||
|
|
||||||
fn Xr(&self) -> DomRoot<XR> {
|
fn Xr(&self) -> DomRoot<XR> {
|
||||||
|
|
|
@ -6,8 +6,8 @@
|
||||||
[SecureContext, Exposed=Window]
|
[SecureContext, Exposed=Window]
|
||||||
interface XR: EventTarget {
|
interface XR: EventTarget {
|
||||||
// Methods
|
// Methods
|
||||||
// Promise<void> supportsSessionMode(XRSessionMode mode);
|
Promise<void> supportsSessionMode(XRSessionMode mode);
|
||||||
// Promise<XRSession> requestSession(optional XRSessionCreationOptions parameters);
|
Promise<XRSession> requestSession(optional XRSessionCreationOptions parameters);
|
||||||
|
|
||||||
// Events
|
// Events
|
||||||
// attribute EventHandler ondevicechange;
|
// attribute EventHandler ondevicechange;
|
||||||
|
|
|
@ -12,12 +12,12 @@ enum XREnvironmentBlendMode {
|
||||||
|
|
||||||
[SecureContext, Exposed=Window] interface XRSession : EventTarget {
|
[SecureContext, Exposed=Window] interface XRSession : EventTarget {
|
||||||
// // Attributes
|
// // Attributes
|
||||||
// readonly attribute XRSessionMode mode;
|
readonly attribute XRSessionMode mode;
|
||||||
// readonly attribute XRPresentationContext outputContext;
|
// readonly attribute XRPresentationContext outputContext;
|
||||||
// readonly attribute XREnvironmentBlendMode environmentBlendMode;
|
// readonly attribute XREnvironmentBlendMode environmentBlendMode;
|
||||||
|
|
||||||
// attribute double depthNear;
|
attribute double depthNear;
|
||||||
// attribute double depthFar;
|
attribute double depthFar;
|
||||||
// attribute XRLayer baseLayer;
|
// attribute XRLayer baseLayer;
|
||||||
|
|
||||||
// // Methods
|
// // Methods
|
||||||
|
|
|
@ -5,6 +5,8 @@
|
||||||
use crate::dom::bindings::cell::DomRefCell;
|
use crate::dom::bindings::cell::DomRefCell;
|
||||||
use crate::dom::bindings::codegen::Bindings::VRDisplayBinding::VRDisplayMethods;
|
use crate::dom::bindings::codegen::Bindings::VRDisplayBinding::VRDisplayMethods;
|
||||||
use crate::dom::bindings::codegen::Bindings::XRBinding;
|
use crate::dom::bindings::codegen::Bindings::XRBinding;
|
||||||
|
use crate::dom::bindings::codegen::Bindings::XRBinding::XRSessionCreationOptions;
|
||||||
|
use crate::dom::bindings::codegen::Bindings::XRBinding::{XRMethods, XRSessionMode};
|
||||||
use crate::dom::bindings::error::Error;
|
use crate::dom::bindings::error::Error;
|
||||||
use crate::dom::bindings::inheritance::Castable;
|
use crate::dom::bindings::inheritance::Castable;
|
||||||
use crate::dom::bindings::reflector::{reflect_dom_object, DomObject};
|
use crate::dom::bindings::reflector::{reflect_dom_object, DomObject};
|
||||||
|
@ -17,6 +19,7 @@ use crate::dom::globalscope::GlobalScope;
|
||||||
use crate::dom::promise::Promise;
|
use crate::dom::promise::Promise;
|
||||||
use crate::dom::vrdisplay::VRDisplay;
|
use crate::dom::vrdisplay::VRDisplay;
|
||||||
use crate::dom::vrdisplayevent::VRDisplayEvent;
|
use crate::dom::vrdisplayevent::VRDisplayEvent;
|
||||||
|
use crate::dom::xrsession::XRSession;
|
||||||
use dom_struct::dom_struct;
|
use dom_struct::dom_struct;
|
||||||
use ipc_channel::ipc::IpcSender;
|
use ipc_channel::ipc::IpcSender;
|
||||||
use profile_traits::ipc;
|
use profile_traits::ipc;
|
||||||
|
@ -53,15 +56,59 @@ impl Drop for XR {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl XRMethods for XR {
|
||||||
|
#[allow(unrooted_must_root)]
|
||||||
|
fn SupportsSessionMode(&self, mode: XRSessionMode) -> Rc<Promise> {
|
||||||
|
// XXXManishearth this should select an XR device first
|
||||||
|
let promise = Promise::new(&self.global());
|
||||||
|
if mode == XRSessionMode::Immersive_vr {
|
||||||
|
promise.resolve_native(&());
|
||||||
|
} else {
|
||||||
|
// XXXManishearth support other modes
|
||||||
|
promise.reject_error(Error::NotSupported);
|
||||||
|
}
|
||||||
|
|
||||||
|
promise
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(unrooted_must_root)]
|
||||||
|
fn RequestSession(&self, options: &XRSessionCreationOptions) -> Rc<Promise> {
|
||||||
|
let promise = Promise::new(&self.global());
|
||||||
|
if options.mode != XRSessionMode::Immersive_vr {
|
||||||
|
promise.reject_error(Error::NotSupported);
|
||||||
|
return promise;
|
||||||
|
}
|
||||||
|
|
||||||
|
let displays = self.get_displays();
|
||||||
|
|
||||||
|
let displays = match displays {
|
||||||
|
Ok(d) => d,
|
||||||
|
Err(_) => {
|
||||||
|
promise.reject_native(&());
|
||||||
|
return promise;
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
if displays.is_empty() {
|
||||||
|
promise.reject_error(Error::Security);
|
||||||
|
}
|
||||||
|
|
||||||
|
let session = XRSession::new(&self.global(), &displays[0]);
|
||||||
|
promise.resolve_native(&session);
|
||||||
|
|
||||||
|
promise
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl XR {
|
impl XR {
|
||||||
#[allow(unrooted_must_root)]
|
#[allow(unrooted_must_root)]
|
||||||
pub fn get_displays(&self) -> Rc<Promise> {
|
pub fn get_displays(&self) -> Result<Vec<DomRoot<VRDisplay>>, ()> {
|
||||||
let promise = Promise::new(&self.global());
|
|
||||||
|
|
||||||
if let Some(webvr_thread) = self.webvr_thread() {
|
if let Some(webvr_thread) = self.webvr_thread() {
|
||||||
let (sender, receiver) =
|
let (sender, receiver) =
|
||||||
ipc::channel(self.global().time_profiler_chan().clone()).unwrap();
|
ipc::channel(self.global().time_profiler_chan().clone()).unwrap();
|
||||||
webvr_thread.send(WebVRMsg::GetDisplays(sender)).unwrap();
|
webvr_thread.send(WebVRMsg::GetDisplays(sender)).unwrap();
|
||||||
|
|
||||||
|
// FIXME(#22505) we should not block here and instead produce a promise
|
||||||
match receiver.recv().unwrap() {
|
match receiver.recv().unwrap() {
|
||||||
Ok(displays) => {
|
Ok(displays) => {
|
||||||
// Sync displays
|
// Sync displays
|
||||||
|
@ -69,27 +116,19 @@ impl XR {
|
||||||
self.sync_display(&display);
|
self.sync_display(&display);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
Err(e) => {
|
Err(e) => return Err(()),
|
||||||
promise.reject_native(&e);
|
|
||||||
return promise;
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// WebVR spec: The Promise MUST be rejected if WebVR is not enabled/supported.
|
// WebVR spec: The Promise MUST be rejected if WebVR is not enabled/supported.
|
||||||
promise.reject_error(Error::Security);
|
return Err(());
|
||||||
return promise;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// convert from Dom to DomRoot
|
// convert from Dom to DomRoot
|
||||||
let displays: Vec<DomRoot<VRDisplay>> = self
|
Ok(self.displays
|
||||||
.displays
|
|
||||||
.borrow()
|
.borrow()
|
||||||
.iter()
|
.iter()
|
||||||
.map(|d| DomRoot::from_ref(&**d))
|
.map(|d| DomRoot::from_ref(&**d))
|
||||||
.collect();
|
.collect())
|
||||||
promise.resolve_native(&displays);
|
|
||||||
|
|
||||||
promise
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn webvr_thread(&self) -> Option<IpcSender<WebVRMsg>> {
|
fn webvr_thread(&self) -> Option<IpcSender<WebVRMsg>> {
|
||||||
|
|
|
@ -2,30 +2,63 @@
|
||||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
|
||||||
|
|
||||||
|
use crate::dom::bindings::codegen::Bindings::XRBinding::XRSessionMode;
|
||||||
use crate::dom::bindings::codegen::Bindings::XRSessionBinding;
|
use crate::dom::bindings::codegen::Bindings::XRSessionBinding;
|
||||||
|
use crate::dom::bindings::codegen::Bindings::XRSessionBinding::XRSessionMethods;
|
||||||
|
use crate::dom::bindings::num::Finite;
|
||||||
use crate::dom::bindings::reflector::reflect_dom_object;
|
use crate::dom::bindings::reflector::reflect_dom_object;
|
||||||
use crate::dom::bindings::root::DomRoot;
|
use crate::dom::bindings::root::{Dom, DomRoot};
|
||||||
use crate::dom::eventtarget::EventTarget;
|
use crate::dom::eventtarget::EventTarget;
|
||||||
use crate::dom::globalscope::GlobalScope;
|
use crate::dom::globalscope::GlobalScope;
|
||||||
|
use crate::dom::vrdisplay::VRDisplay;
|
||||||
use dom_struct::dom_struct;
|
use dom_struct::dom_struct;
|
||||||
|
use std::cell::Cell;
|
||||||
|
|
||||||
#[dom_struct]
|
#[dom_struct]
|
||||||
pub struct XRSession {
|
pub struct XRSession {
|
||||||
eventtarget: EventTarget,
|
eventtarget: EventTarget,
|
||||||
|
display: Dom<VRDisplay>,
|
||||||
|
depth_near: Cell<f64>,
|
||||||
|
depth_far: Cell<f64>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl XRSession {
|
impl XRSession {
|
||||||
fn new_inherited() -> XRSession {
|
fn new_inherited(display: &VRDisplay) -> XRSession {
|
||||||
XRSession {
|
XRSession {
|
||||||
eventtarget: EventTarget::new_inherited(),
|
eventtarget: EventTarget::new_inherited(),
|
||||||
|
display: Dom::from_ref(display),
|
||||||
|
depth_near: Cell::new(0.1),
|
||||||
|
depth_far: Cell::new(1000.),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new(global: &GlobalScope) -> DomRoot<XRSession> {
|
pub fn new(global: &GlobalScope, display: &VRDisplay) -> DomRoot<XRSession> {
|
||||||
reflect_dom_object(
|
reflect_dom_object(
|
||||||
Box::new(XRSession::new_inherited()),
|
Box::new(XRSession::new_inherited(display)),
|
||||||
global,
|
global,
|
||||||
XRSessionBinding::Wrap,
|
XRSessionBinding::Wrap,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl XRSessionMethods for XRSession {
|
||||||
|
fn DepthNear(&self) -> Finite<f64> {
|
||||||
|
Finite::wrap(self.depth_near.get())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn DepthFar(&self) -> Finite<f64> {
|
||||||
|
Finite::wrap(self.depth_far.get())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn SetDepthNear(&self, d: Finite<f64>) {
|
||||||
|
self.depth_near.set(*d)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn SetDepthFar(&self, d: Finite<f64>) {
|
||||||
|
self.depth_far.set(*d)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn Mode(&self) -> XRSessionMode {
|
||||||
|
XRSessionMode::Immersive_vr
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue