Skip to content

Commit

Permalink
More experimentation
Browse files Browse the repository at this point in the history
  • Loading branch information
DJMcNab committed Oct 29, 2024
1 parent f450d43 commit 82b14bd
Show file tree
Hide file tree
Showing 3 changed files with 248 additions and 42 deletions.
6 changes: 5 additions & 1 deletion examples/with_winit/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,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"] }
ndk = { version = "0.9", features = ["api-level-33", "nativewindow"] }

[target.'cfg(target_arch = "wasm32")'.dependencies]
console_error_panic_hook = "0.1.7"
Expand All @@ -75,3 +75,7 @@ getrandom = { version = "0.2.15", features = ["js"] }

[package.metadata.android.application]
debuggable = true

[package.metadata.android.sdk]
target_sdk_version = 33
min_sdk_version = 33
117 changes: 90 additions & 27 deletions examples/with_winit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use std::collections::HashSet;
use std::num::NonZeroUsize;
use std::rc::Rc;
use std::sync::Arc;

#[cfg(not(target_arch = "wasm32"))]
Expand All @@ -16,6 +17,8 @@ use web_time::Instant;
use winit::application::ApplicationHandler;
use winit::event::*;
use winit::keyboard::*;
use winit::raw_window_handle::HasRawWindowHandle;
use winit::raw_window_handle::HasWindowHandle;
use winit::window::WindowId;

#[cfg(all(feature = "wgpu-profiler", not(target_arch = "wasm32")))]
Expand Down Expand Up @@ -169,7 +172,7 @@ struct VelloApp<'s> {
modifiers: ModifiersState,

debug: DebugLayers,
choreographer: Option<ndk::choreographer::Choreographer>,
choreographer: Option<Rc<ndk::choreographer::Choreographer>>,
animation_in_flight: bool,
proxy: winit::event_loop::EventLoopProxy<UserEvent>,
}
Expand Down Expand Up @@ -405,13 +408,45 @@ impl<'s> ApplicationHandler<UserEvent> for VelloApp<'s> {
// in a touch context (i.e. Windows/Linux/MacOS with a touch screen could
// also be using mouse/keyboard controls)
// Note that winit's rendering is y-down
if let Some(RenderState { surface, .. }) = &self.state {
if let Some(RenderState { surface, window }) = &self.state {
if touch.location.y > surface.config.height as f64 * 2. / 3. {
self.navigation_fingers.insert(touch.id);
// The left third of the navigation zone navigates backwards
if touch.location.x < surface.config.width as f64 / 3. {
if let wgpu::rwh::RawWindowHandle::AndroidNdk(
android_ndk_window_handle,
) = window.window_handle().unwrap().as_raw()
{
let window = unsafe {
ndk::native_window::NativeWindow::clone_from_ptr(
android_ndk_window_handle.a_native_window.cast(),
)
};
window
.set_frame_rate(
60.,
ndk::native_window::FrameRateCompatibility::Default,
)
.unwrap();
}
self.scene_ix = self.scene_ix.saturating_sub(1);
} else if touch.location.x > 2. * surface.config.width as f64 / 3. {
if let wgpu::rwh::RawWindowHandle::AndroidNdk(
android_ndk_window_handle,
) = window.window_handle().unwrap().as_raw()
{
let window = unsafe {
ndk::native_window::NativeWindow::clone_from_ptr(
android_ndk_window_handle.a_native_window.cast(),
)
};
window
.set_frame_rate(
90.,
ndk::native_window::FrameRateCompatibility::Default,
)
.unwrap();
}
self.scene_ix = self.scene_ix.saturating_add(1);
}
}
Expand Down Expand Up @@ -608,9 +643,9 @@ impl<'s> ApplicationHandler<UserEvent> for VelloApp<'s> {
// let result = display_timing.get_refresh_cycle_duration(swc);
// eprintln!("Refresh duration: {result:?}");
if present_id % 5 == 0 {
let result = display_timing.get_past_presentation_timing(swc);
eprintln!("Display timings: {result:?}");
eprintln!("Most recent present id: {}", present_id);
// let result = display_timing.get_past_presentation_timing(swc);
// eprintln!("Display timings: {result:?}");
// eprintln!("Most recent present id: {}", present_id);
}
}
}
Expand Down Expand Up @@ -666,27 +701,27 @@ impl<'s> ApplicationHandler<UserEvent> for VelloApp<'s> {

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;
// 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().min(3)) {
// eprintln!(
// "{:?} {}",
// frame.frame_timeline_deadline(timeline) - frame_time,
// if timeline == preferred_index {
// "(Preferred)"
// } else {
// ""
// }
// );
// }
// eprintln!("{frame:?}");
// // proxy
// // .send_event(UserEvent::ChoreographerFrame(window_id))
// // .unwrap();
// }));
window.request_redraw();
} else {
window.request_redraw();
}
Expand Down Expand Up @@ -856,10 +891,38 @@ fn run(
modifiers: ModifiersState::default(),
debug,
// We know looper is active since we have the `EventLoop`
choreographer: ndk::choreographer::Choreographer::instance(),
choreographer: ndk::choreographer::Choreographer::instance().map(Rc::new),
proxy: event_loop.create_proxy(),
animation_in_flight: false,
};
if let Some(choreographer) = app.choreographer.as_ref() {
fn post_callback(choreographer: &Rc<ndk::choreographer::Choreographer>) {
let new_choreographer = Rc::clone(choreographer);
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().min(4)) {
eprintln!(
"{:?} {}",
frame.frame_timeline_deadline(timeline) - frame_time,
if timeline == preferred_index {
"(Preferred)"
} else {
""
}
);
}
eprintln!("{frame:?}");
post_callback(&new_choreographer);
}));
}
// post_callback(choreographer);
choreographer.register_refresh_rate_callback(Box::new(|value| {
let span = tracing::info_span!("Getting a new refresh rate", ?value).entered();
eprintln!("New refresh rate Testing: {value:?}; {}", value.as_nanos());
}));
}

event_loop.run_app(&mut app).expect("run to completion");
}
Expand Down
Loading

0 comments on commit 82b14bd

Please sign in to comment.