Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Experimental integration with Achoreographer #703

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions Cargo.lock

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

6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,9 @@ pollster = "0.3.0"
web-time = "1.1.0"
wgpu-profiler = "0.18.0"
scenes = { path = "examples/scenes" }

[patch.crates-io]
# "Choreographer" branch: https://github.com/rust-mobile/ndk/tree/choreographer
# With a patch to make version numbers work https://github.com/djmcnab/ndk/tree/choreographer
ndk = { git = "https://github.com/djmcnab/ndk/", rev = "81627a7564982a04027a49d538cac1686d870dbd" }
ndk-sys = { git = "https://github.com/djmcnab/ndk/", rev = "81627a7564982a04027a49d538cac1686d870dbd" }
4 changes: 4 additions & 0 deletions examples/with_winit/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ tracing-subscriber = { version = "0.3.18", default-features = false, features =
"registry",
] }
profiling = { version = "1.0.15", features = ["profile-with-tracing"] }
ndk = { version = "0.9", features = ["api-level-33"] }

[target.'cfg(target_arch = "wasm32")'.dependencies]
console_error_panic_hook = "0.1.7"
Expand All @@ -70,3 +71,6 @@ wasm-bindgen-futures = "0.4.43"
web-sys = { version = "0.3.70", features = ["HtmlCollection", "Text"] }
web-time = { workspace = true }
getrandom = { version = "0.2.15", features = ["js"] }

[package.metadata.android.application]
debuggable = true
55 changes: 49 additions & 6 deletions examples/with_winit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use web_time::Instant;
use winit::application::ApplicationHandler;
use winit::event::*;
use winit::keyboard::*;
use winit::window::WindowId;

#[cfg(all(feature = "wgpu-profiler", not(target_arch = "wasm32")))]
use std::time::Duration;
Expand Down Expand Up @@ -163,6 +164,9 @@ struct VelloApp<'s> {
modifiers: ModifiersState,

debug: vello::DebugLayers,
choreographer: Option<ndk::choreographer::Choreographer>,
animation_in_flight: bool,
proxy: winit::event_loop::EventLoopProxy<UserEvent>,
}

impl<'s> ApplicationHandler<UserEvent> for VelloApp<'s> {
Expand Down Expand Up @@ -438,11 +442,12 @@ impl<'s> ApplicationHandler<UserEvent> for VelloApp<'s> {
self.prior_position = Some(position);
}
WindowEvent::RedrawRequested => {
if self.animation_in_flight {
return;
}
let _rendering_span = tracing::trace_span!("Actioning Requested Redraw").entered();
let encoding_span = tracing::trace_span!("Encoding scene").entered();

render_state.window.request_redraw();

let Some(RenderState { surface, window }) = &self.state else {
return;
};
Expand Down Expand Up @@ -587,6 +592,33 @@ impl<'s> ApplicationHandler<UserEvent> for VelloApp<'s> {
frame_time_us: (new_time - self.frame_start_time).as_micros() as u64,
});
self.frame_start_time = new_time;

if let Some(choreographer) = self.choreographer.as_ref() {
let proxy = self.proxy.clone();
choreographer.post_vsync_callback(Box::new(move |frame| {
// eprintln!("New frame");
// let frame_time = frame.frame_time();
// let preferred_index = frame.preferred_frame_timeline_index();
// for timeline in 0..frame.frame_timelines_length() {
// eprintln!(
// "{:?} {}",
// frame.frame_timeline_deadline(timeline) - frame_time,
// if timeline == preferred_index {
// "(Preferred)"
// } else {
// ""
// }
// );
// }
// eprintln!("{frame:?}");
proxy
.send_event(UserEvent::ChoreographerFrame(window_id))
.unwrap();
}));
self.animation_in_flight = true;
} else {
window.request_redraw();
}
}
_ => {}
}
Expand All @@ -605,12 +637,14 @@ impl<'s> ApplicationHandler<UserEvent> for VelloApp<'s> {
* self.transform;
}

if let Some(render_state) = &mut self.state {
render_state.window.request_redraw();
}
// if let Some(render_state) = &mut self.state {
// if !self.animation_in_flight {
// render_state.window.request_redraw();
// }
// }
}

fn user_event(&mut self, _event_loop: &winit::event_loop::ActiveEventLoop, event: UserEvent) {
fn user_event(&mut self, event_loop: &winit::event_loop::ActiveEventLoop, event: UserEvent) {
match event {
#[cfg(not(any(target_arch = "wasm32", target_os = "android")))]
UserEvent::HotReload => {
Expand All @@ -630,6 +664,10 @@ impl<'s> ApplicationHandler<UserEvent> for VelloApp<'s> {
Err(e) => log::error!("Failed to reload shaders: {e}"),
}
}
UserEvent::ChoreographerFrame(window_id) => {
self.animation_in_flight = false;
self.window_event(event_loop, window_id, WindowEvent::RedrawRequested);
}
}
}

Expand Down Expand Up @@ -744,6 +782,10 @@ fn run(
prev_scene_ix: 0,
modifiers: ModifiersState::default(),
debug,
// We know looper is active since we have the `EventLoop`
choreographer: ndk::choreographer::Choreographer::instance(),
proxy: event_loop.create_proxy(),
animation_in_flight: false,
};

event_loop.run_app(&mut app).expect("run to completion");
Expand Down Expand Up @@ -782,6 +824,7 @@ fn window_attributes() -> WindowAttributes {
enum UserEvent {
#[cfg(not(any(target_arch = "wasm32", target_os = "android")))]
HotReload,
ChoreographerFrame(WindowId),
}

#[cfg(target_arch = "wasm32")]
Expand Down
Loading