11use android_activity:: { AndroidApp , InputStatus , MainEvent , PollEvent } ;
22use log:: info;
3+ use ndk:: native_window:: HardwareBufferFormat ;
34
45#[ no_mangle]
56fn android_main ( app : AndroidApp ) {
@@ -38,6 +39,14 @@ fn android_main(app: AndroidApp) {
3839 }
3940 MainEvent :: InitWindow { .. } => {
4041 native_window = app. native_window ( ) ;
42+ if let Some ( nw) = & native_window {
43+ nw. set_buffers_geometry (
44+ 0 ,
45+ 0 ,
46+ Some ( HardwareBufferFormat :: R8G8B8A8_UNORM ) ,
47+ )
48+ . unwrap ( )
49+ }
4150 redraw_pending = true ;
4251 }
4352 MainEvent :: TerminateWindow { .. } => {
@@ -91,12 +100,15 @@ fn android_main(app: AndroidApp) {
91100/// responsive, otherwise it will stop delivering input
92101/// events to us.
93102fn dummy_render ( native_window : & ndk:: native_window:: NativeWindow ) {
94- unsafe {
95- let mut buf: ndk_sys:: ANativeWindow_Buffer = std:: mem:: zeroed ( ) ;
96- let mut rect: ndk_sys:: ARect = std:: mem:: zeroed ( ) ;
97- ndk_sys:: ANativeWindow_lock ( native_window. ptr ( ) . as_ptr ( ) as _ , & mut buf as _ , & mut rect as _ ) ;
98- // Note: we don't try and touch the buffer since that
99- // also requires us to handle various buffer formats
100- ndk_sys:: ANativeWindow_unlockAndPost ( native_window. ptr ( ) . as_ptr ( ) as _ ) ;
103+ let mut lock = native_window. lock ( None ) . unwrap ( ) ;
104+ let ( w, h) = ( lock. width ( ) , lock. height ( ) ) ;
105+
106+ for ( y, line) in lock. lines ( ) . unwrap ( ) . enumerate ( ) {
107+ let r = y * 255 / h;
108+ for ( x, pixels) in line. chunks_mut ( 4 ) . enumerate ( ) {
109+ let g = x * 255 / w;
110+ pixels[ 0 ] . write ( r as u8 ) ;
111+ pixels[ 1 ] . write ( g as u8 ) ;
112+ }
101113 }
102- }
114+ }
0 commit comments