Skip to content

Commit

Permalink
Auto merge of #31 - asajeffrey:sessions-track-draw-texture, r=Manishe…
Browse files Browse the repository at this point in the history
…arth

XR Sessions track which texture to draw to

Have sessions track which texture WebGL is drawing into. This is the webxr part of fixing a bug where XR was using the default canvas framebuffer rather than the XR framebuffer.
  • Loading branch information
bors-servo authored Jul 26, 2019
2 parents cf974f5 + 3b175b7 commit 35559ae
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 16 deletions.
1 change: 1 addition & 0 deletions webxr-api/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ pub use view::Views;

pub use webgl::WebGLContextId;
pub use webgl::WebGLExternalImageApi;
pub use webgl::WebGLTextureId;

#[cfg(feature = "ipc")]
use std::thread;
Expand Down
33 changes: 21 additions & 12 deletions webxr-api/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,14 @@ use crate::Viewport;
use crate::Views;
use crate::WebGLContextId;
use crate::WebGLExternalImageApi;
use crate::WebGLTextureId;

use euclid::default::Size2D as UntypedSize2D;
use euclid::RigidTransform3D;
use euclid::Size2D;

use gleam::gl::GLsizei;

use std::thread;
use std::time::Duration;

Expand All @@ -43,7 +47,7 @@ pub type HighResTimeStamp = f64;
// The messages that are sent from the content thread to the session thread.
#[cfg_attr(feature = "ipc", derive(Serialize, Deserialize))]
enum SessionMsg {
SetWebGLContext(Option<WebGLContextId>),
SetTexture(WebGLContextId, WebGLTextureId, UntypedSize2D<GLsizei>),
SetEventDest(Sender<Event>),
RequestAnimationFrame(Sender<(HighResTimeStamp, Frame)>),
RenderAnimationFrame,
Expand Down Expand Up @@ -91,8 +95,13 @@ impl Session {
self.resolution
}

pub fn set_webgl_context(&mut self, id: Option<WebGLContextId>) {
let _ = self.sender.send(SessionMsg::SetWebGLContext(id));
pub fn set_texture(
&mut self,
ctxt: WebGLContextId,
txt: WebGLTextureId,
size: UntypedSize2D<GLsizei>,
) {
let _ = self.sender.send(SessionMsg::SetTexture(ctxt, txt, size));
}

pub fn request_animation_frame(&mut self, dest: Sender<(HighResTimeStamp, Frame)>) {
Expand All @@ -117,7 +126,7 @@ pub struct SessionThread<D> {
receiver: Receiver<SessionMsg>,
sender: Sender<SessionMsg>,
webgl: Box<dyn WebGLExternalImageApi>,
webgl_context: Option<WebGLContextId>,
texture: Option<(WebGLContextId, WebGLTextureId, UntypedSize2D<GLsizei>)>,
timestamp: HighResTimeStamp,
running: bool,
device: D,
Expand All @@ -133,14 +142,14 @@ impl<D: Device> SessionThread<D> {
sender: sender.clone(),
});
let timestamp = 0.0;
let webgl_context = None;
let texture = None;
let running = true;
Ok(SessionThread {
sender,
receiver,
device,
webgl,
webgl_context,
texture,
timestamp,
running,
})
Expand Down Expand Up @@ -176,8 +185,8 @@ impl<D: Device> SessionThread<D> {

fn handle_msg(&mut self, msg: SessionMsg) -> bool {
match msg {
SessionMsg::SetWebGLContext(id) => {
self.webgl_context = id;
SessionMsg::SetTexture(ctxt, txt, size) => {
self.texture = Some((ctxt, txt, size));
}
SessionMsg::SetEventDest(dest) => {
self.device.set_event_dest(dest);
Expand All @@ -189,10 +198,10 @@ impl<D: Device> SessionThread<D> {
}
SessionMsg::RenderAnimationFrame => {
self.timestamp += 1.0;
if let Some(id) = self.webgl_context {
let (texture_id, size, sync) = self.webgl.lock(id);
self.device.render_animation_frame(texture_id, size, sync);
self.webgl.unlock(id);
if let Some((ctxt, txt, size)) = self.texture {
let sync = self.webgl.lock(ctxt);
self.device.render_animation_frame(txt, size, sync);
self.webgl.unlock(ctxt);
}
}
SessionMsg::Quit => {
Expand Down
7 changes: 3 additions & 4 deletions webxr-api/webgl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

//! The WebGL functionality needed by WebXR.

use euclid::default::Size2D;
use gleam::gl::GLsync;
use gleam::gl::GLuint;

pub type WebGLContextId = usize;
pub type WebGLTextureId = GLuint;

/// A trait to get access a GL texture from a WebGL context.
// Note that this is not serializable, we run it in the same
Expand All @@ -17,9 +17,8 @@ pub type WebGLContextId = usize;
// though, which is the main difference between this trait and
// the matching webrender trait.
pub trait WebGLExternalImageApi: Send {
/// Lock the WebGL context, and get back a texture id, the size of the texture,
/// and a sync object for the texture.
fn lock(&self, id: WebGLContextId) -> (GLuint, Size2D<i32>, GLsync);
/// Lock the WebGL context, and get back a sync object for its current state.
fn lock(&self, id: WebGLContextId) -> GLsync;

/// Unlock the WebGL context.
fn unlock(&self, id: WebGLContextId);
Expand Down

0 comments on commit 35559ae

Please sign in to comment.