Skip to content

Commit

Permalink
Auto merge of #25810 - Manishearth:xr-profile, r=jdm
Browse files Browse the repository at this point in the history
Add profiling to WebXR

Fixes servo/webxr#128

Depends on servo/webxr#131

r? @jdm
  • Loading branch information
bors-servo committed Feb 23, 2020
2 parents 504eeb8 + f3e1aba commit 6bc4a7d
Show file tree
Hide file tree
Showing 12 changed files with 95 additions and 11 deletions.
7 changes: 5 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions components/canvas/Cargo.toml
Expand Up @@ -15,6 +15,8 @@ canvas2d-azure = ["azure"]
canvas2d-raqote = ["raqote"]
webgl_backtrace = ["canvas_traits/webgl_backtrace"]
no-wgl = ["surfman/sm-no-wgl"]
xr-profile = ["webxr-api/profile", "time"]


[dependencies]
azure = {git = "https://github.com/servo/rust-azure", optional = true}
Expand All @@ -32,6 +34,7 @@ ipc-channel = "0.14"
log = "0.4"
num-traits = "0.2"
raqote = {git = "https://github.com/jrmuizel/raqote", optional = true}
time = { version = "0.1.0", optional = true }
pixels = {path = "../pixels"}
servo_config = {path = "../config"}
sparkle = "0.1.16"
Expand Down
31 changes: 27 additions & 4 deletions components/canvas/webgl_thread.rs
Expand Up @@ -75,6 +75,11 @@ use surfman_chains_api::SwapChainsAPI;
use webrender_traits::{WebrenderExternalImageRegistry, WebrenderImageHandlerType};
use webxr_api::SwapChainId as WebXRSwapChainId;

#[cfg(feature = "xr-profile")]
fn to_ms(ns: u64) -> f64 {
ns as f64 / 1_000_000.
}

struct GLContextData {
ctx: Context,
gl: Rc<Gl>,
Expand Down Expand Up @@ -329,8 +334,8 @@ impl WebGLThread {
WebGLMsg::CreateWebXRSwapChain(ctx_id, size, sender) => {
let _ = sender.send(self.create_webxr_swap_chain(ctx_id, size));
},
WebGLMsg::SwapBuffers(swap_ids, sender) => {
self.handle_swap_buffers(swap_ids, sender);
WebGLMsg::SwapBuffers(swap_ids, sender, sent_time) => {
self.handle_swap_buffers(swap_ids, sender, sent_time);
},
WebGLMsg::DOMToTextureCommand(command) => {
self.handle_dom_to_texture(command);
Expand Down Expand Up @@ -667,8 +672,16 @@ impl WebGLThread {
fn handle_swap_buffers(
&mut self,
swap_ids: Vec<SwapChainId>,
completed_sender: WebGLSender<()>,
completed_sender: WebGLSender<u64>,
_sent_time: u64,
) {
#[cfg(feature = "xr-profile")]
let start_swap = time::precise_time_ns();
#[cfg(feature = "xr-profile")]
println!(
"WEBXR PROFILING [swap request]:\t{}ms",
to_ms(start_swap - _sent_time)
);
debug!("handle_swap_buffers()");
for swap_id in swap_ids {
let context_id = swap_id.context_id();
Expand Down Expand Up @@ -733,7 +746,17 @@ impl WebGLThread {
);
}

completed_sender.send(()).unwrap();
#[allow(unused)]
let mut end_swap = 0;
#[cfg(feature = "xr-profile")]
{
end_swap = time::precise_time_ns();
println!(
"WEBXR PROFILING [swap buffer]:\t{}ms",
to_ms(end_swap - start_swap)
);
}
completed_sender.send(end_swap).unwrap();
}

/// Creates a new WebXR swap chain
Expand Down
2 changes: 2 additions & 0 deletions components/canvas_traits/Cargo.toml
Expand Up @@ -12,6 +12,7 @@ path = "lib.rs"

[features]
webgl_backtrace = []
xr-profile = ["webxr-api/profile", "time"]

[dependencies]
crossbeam-channel = "0.4"
Expand All @@ -26,6 +27,7 @@ serde = "1.0"
serde_bytes = "0.11"
servo_config = {path = "../config"}
sparkle = "0.1"
time = { version = "0.1.0", optional = true }
webrender_api = {git = "https://github.com/servo/webrender"}
webvr_traits = {path = "../webvr_traits"}
webxr-api = {git = "https://github.com/servo/webxr", features = ["ipc"]}
24 changes: 21 additions & 3 deletions components/canvas_traits/webgl.rs
Expand Up @@ -82,7 +82,11 @@ pub enum WebGLMsg {
WebGLSender<Option<WebXRSwapChainId>>,
),
/// Performs a buffer swap.
SwapBuffers(Vec<SwapChainId>, WebGLSender<()>),
///
/// The third field contains the time (in ns) when the request
/// was initiated. The u64 in the second field will be the time the
/// request is fulfilled
SwapBuffers(Vec<SwapChainId>, WebGLSender<u64>, u64),
/// Frees all resources and closes the thread.
Exit,
}
Expand Down Expand Up @@ -195,9 +199,23 @@ impl WebGLMsgSender {
.map(|id| SwapChainId::Framebuffer(self.ctx_id, id))
.unwrap_or_else(|| SwapChainId::Context(self.ctx_id));
let (sender, receiver) = webgl_channel()?;
#[allow(unused)]
let mut time = 0;
#[cfg(feature = "xr-profile")]
{
time = time::precise_time_ns();
}

self.sender
.send(WebGLMsg::SwapBuffers(vec![swap_id], sender))?;
receiver.recv()?;
.send(WebGLMsg::SwapBuffers(vec![swap_id], sender, time))?;

#[allow(unused)]
let sent_time = receiver.recv()?;
#[cfg(feature = "xr-profile")]
println!(
"WEBXR PROFILING [swap complete]:\t{}ms",
(time::precise_time_ns() - sent_time) as f64 / 1_000_000.
);
Ok(())
}

Expand Down
1 change: 1 addition & 0 deletions components/script/Cargo.toml
Expand Up @@ -22,6 +22,7 @@ js_backtrace = ["backtrace"]
refcell_backtrace = ["accountable-refcell"]
uwp = ["js/uwp"]
vslatestinstalled = ["js/vslatestinstalled"]
xr-profile = ["webxr-api/profile"]

[build-dependencies]
phf_codegen = "0.8"
Expand Down
8 changes: 7 additions & 1 deletion components/script/dom/document.rs
Expand Up @@ -2530,11 +2530,17 @@ impl Document {
return;
}

#[allow(unused)]
let mut time = 0;
#[cfg(feature = "xr-profile")]
{
time = time::precise_time_ns();
}
let (sender, receiver) = webgl::webgl_channel().unwrap();
self.window
.webgl_chan()
.expect("Where's the WebGL channel?")
.send(WebGLMsg::SwapBuffers(dirty_context_ids, sender))
.send(WebGLMsg::SwapBuffers(dirty_context_ids, sender, time))
.unwrap();
receiver.recv().unwrap();
}
Expand Down
26 changes: 25 additions & 1 deletion components/script/dom/xrsession.rs
Expand Up @@ -164,10 +164,21 @@ impl XRSession {
ROUTER.add_route(
frame_receiver.to_opaque(),
Box::new(move |message| {
#[allow(unused)]
let mut frame: Frame = message.to().unwrap();
#[cfg(feature = "xr-profile")]
{
let received = time::precise_time_ns();
println!(
"WEBXR PROFILING [raf receive]:\t{}ms",
(received - frame.sent_time) as f64 / 1_000_000.
);
frame.sent_time = received;
}
let this = this.clone();
let _ = task_source.queue_with_canceller(
task!(xr_raf_callback: move || {
this.root().raf_callback(message.to().unwrap());
this.root().raf_callback(frame);
}),
&canceller,
);
Expand Down Expand Up @@ -333,6 +344,13 @@ impl XRSession {
/// https://immersive-web.github.io/webxr/#xr-animation-frame
fn raf_callback(&self, mut frame: Frame) {
debug!("WebXR RAF callback");
#[cfg(feature = "xr-profile")]
let raf_start = time::precise_time_ns();
#[cfg(feature = "xr-profile")]
println!(
"WEBXR PROFILING [raf queued]:\t{}ms",
(raf_start - frame.sent_time) as f64 / 1_000_000.
);

// Step 1
if let Some(pending) = self.pending_render_state.take() {
Expand Down Expand Up @@ -389,6 +407,12 @@ impl XRSession {
self.session.borrow_mut().start_render_loop();
}

#[cfg(feature = "xr-profile")]
println!(
"WEBXR PROFILING [raf execute]:\t{}ms",
(time::precise_time_ns() - raf_start) as f64 / 1_000_000.
);

// If the canvas element is attached to the DOM, it is now dirty,
// and we need to trigger a reflow.
base_layer
Expand Down
1 change: 1 addition & 0 deletions components/servo/Cargo.toml
Expand Up @@ -39,6 +39,7 @@ webgl_backtrace = [
vslatestinstalled = ["script/vslatestinstalled"]
media-dummy = ["servo-media-dummy"]
media-gstreamer = ["servo-media-gstreamer", "gstreamer"]
xr-profile = ["canvas/xr-profile", "canvas_traits/xr-profile", "script/xr-profile", "webxr/profile"]

[dependencies]
background_hang_monitor = {path = "../background_hang_monitor"}
Expand Down
1 change: 1 addition & 0 deletions ports/glutin/Cargo.toml
Expand Up @@ -47,6 +47,7 @@ webdriver = ["libservo/webdriver"]
webgl_backtrace = ["libservo/webgl_backtrace"]
webrender_debugger = ["libservo/webrender_debugger"]
vslatestinstalled = ["libservo/vslatestinstalled"]
xr-profile = ["libservo/xr-profile"]

[target.'cfg(not(target_os = "android"))'.dependencies]
backtrace = "0.3"
Expand Down
1 change: 1 addition & 0 deletions ports/libsimpleservo/api/Cargo.toml
Expand Up @@ -54,3 +54,4 @@ webdriver = ["libservo/webdriver"]
uwp = ["libservo/uwp", "webxr/openxr-api"]
webgl_backtrace = ["libservo/webgl_backtrace"]
vslatestinstalled = ["libservo/vslatestinstalled"]
xr-profile = ["libservo/xr-profile"]
1 change: 1 addition & 0 deletions ports/libsimpleservo/capi/Cargo.toml
Expand Up @@ -49,3 +49,4 @@ uwp = ["simpleservo/uwp"]
webdriver = ["simpleservo/webdriver"]
webgl_backtrace = ["simpleservo/webgl_backtrace"]
vslatestinstalled = ["simpleservo/vslatestinstalled"]
xr-profile = ["simpleservo/xr-profile"]

0 comments on commit 6bc4a7d

Please sign in to comment.