Skip to content

Commit

Permalink
na-mainloop: Use new NativeWindow::lock() API
Browse files Browse the repository at this point in the history
  • Loading branch information
MarijnS95 committed Jun 25, 2023
1 parent 3ad2b59 commit 0cd504c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 19 deletions.
19 changes: 11 additions & 8 deletions na-mainloop/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]


####################
Expand All @@ -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.
Expand Down Expand Up @@ -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"
#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" }
30 changes: 19 additions & 11 deletions na-mainloop/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down Expand Up @@ -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 { .. } => {
Expand Down Expand Up @@ -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);
}
}
}

0 comments on commit 0cd504c

Please sign in to comment.