Skip to content

Commit

Permalink
Auto merge of #53 - paulrouget:shutdown, r=Manishearth
Browse files Browse the repository at this point in the history
Asynchonous shutdown

OpenXR shuts down asynchronously.

Ideally webxr would intercept the `SessionEnd` event. But for now, I think this will do.
  • Loading branch information
bors-servo committed Sep 17, 2019
2 parents 892530c + 65fb047 commit 614c1b7
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 6 deletions.
4 changes: 4 additions & 0 deletions webxr-api/device.rs
Expand Up @@ -48,6 +48,10 @@ pub trait Device: 'static {
Size2D::new(viewport.max_x(), viewport.max_y())
}

/// This method checks if the session has exited since last
/// wait_for_animation_frame call.
fn is_running(&self) -> bool;

/// This method should block waiting for the next frame,
/// and return the information for it.
fn wait_for_animation_frame(&mut self) -> Frame;
Expand Down
9 changes: 6 additions & 3 deletions webxr-api/session.rs
Expand Up @@ -205,8 +205,12 @@ impl<D: Device> SessionThread<D> {
}
SessionMsg::RequestAnimationFrame(dest) => {
let timestamp = self.timestamp;
let frame = self.device.wait_for_animation_frame();
let _ = dest.send((timestamp, frame));
if self.device.is_running() {
let frame = self.device.wait_for_animation_frame();
let _ = dest.send((timestamp, frame));
} else {
return false;
}
}
SessionMsg::UpdateClipPlanes(near, far) => self.device.update_clip_planes(near, far),
SessionMsg::RenderAnimationFrame => {
Expand All @@ -219,7 +223,6 @@ impl<D: Device> SessionThread<D> {
}
SessionMsg::Quit => {
self.device.quit();
return false;
}
}
true
Expand Down
7 changes: 7 additions & 0 deletions webxr/glwindow/mod.rs
Expand Up @@ -89,6 +89,7 @@ pub struct GlWindowDevice {
read_fbo: GLuint,
events: EventBuffer,
clip_planes: ClipPlanes,
is_running: bool,
}

impl Device for GlWindowDevice {
Expand Down Expand Up @@ -177,7 +178,12 @@ impl Device for GlWindowDevice {
self.events.upgrade(dest)
}

fn is_running(&self) -> bool {
self.is_running
}

fn quit(&mut self) {
self.is_running = false;
self.events.callback(Event::SessionEnd);
}

Expand All @@ -204,6 +210,7 @@ impl GlWindowDevice {
read_fbo,
events: Default::default(),
clip_planes: Default::default(),
is_running: true,
})
}

Expand Down
8 changes: 8 additions & 0 deletions webxr/googlevr/device.rs
Expand Up @@ -63,6 +63,7 @@ pub(crate) struct GoogleVRDevice {
depth: bool,
clip_planes: ClipPlanes,
input: Option<GoogleVRController>,
is_running: bool,

#[cfg(target_os = "android")]
java_class: ndk::jclass,
Expand Down Expand Up @@ -98,6 +99,7 @@ impl GoogleVRDevice {
depth: false,
clip_planes: Default::default(),
input: None,
is_running: true,

ctx: ctx.get(),
controller_ctx: controller_ctx.get(),
Expand Down Expand Up @@ -139,6 +141,7 @@ impl GoogleVRDevice {
depth: false,
clip_planes: Default::default(),
input: None,
is_running: true,

ctx: ctx.get(),
controller_ctx: controller_ctx.get(),
Expand Down Expand Up @@ -579,9 +582,14 @@ impl Device for GoogleVRDevice {
self.events.upgrade(dest);
}

fn is_running(&self) -> bool {
self.is_running
}

fn quit(&mut self) {
self.stop_present();
self.events.callback(Event::SessionEnd);
self.is_running = false;
}

fn set_quitter(&mut self, _: Quitter) {
Expand Down
14 changes: 13 additions & 1 deletion webxr/headless/mod.rs
Expand Up @@ -58,6 +58,7 @@ struct InputInfo {
struct HeadlessDevice {
gl: Rc<dyn Gl>,
data: Arc<Mutex<HeadlessDeviceData>>,
is_running: bool,
}

struct HeadlessDeviceData {
Expand Down Expand Up @@ -119,7 +120,13 @@ impl Discovery for HeadlessDiscovery {
}
let gl = self.gl.clone();
let data = self.data.clone();
xr.run_on_main_thread(move || Ok(HeadlessDevice { gl, data }))
xr.run_on_main_thread(move || {
Ok(HeadlessDevice {
gl,
data,
is_running: true,
})
})
}

fn supports_session(&self, mode: SessionMode) -> bool {
Expand Down Expand Up @@ -177,7 +184,12 @@ impl Device for HeadlessDevice {
self.data.lock().unwrap().events.upgrade(dest)
}

fn is_running(&self) -> bool {
self.is_running
}

fn quit(&mut self) {
self.is_running = false;
self.data.lock().unwrap().events.callback(Event::SessionEnd);
}

Expand Down
6 changes: 6 additions & 0 deletions webxr/magicleap/mod.rs
Expand Up @@ -98,6 +98,7 @@ pub struct MagicLeapDevice {
frame_handle: MLHandle,
cameras: MLGraphicsVirtualCameraInfoArray,
view_update_needed: bool,
is_running: bool,
}

impl MagicLeapDiscovery {
Expand Down Expand Up @@ -442,7 +443,12 @@ impl Device for MagicLeapDevice {
// TODO: handle events
}

fn is_running(&self) -> bool {
self.is_running
}

fn quit(&mut self) {
self.is_running = false;
// TODO: handle quit
}

Expand Down
36 changes: 34 additions & 2 deletions webxr/openxr/mod.rs
Expand Up @@ -98,6 +98,7 @@ impl Discovery for OpenXrDiscovery {
}

struct OpenXrDevice {
instance: Instance,
#[allow(unused)]
gl: Rc<dyn Gl>,
#[allow(unused)]
Expand All @@ -121,6 +122,7 @@ struct OpenXrDevice {
resource: ComPtr<dxgi::IDXGIResource>,
device_context: ComPtr<d3d11::ID3D11DeviceContext>,
device: ComPtr<d3d11::ID3D11Device>,
is_running: bool,
}

impl OpenXrDevice {
Expand Down Expand Up @@ -150,6 +152,8 @@ impl OpenXrDevice {
.map_err(|e| Error::BackendSpecific(format!("{:?}", e)))?
};

// XXXPaul initialisation should happen on SessionStateChanged(Ready)?

session
.begin(ViewConfigurationType::PRIMARY_STEREO)
.map_err(|e| Error::BackendSpecific(format!("{:?}", e)))?;
Expand Down Expand Up @@ -235,6 +239,7 @@ impl OpenXrDevice {
);

Ok(OpenXrDevice {
instance,
events: Default::default(),
gl,
read_fbo,
Expand All @@ -256,8 +261,31 @@ impl OpenXrDevice {
resource,
device_context,
device,
is_running: true,
})
}

fn handle_openxr_events(&mut self) {
let mut buffer = openxr::EventDataBuffer::new();
while let Some(event) = self.instance.poll_event(&mut buffer).unwrap() {
use openxr::Event::*;
match event {
SessionStateChanged(session_change) => match session_change.state() {
openxr::SessionState::STOPPING => {
self.events.callback(Event::SessionEnd);
self.session.end().unwrap();
self.is_running = false;
}
_ => {
// FIXME: Handle other states
}
},
_ => {
// FIXME: Handle other events
}
}
}
}
}

impl Device for OpenXrDevice {
Expand Down Expand Up @@ -303,7 +331,12 @@ impl Device for OpenXrDevice {
Views::Stereo(left_view, right_view)
}

fn is_running(&self) -> bool {
self.is_running
}

fn wait_for_animation_frame(&mut self) -> Frame {
self.handle_openxr_events();
self.frame_state = self.frame_waiter.wait().expect("error waiting for frame");
// XXXManishearth should we check frame_state.should_render?
let (_view_flags, views) = self
Expand Down Expand Up @@ -538,8 +571,7 @@ impl Device for OpenXrDevice {
}

fn quit(&mut self) {
self.events.callback(Event::SessionEnd);
self.session.request_exit();
self.session.request_exit().unwrap();
}

fn set_quitter(&mut self, _: Quitter) {
Expand Down

0 comments on commit 614c1b7

Please sign in to comment.