Skip to content

Commit e7a8ba9

Browse files
committed
na-mainloop: Use new NativeWindow::lock() API
1 parent a05efc2 commit e7a8ba9

File tree

2 files changed

+31
-15
lines changed

2 files changed

+31
-15
lines changed

na-mainloop/Cargo.toml

+11-7
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,15 @@ name = "na-mainloop"
33
version = "0.1.0"
44
edition = "2021"
55

6-
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7-
86
[dependencies]
97
log = "0.4"
108
android_logger = "0.11.0"
11-
android-activity = { version = "0.4", features = [ "native-activity" ] }
9+
android-activity = { version = "0.4", features = ["native-activity"] }
10+
ndk = "0.7"
1211

1312
[lib]
14-
#name="na_mainloop"
15-
crate_type=["cdylib"]
13+
# name = "na_mainloop"
14+
crate_type = ["cdylib"]
1615

1716

1817
####################
@@ -24,7 +23,7 @@ crate_type=["cdylib"]
2423
package = "com.foo.bar"
2524

2625
# Specifies the array of targets to build for.
27-
build_targets = [ "aarch64-linux-android" ]
26+
build_targets = ["aarch64-linux-android"]
2827

2928
# Path to your application's resources folder.
3029
# If not specified, resources will not be included in the APK.
@@ -179,4 +178,9 @@ label = "Application Name"
179178
#port = "8080"
180179
#path = "/rust-windowing/android-ndk-rs/tree/master/cargo-apk"
181180
#path_prefix = "/rust-windowing/"
182-
#mime_type = "image/jpeg"
181+
#mime_type = "image/jpeg"
182+
183+
[patch.crates-io]
184+
android-activity = { git = "https://github.com/MarijnS95/android-activity", rev = "d8eade5" }
185+
ndk = { git = "https://github.com/rust-mobile/ndk", rev = "b520751" }
186+
ndk-sys = { git = "https://github.com/rust-mobile/ndk", rev = "b520751" }

na-mainloop/src/lib.rs

+20-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use android_activity::{AndroidApp, InputStatus, MainEvent, PollEvent};
22
use log::info;
3+
use ndk::native_window::HardwareBufferFormat;
34

45
#[no_mangle]
56
fn 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.
93102
fn 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

Comments
 (0)