Skip to content

Commit

Permalink
Keep exploring with choreographer
Browse files Browse the repository at this point in the history
  • Loading branch information
DJMcNab committed Sep 26, 2024
1 parent 33f2a7a commit ee980ef
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 22 deletions.
15 changes: 3 additions & 12 deletions Cargo.lock

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

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,6 @@ scenes = { path = "examples/scenes" }

[patch.crates-io]
# "Choreographer" branch: https://github.com/rust-mobile/ndk/tree/choreographer
ndk = { git = "https://github.com/rust-mobile/ndk/", rev = "01425d88813a4bdf02fd58abd34a6bbfb6383def" }
# 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" }
33 changes: 24 additions & 9 deletions examples/with_winit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ use std::collections::HashSet;
use std::num::NonZeroUsize;
use std::sync::Arc;

use ndk::choreographer;
#[cfg(not(target_arch = "wasm32"))]
use std::time::Instant;
#[cfg(target_arch = "wasm32")]
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 @@ -165,6 +165,8 @@ struct VelloApp<'s> {

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 @@ -443,8 +445,6 @@ impl<'s> ApplicationHandler<UserEvent> for VelloApp<'s> {
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 All @@ -463,11 +463,6 @@ impl<'s> ApplicationHandler<UserEvent> for VelloApp<'s> {
self.prev_scene_ix = self.scene_ix;
window.set_title(&format!("Vello demo - {}", example_scene.config.name));
}
if example_scene.config.animated {
if let Some(choreographer) = self.choreographer {
choreographer.post_frame_callback(Box::new(|time| eprintln!("{time}")));
}
}
self.fragment.reset();
let mut scene_params = SceneParams {
time: self.start.elapsed().as_secs_f64(),
Expand Down Expand Up @@ -594,6 +589,20 @@ 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 example_scene.config.animated {
if let Some(choreographer) = self.choreographer.as_ref() {
let proxy = self.proxy.clone();
choreographer.post_vsync_callback(Box::new(move |frame| {
eprintln!("{frame:?}");
proxy
.send_event(UserEvent::ChoreographerFrame(window_id))
.unwrap();
}));
self.animation_in_flight = true;
} else {
window.request_redraw();
}
}
}
_ => {}
}
Expand All @@ -617,7 +626,7 @@ impl<'s> ApplicationHandler<UserEvent> for VelloApp<'s> {
}
}

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 @@ -637,6 +646,9 @@ impl<'s> ApplicationHandler<UserEvent> for VelloApp<'s> {
Err(e) => log::error!("Failed to reload shaders: {e}"),
}
}
UserEvent::ChoreographerFrame(window_id) => {
self.window_event(event_loop, window_id, WindowEvent::RedrawRequested);
}
}
}

Expand Down Expand Up @@ -753,6 +765,8 @@ fn run(
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 @@ -791,6 +805,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

0 comments on commit ee980ef

Please sign in to comment.