Skip to content

Commit 216a7ef

Browse files
committed
More experimentation
1 parent f21b180 commit 216a7ef

File tree

3 files changed

+248
-42
lines changed

3 files changed

+248
-42
lines changed

examples/with_winit/Cargo.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ tracing-subscriber = { version = "0.3.18", default-features = false, features =
6363
"registry",
6464
] }
6565
profiling = { version = "1.0.15", features = ["profile-with-tracing"] }
66-
ndk = { version = "0.9", features = ["api-level-33"] }
66+
ndk = { version = "0.9", features = ["api-level-33", "nativewindow"] }
6767

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

7676
[package.metadata.android.application]
7777
debuggable = true
78+
79+
[package.metadata.android.sdk]
80+
target_sdk_version = 33
81+
min_sdk_version = 33

examples/with_winit/src/lib.rs

Lines changed: 90 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use std::collections::HashSet;
88
use std::num::NonZeroUsize;
9+
use std::rc::Rc;
910
use std::sync::Arc;
1011

1112
#[cfg(not(target_arch = "wasm32"))]
@@ -15,6 +16,8 @@ use web_time::Instant;
1516
use winit::application::ApplicationHandler;
1617
use winit::event::*;
1718
use winit::keyboard::*;
19+
use winit::raw_window_handle::HasRawWindowHandle;
20+
use winit::raw_window_handle::HasWindowHandle;
1821
use winit::window::WindowId;
1922

2023
#[cfg(all(feature = "wgpu-profiler", not(target_arch = "wasm32")))]
@@ -168,7 +171,7 @@ struct VelloApp<'s> {
168171
modifiers: ModifiersState,
169172

170173
debug: vello::DebugLayers,
171-
choreographer: Option<ndk::choreographer::Choreographer>,
174+
choreographer: Option<Rc<ndk::choreographer::Choreographer>>,
172175
animation_in_flight: bool,
173176
proxy: winit::event_loop::EventLoopProxy<UserEvent>,
174177
}
@@ -405,13 +408,45 @@ impl<'s> ApplicationHandler<UserEvent> for VelloApp<'s> {
405408
// in a touch context (i.e. Windows/Linux/MacOS with a touch screen could
406409
// also be using mouse/keyboard controls)
407410
// Note that winit's rendering is y-down
408-
if let Some(RenderState { surface, .. }) = &self.state {
411+
if let Some(RenderState { surface, window }) = &self.state {
409412
if touch.location.y > surface.config.height as f64 * 2. / 3. {
410413
self.navigation_fingers.insert(touch.id);
411414
// The left third of the navigation zone navigates backwards
412415
if touch.location.x < surface.config.width as f64 / 3. {
416+
if let wgpu::rwh::RawWindowHandle::AndroidNdk(
417+
android_ndk_window_handle,
418+
) = window.window_handle().unwrap().as_raw()
419+
{
420+
let window = unsafe {
421+
ndk::native_window::NativeWindow::clone_from_ptr(
422+
android_ndk_window_handle.a_native_window.cast(),
423+
)
424+
};
425+
window
426+
.set_frame_rate(
427+
60.,
428+
ndk::native_window::FrameRateCompatibility::Default,
429+
)
430+
.unwrap();
431+
}
413432
self.scene_ix = self.scene_ix.saturating_sub(1);
414433
} else if touch.location.x > 2. * surface.config.width as f64 / 3. {
434+
if let wgpu::rwh::RawWindowHandle::AndroidNdk(
435+
android_ndk_window_handle,
436+
) = window.window_handle().unwrap().as_raw()
437+
{
438+
let window = unsafe {
439+
ndk::native_window::NativeWindow::clone_from_ptr(
440+
android_ndk_window_handle.a_native_window.cast(),
441+
)
442+
};
443+
window
444+
.set_frame_rate(
445+
90.,
446+
ndk::native_window::FrameRateCompatibility::Default,
447+
)
448+
.unwrap();
449+
}
415450
self.scene_ix = self.scene_ix.saturating_add(1);
416451
}
417452
}
@@ -609,9 +644,9 @@ impl<'s> ApplicationHandler<UserEvent> for VelloApp<'s> {
609644
// let result = display_timing.get_refresh_cycle_duration(swc);
610645
// eprintln!("Refresh duration: {result:?}");
611646
if present_id % 5 == 0 {
612-
let result = display_timing.get_past_presentation_timing(swc);
613-
eprintln!("Display timings: {result:?}");
614-
eprintln!("Most recent present id: {}", present_id);
647+
// let result = display_timing.get_past_presentation_timing(swc);
648+
// eprintln!("Display timings: {result:?}");
649+
// eprintln!("Most recent present id: {}", present_id);
615650
}
616651
}
617652
}
@@ -664,27 +699,27 @@ impl<'s> ApplicationHandler<UserEvent> for VelloApp<'s> {
664699

665700
if let Some(choreographer) = self.choreographer.as_ref() {
666701
let proxy = self.proxy.clone();
667-
choreographer.post_vsync_callback(Box::new(move |frame| {
668-
// eprintln!("New frame");
669-
// let frame_time = frame.frame_time();
670-
// let preferred_index = frame.preferred_frame_timeline_index();
671-
// for timeline in 0..frame.frame_timelines_length() {
672-
// eprintln!(
673-
// "{:?} {}",
674-
// frame.frame_timeline_deadline(timeline) - frame_time,
675-
// if timeline == preferred_index {
676-
// "(Preferred)"
677-
// } else {
678-
// ""
679-
// }
680-
// );
681-
// }
682-
// eprintln!("{frame:?}");
683-
proxy
684-
.send_event(UserEvent::ChoreographerFrame(window_id))
685-
.unwrap();
686-
}));
687-
self.animation_in_flight = true;
702+
// choreographer.post_vsync_callback(Box::new(move |frame| {
703+
// eprintln!("New frame");
704+
// let frame_time = frame.frame_time();
705+
// let preferred_index = frame.preferred_frame_timeline_index();
706+
// for timeline in 0..(frame.frame_timelines_length().min(3)) {
707+
// eprintln!(
708+
// "{:?} {}",
709+
// frame.frame_timeline_deadline(timeline) - frame_time,
710+
// if timeline == preferred_index {
711+
// "(Preferred)"
712+
// } else {
713+
// ""
714+
// }
715+
// );
716+
// }
717+
// eprintln!("{frame:?}");
718+
// // proxy
719+
// // .send_event(UserEvent::ChoreographerFrame(window_id))
720+
// // .unwrap();
721+
// }));
722+
window.request_redraw();
688723
} else {
689724
window.request_redraw();
690725
}
@@ -854,10 +889,38 @@ fn run(
854889
modifiers: ModifiersState::default(),
855890
debug,
856891
// We know looper is active since we have the `EventLoop`
857-
choreographer: ndk::choreographer::Choreographer::instance(),
892+
choreographer: ndk::choreographer::Choreographer::instance().map(Rc::new),
858893
proxy: event_loop.create_proxy(),
859894
animation_in_flight: false,
860895
};
896+
if let Some(choreographer) = app.choreographer.as_ref() {
897+
fn post_callback(choreographer: &Rc<ndk::choreographer::Choreographer>) {
898+
let new_choreographer = Rc::clone(choreographer);
899+
choreographer.post_vsync_callback(Box::new(move |frame| {
900+
eprintln!("New frame");
901+
let frame_time = frame.frame_time();
902+
let preferred_index = frame.preferred_frame_timeline_index();
903+
for timeline in 0..(frame.frame_timelines_length().min(4)) {
904+
eprintln!(
905+
"{:?} {}",
906+
frame.frame_timeline_deadline(timeline) - frame_time,
907+
if timeline == preferred_index {
908+
"(Preferred)"
909+
} else {
910+
""
911+
}
912+
);
913+
}
914+
eprintln!("{frame:?}");
915+
post_callback(&new_choreographer);
916+
}));
917+
}
918+
// post_callback(choreographer);
919+
choreographer.register_refresh_rate_callback(Box::new(|value| {
920+
let span = tracing::info_span!("Getting a new refresh rate", ?value).entered();
921+
eprintln!("New refresh rate Testing: {value:?}; {}", value.as_nanos());
922+
}));
923+
}
861924

862925
event_loop.run_app(&mut app).expect("run to completion");
863926
}

0 commit comments

Comments
 (0)