diff --git a/na-mainloop/Cargo.toml b/na-mainloop/Cargo.toml index 63f234c..b7de3e5 100644 --- a/na-mainloop/Cargo.toml +++ b/na-mainloop/Cargo.toml @@ -3,19 +3,16 @@ name = "na-mainloop" version = "0.1.0" edition = "2021" -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - [dependencies] log = "0.4" android_logger = "0.11.0" -android-activity = { version = "0.4", features = [ "native-activity" ] } -#android-activity = { path = "../../android-activity/android-activity", features = [ "native-activity" ] } +android-activity = { version = "0.4", features = ["native-activity"] } ndk-sys = "0.4" ndk = "0.7" [lib] -#name="na_mainloop" -crate_type=["cdylib"] +# name = "na_mainloop" +crate_type = ["cdylib"] #################### @@ -27,7 +24,7 @@ crate_type=["cdylib"] package = "com.foo.bar" # Specifies the array of targets to build for. -build_targets = [ "aarch64-linux-android" ] +build_targets = ["aarch64-linux-android"] # Path to your application's resources folder. # If not specified, resources will not be included in the APK. @@ -182,4 +179,10 @@ label = "Application Name" #port = "8080" #path = "/rust-windowing/android-ndk-rs/tree/master/cargo-apk" #path_prefix = "/rust-windowing/" -#mime_type = "image/jpeg" \ No newline at end of file +#mime_type = "image/jpeg" + +[patch.crates-io] +# https://github.com/MarijnS95/android-activity/tree/ndk-breaking-prep +android-activity = { git = "https://github.com/MarijnS95/android-activity", rev = "d8eade5" } +ndk = { git = "https://github.com/rust-mobile/ndk", rev = "b520751" } +ndk-sys = { git = "https://github.com/rust-mobile/ndk", rev = "b520751" } diff --git a/na-mainloop/src/lib.rs b/na-mainloop/src/lib.rs index 166ee69..3d33f89 100644 --- a/na-mainloop/src/lib.rs +++ b/na-mainloop/src/lib.rs @@ -1,5 +1,6 @@ use android_activity::{AndroidApp, InputStatus, MainEvent, PollEvent}; use log::info; +use ndk::native_window::HardwareBufferFormat; #[no_mangle] fn android_main(app: AndroidApp) { @@ -38,6 +39,14 @@ fn android_main(app: AndroidApp) { } MainEvent::InitWindow { .. } => { native_window = app.native_window(); + if let Some(nw) = &native_window { + nw.set_buffers_geometry( + 0, + 0, + Some(HardwareBufferFormat::R8G8B8A8_UNORM), + ) + .unwrap() + } redraw_pending = true; } MainEvent::TerminateWindow { .. } => { @@ -91,16 +100,15 @@ fn android_main(app: AndroidApp) { /// responsive, otherwise it will stop delivering input /// events to us. fn dummy_render(native_window: &ndk::native_window::NativeWindow) { - unsafe { - let mut buf: ndk_sys::ANativeWindow_Buffer = std::mem::zeroed(); - let mut rect: ndk_sys::ARect = std::mem::zeroed(); - ndk_sys::ANativeWindow_lock( - native_window.ptr().as_ptr() as _, - &mut buf as _, - &mut rect as _, - ); - // Note: we don't try and touch the buffer since that - // also requires us to handle various buffer formats - ndk_sys::ANativeWindow_unlockAndPost(native_window.ptr().as_ptr() as _); + let mut lock = native_window.lock(None).unwrap(); + let (w, h) = (lock.width(), lock.height()); + + for (y, line) in lock.lines().unwrap().enumerate() { + let r = y * 255 / h; + for (x, pixels) in line.chunks_mut(4).enumerate() { + let g = x * 255 / w; + pixels[0].write(r as u8); + pixels[1].write(g as u8); + } } }