Skip to content
Permalink
Browse files

Do not wait for rAF to be requested in render loop

  • Loading branch information
Manishearth committed Dec 19, 2019
1 parent 267ce46 commit 01f27e9c6916bcc1ea3ef7bc68eeac77d2f23113
Showing with 24 additions and 31 deletions.
  1. +2 −2 Cargo.lock
  2. +11 −5 components/script/dom/xr.rs
  3. +11 −24 components/script/dom/xrsession.rs

Some generated files are not rendered by default. Learn more.

@@ -26,14 +26,14 @@ use crate::dom::xrtest::XRTest;
use crate::script_thread::ScriptThread;
use crate::task_source::TaskSource;
use dom_struct::dom_struct;
use ipc_channel::ipc::IpcSender;
use ipc_channel::ipc::{self as ipc_crate, IpcReceiver, IpcSender};
use ipc_channel::router::ROUTER;
use profile_traits::ipc;
use std::cell::Cell;
use std::rc::Rc;
use webvr_traits::{WebVRDisplayData, WebVRDisplayEvent, WebVREvent, WebVRMsg};
use webvr_traits::{WebVRGamepadData, WebVRGamepadEvent, WebVRGamepadState};
use webxr_api::{Error as XRError, Session, SessionMode};
use webxr_api::{Error as XRError, Frame, Session, SessionMode};

#[dom_struct]
pub struct XR {
@@ -185,12 +185,15 @@ impl XRMethods for XR {
.task_manager()
.dom_manipulation_task_source_with_canceller();
let (sender, receiver) = ipc::channel(global.time_profiler_chan().clone()).unwrap();
let (frame_sender, frame_receiver) = ipc_crate::channel().unwrap();
let mut frame_receiver = Some(frame_receiver);
ROUTER.add_route(
receiver.to_opaque(),
Box::new(move |message| {
// router doesn't know this is only called once
let trusted = trusted.take().unwrap();
let this = this.clone();
let frame_receiver = frame_receiver.take().unwrap();
let message: Result<Session, webxr_api::Error> = if let Ok(message) = message.to() {
message
} else {
@@ -199,13 +202,15 @@ impl XRMethods for XR {
};
let _ = task_source.queue_with_canceller(
task!(request_session: move || {
this.root().session_obtained(message, trusted.root(), mode);
this.root().session_obtained(message, trusted.root(), mode, frame_receiver);
}),
&canceller,
);
}),
);
window.webxr_registry().request_session(mode.into(), sender);
window
.webxr_registry()
.request_session(mode.into(), sender, frame_sender);

promise
}
@@ -222,6 +227,7 @@ impl XR {
response: Result<Session, XRError>,
promise: Rc<Promise>,
mode: XRSessionMode,
frame_receiver: IpcReceiver<Frame>,
) {
let session = match response {
Ok(session) => session,
@@ -231,7 +237,7 @@ impl XR {
},
};

let session = XRSession::new(&self.global(), session, mode);
let session = XRSession::new(&self.global(), session, mode, frame_receiver);
if mode == XRSessionMode::Inline {
self.active_inline_sessions
.borrow_mut()
@@ -41,7 +41,7 @@ use crate::dom::xrwebgllayer::XRWebGLLayer;
use crate::task_source::TaskSource;
use dom_struct::dom_struct;
use euclid::{Rect, RigidTransform3D, Transform3D};
use ipc_channel::ipc::IpcSender;
use ipc_channel::ipc::IpcReceiver;
use ipc_channel::router::ROUTER;
use metrics::ToMs;
use profile_traits::ipc;
@@ -73,8 +73,6 @@ pub struct XRSession {
next_raf_id: Cell<i32>,
#[ignore_malloc_size_of = "closures are hard"]
raf_callback_list: DomRefCell<Vec<(i32, Option<Rc<XRFrameRequestCallback>>)>>,
#[ignore_malloc_size_of = "defined in ipc-channel"]
raf_sender: DomRefCell<Option<IpcSender<Frame>>>,
input_sources: Dom<XRInputSourceArray>,
// Any promises from calling end()
#[ignore_malloc_size_of = "promises are hard"]
@@ -108,15 +106,19 @@ impl XRSession {

next_raf_id: Cell::new(0),
raf_callback_list: DomRefCell::new(vec![]),
raf_sender: DomRefCell::new(None),
input_sources: Dom::from_ref(input_sources),
end_promises: DomRefCell::new(vec![]),
ended: Cell::new(false),
outside_raf: Cell::new(true),
}
}

pub fn new(global: &GlobalScope, session: Session, mode: XRSessionMode) -> DomRoot<XRSession> {
pub fn new(
global: &GlobalScope,
session: Session,
mode: XRSessionMode,
frame_receiver: IpcReceiver<Frame>,
) -> DomRoot<XRSession> {
let ivfov = if mode == XRSessionMode::Inline {
Some(FRAC_PI_2)
} else {
@@ -136,7 +138,7 @@ impl XRSession {
);
input_sources.set_initial_inputs(&ret);
ret.attach_event_handler();
ret.setup_raf_loop();
ret.setup_raf_loop(frame_receiver);
ret
}

@@ -153,21 +155,15 @@ impl XRSession {
self.mode != XRSessionMode::Inline
}

fn setup_raf_loop(&self) {
assert!(
self.raf_sender.borrow().is_none(),
"RAF loop already set up"
);
fn setup_raf_loop(&self, frame_receiver: IpcReceiver<Frame>) {
let this = Trusted::new(self);
let global = self.global();
let window = global.as_window();
let (task_source, canceller) = window
.task_manager()
.dom_manipulation_task_source_with_canceller();
let (sender, receiver) = ipc::channel(global.time_profiler_chan().clone()).unwrap();
*self.raf_sender.borrow_mut() = Some(sender);
ROUTER.add_route(
receiver.to_opaque(),
frame_receiver.to_opaque(),
Box::new(move |message| {
let this = this.clone();
let _ = task_source.queue_with_canceller(
@@ -179,15 +175,7 @@ impl XRSession {
}),
);

self.request_new_xr_frame();
}

/// Requests a new https://immersive-web.github.io/webxr/#xr-animation-frame
///
/// This happens regardless of the presense of rAF callbacks
fn request_new_xr_frame(&self) {
let sender = self.raf_sender.borrow().clone().unwrap();
self.session.borrow_mut().request_animation_frame(sender);
self.session.borrow_mut().start_render_loop();
}

pub fn is_outside_raf(&self) -> bool {
@@ -361,7 +349,6 @@ impl XRSession {
base_layer.swap_buffers();
self.session.borrow_mut().render_animation_frame();
}
self.request_new_xr_frame();

// If the canvas element is attached to the DOM, it is now dirty,
// and we need to trigger a reflow.

0 comments on commit 01f27e9

Please sign in to comment.
You can’t perform that action at this time.