6
6
7
7
use std:: collections:: HashSet ;
8
8
use std:: num:: NonZeroUsize ;
9
+ use std:: rc:: Rc ;
9
10
use std:: sync:: Arc ;
10
11
11
12
#[ cfg( not( target_arch = "wasm32" ) ) ]
@@ -15,6 +16,8 @@ use web_time::Instant;
15
16
use winit:: application:: ApplicationHandler ;
16
17
use winit:: event:: * ;
17
18
use winit:: keyboard:: * ;
19
+ use winit:: raw_window_handle:: HasRawWindowHandle ;
20
+ use winit:: raw_window_handle:: HasWindowHandle ;
18
21
use winit:: window:: WindowId ;
19
22
20
23
#[ cfg( all( feature = "wgpu-profiler" , not( target_arch = "wasm32" ) ) ) ]
@@ -168,7 +171,7 @@ struct VelloApp<'s> {
168
171
modifiers : ModifiersState ,
169
172
170
173
debug : vello:: DebugLayers ,
171
- choreographer : Option < ndk:: choreographer:: Choreographer > ,
174
+ choreographer : Option < Rc < ndk:: choreographer:: Choreographer > > ,
172
175
animation_in_flight : bool ,
173
176
proxy : winit:: event_loop:: EventLoopProxy < UserEvent > ,
174
177
}
@@ -405,13 +408,45 @@ impl<'s> ApplicationHandler<UserEvent> for VelloApp<'s> {
405
408
// in a touch context (i.e. Windows/Linux/MacOS with a touch screen could
406
409
// also be using mouse/keyboard controls)
407
410
// 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 {
409
412
if touch. location . y > surface. config . height as f64 * 2. / 3. {
410
413
self . navigation_fingers . insert ( touch. id ) ;
411
414
// The left third of the navigation zone navigates backwards
412
415
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
+ }
413
432
self . scene_ix = self . scene_ix . saturating_sub ( 1 ) ;
414
433
} 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
+ }
415
450
self . scene_ix = self . scene_ix . saturating_add ( 1 ) ;
416
451
}
417
452
}
@@ -609,9 +644,9 @@ impl<'s> ApplicationHandler<UserEvent> for VelloApp<'s> {
609
644
// let result = display_timing.get_refresh_cycle_duration(swc);
610
645
// eprintln!("Refresh duration: {result:?}");
611
646
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);
615
650
}
616
651
}
617
652
}
@@ -664,27 +699,27 @@ impl<'s> ApplicationHandler<UserEvent> for VelloApp<'s> {
664
699
665
700
if let Some ( choreographer) = self . choreographer . as_ref ( ) {
666
701
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 ( ) ;
688
723
} else {
689
724
window. request_redraw ( ) ;
690
725
}
@@ -854,10 +889,38 @@ fn run(
854
889
modifiers : ModifiersState :: default ( ) ,
855
890
debug,
856
891
// 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 ) ,
858
893
proxy : event_loop. create_proxy ( ) ,
859
894
animation_in_flight : false ,
860
895
} ;
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
+ }
861
924
862
925
event_loop. run_app ( & mut app) . expect ( "run to completion" ) ;
863
926
}
0 commit comments