Skip to content

Commit c5954d6

Browse files
committed
Use new NativeWindow::lock() API in dummy_render()
1 parent 3ad2b59 commit c5954d6

File tree

9 files changed

+615
-270
lines changed

9 files changed

+615
-270
lines changed

agdk-mainloop/Cargo.lock

Lines changed: 227 additions & 62 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

agdk-mainloop/Cargo.toml

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,12 @@ name = "agdk-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]
9-
log = "0.4"
7+
android-activity = { version = "0.5.0-beta.1", features = ["native-activity"] }
108
android_logger = "0.11.0"
11-
android-activity = { version = "0.4", features = ["game-activity"] }
12-
ndk-sys = "0.4"
13-
ndk = "0.7"
9+
log = "0.4"
10+
ndk = "0.8.0-beta.0"
1411

1512
[lib]
16-
name="main"
17-
crate_type=["cdylib"]
13+
name = "main"
14+
crate_type = ["cdylib"]

agdk-mainloop/src/lib.rs

Lines changed: 21 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,16 @@ fn android_main(app: AndroidApp) {
3839
}
3940
MainEvent::InitWindow { .. } => {
4041
native_window = app.native_window();
42+
if let Some(nw) = &native_window {
43+
// Set the backing buffer to a known format (without changing
44+
// the size) so that we can safely draw to it in dummy_render().
45+
nw.set_buffers_geometry(
46+
0,
47+
0,
48+
Some(HardwareBufferFormat::R8G8B8A8_UNORM),
49+
)
50+
.unwrap()
51+
}
4152
redraw_pending = true;
4253
}
4354
MainEvent::TerminateWindow { .. } => {
@@ -91,16 +102,15 @@ fn android_main(app: AndroidApp) {
91102
/// responsive, otherwise it will stop delivering input
92103
/// events to us.
93104
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 _);
105+
let mut lock = native_window.lock(None).unwrap();
106+
let (w, h) = (lock.width(), lock.height());
107+
108+
for (y, line) in lock.lines().unwrap().enumerate() {
109+
let r = y * 255 / h;
110+
for (x, pixels) in line.chunks_mut(4).enumerate() {
111+
let g = x * 255 / w;
112+
pixels[0].write(r as u8);
113+
pixels[1].write(g as u8);
114+
}
105115
}
106116
}

0 commit comments

Comments
 (0)