Skip to content

Commit 0cd504c

Browse files
committed
na-mainloop: Use new NativeWindow::lock() API
1 parent 3ad2b59 commit 0cd504c

File tree

2 files changed

+30
-19
lines changed

2 files changed

+30
-19
lines changed

na-mainloop/Cargo.toml

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,16 @@ 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" ] }
12-
#android-activity = { path = "../../android-activity/android-activity", features = [ "native-activity" ] }
9+
android-activity = { version = "0.4", features = ["native-activity"] }
1310
ndk-sys = "0.4"
1411
ndk = "0.7"
1512

1613
[lib]
17-
#name="na_mainloop"
18-
crate_type=["cdylib"]
14+
# name = "na_mainloop"
15+
crate_type = ["cdylib"]
1916

2017

2118
####################
@@ -27,7 +24,7 @@ crate_type=["cdylib"]
2724
package = "com.foo.bar"
2825

2926
# Specifies the array of targets to build for.
30-
build_targets = [ "aarch64-linux-android" ]
27+
build_targets = ["aarch64-linux-android"]
3128

3229
# Path to your application's resources folder.
3330
# If not specified, resources will not be included in the APK.
@@ -182,4 +179,10 @@ label = "Application Name"
182179
#port = "8080"
183180
#path = "/rust-windowing/android-ndk-rs/tree/master/cargo-apk"
184181
#path_prefix = "/rust-windowing/"
185-
#mime_type = "image/jpeg"
182+
#mime_type = "image/jpeg"
183+
184+
[patch.crates-io]
185+
# https://github.com/MarijnS95/android-activity/tree/ndk-breaking-prep
186+
android-activity = { git = "https://github.com/MarijnS95/android-activity", rev = "d8eade5" }
187+
ndk = { git = "https://github.com/rust-mobile/ndk", rev = "b520751" }
188+
ndk-sys = { git = "https://github.com/rust-mobile/ndk", rev = "b520751" }

na-mainloop/src/lib.rs

Lines changed: 19 additions & 11 deletions
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,16 +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(
98-
native_window.ptr().as_ptr() as _,
99-
&mut buf as _,
100-
&mut rect as _,
101-
);
102-
// Note: we don't try and touch the buffer since that
103-
// also requires us to handle various buffer formats
104-
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+
}
105113
}
106114
}

0 commit comments

Comments
 (0)