Skip to content

Commit 110ad0b

Browse files
committed
Use new NativeWindow::lock() API in dummy_render()
1 parent 7b42ef8 commit 110ad0b

File tree

3 files changed

+64
-35
lines changed

3 files changed

+64
-35
lines changed

agdk-mainloop/src/lib.rs

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use jni::{
99
refs::Global,
1010
vm::JavaVM,
1111
};
12+
use ndk::native_window::HardwareBufferFormat;
1213
use tracing::{error, info};
1314

1415
jni::bind_java_type! { Context => "android.content.Context" }
@@ -176,6 +177,16 @@ fn android_main(app: AndroidApp) {
176177
}
177178
MainEvent::InitWindow { .. } => {
178179
native_window = app.native_window();
180+
if let Some(nw) = &native_window {
181+
// Set the backing buffer to a known format (without changing
182+
// the size) so that we can safely draw to it in dummy_render().
183+
nw.set_buffers_geometry(
184+
0,
185+
0,
186+
Some(HardwareBufferFormat::R8G8B8A8_UNORM),
187+
)
188+
.unwrap()
189+
}
179190
redraw_pending = true;
180191
}
181192
MainEvent::TerminateWindow { .. } => {
@@ -401,16 +412,15 @@ fn character_map_and_combine_key(
401412
/// responsive, otherwise it will stop delivering input
402413
/// events to us.
403414
fn dummy_render(native_window: &ndk::native_window::NativeWindow) {
404-
unsafe {
405-
let mut buf: ndk_sys::ANativeWindow_Buffer = std::mem::zeroed();
406-
let mut rect: ndk_sys::ARect = std::mem::zeroed();
407-
ndk_sys::ANativeWindow_lock(
408-
native_window.ptr().as_ptr() as _,
409-
&mut buf as _,
410-
&mut rect as _,
411-
);
412-
// Note: we don't try and touch the buffer since that
413-
// also requires us to handle various buffer formats
414-
ndk_sys::ANativeWindow_unlockAndPost(native_window.ptr().as_ptr() as _);
415+
let mut lock = native_window.lock(None).unwrap();
416+
let (w, h) = (lock.width(), lock.height());
417+
418+
for (y, line) in lock.lines().unwrap().enumerate() {
419+
let r = y * 255 / h;
420+
for (x, pixels) in line.chunks_mut(4).enumerate() {
421+
let g = x * 255 / w;
422+
pixels[0].write(r as u8);
423+
pixels[1].write(g as u8);
424+
}
415425
}
416426
}

na-mainloop/src/lib.rs

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
use std::sync::OnceLock;
22

33
use android_activity::{
4-
AndroidApp, InputStatus, MainEvent, OnCreateState, PollEvent,
54
input::{InputEvent, KeyAction, KeyEvent, KeyMapChar, MotionAction},
6-
ndk, ndk_sys,
5+
ndk, ndk_sys, AndroidApp, InputStatus, MainEvent, OnCreateState, PollEvent,
76
};
87
use jni::{
98
objects::{JObject, JString},
109
refs::Global,
1110
vm::JavaVM,
1211
};
12+
use ndk::native_window::HardwareBufferFormat;
1313
use tracing::{error, info};
1414

1515
jni::bind_java_type! { Context => "android.content.Context" }
@@ -177,6 +177,16 @@ fn android_main(app: AndroidApp) {
177177
}
178178
MainEvent::InitWindow { .. } => {
179179
native_window = app.native_window();
180+
if let Some(nw) = &native_window {
181+
// Set the backing buffer to a known format (without changing
182+
// the size) so that we can safely draw to it in dummy_render().
183+
nw.set_buffers_geometry(
184+
0,
185+
0,
186+
Some(HardwareBufferFormat::R8G8B8A8_UNORM),
187+
)
188+
.unwrap()
189+
}
180190
redraw_pending = true;
181191
}
182192
MainEvent::TerminateWindow { .. } => {
@@ -402,16 +412,15 @@ fn character_map_and_combine_key(
402412
/// responsive, otherwise it will stop delivering input
403413
/// events to us.
404414
fn dummy_render(native_window: &ndk::native_window::NativeWindow) {
405-
unsafe {
406-
let mut buf: ndk_sys::ANativeWindow_Buffer = std::mem::zeroed();
407-
let mut rect: ndk_sys::ARect = std::mem::zeroed();
408-
ndk_sys::ANativeWindow_lock(
409-
native_window.ptr().as_ptr() as _,
410-
&mut buf as _,
411-
&mut rect as _,
412-
);
413-
// Note: we don't try and touch the buffer since that
414-
// also requires us to handle various buffer formats
415-
ndk_sys::ANativeWindow_unlockAndPost(native_window.ptr().as_ptr() as _);
415+
let mut lock = native_window.lock(None).unwrap();
416+
let (w, h) = (lock.width(), lock.height());
417+
418+
for (y, line) in lock.lines().unwrap().enumerate() {
419+
let r = y * 255 / h;
420+
for (x, pixels) in line.chunks_mut(4).enumerate() {
421+
let g = x * 255 / w;
422+
pixels[0].write(r as u8);
423+
pixels[1].write(g as u8);
424+
}
416425
}
417426
}

na-subclass-jni/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
#[unsafe(no_mangle)]
56
fn android_main(app: AndroidApp) {
@@ -40,6 +41,16 @@ fn android_main(app: AndroidApp) {
4041
}
4142
MainEvent::InitWindow { .. } => {
4243
native_window = app.native_window();
44+
if let Some(nw) = &native_window {
45+
// Set the backing buffer to a known format (without changing
46+
// the size) so that we can safely draw to it in dummy_render().
47+
nw.set_buffers_geometry(
48+
0,
49+
0,
50+
Some(HardwareBufferFormat::R8G8B8A8_UNORM),
51+
)
52+
.unwrap()
53+
}
4354
redraw_pending = true;
4455
}
4556
MainEvent::TerminateWindow { .. } => {
@@ -101,17 +112,16 @@ fn android_main(app: AndroidApp) {
101112
/// responsive, otherwise it will stop delivering input
102113
/// events to us.
103114
fn dummy_render(native_window: &ndk::native_window::NativeWindow) {
104-
unsafe {
105-
let mut buf: ndk_sys::ANativeWindow_Buffer = std::mem::zeroed();
106-
let mut rect: ndk_sys::ARect = std::mem::zeroed();
107-
ndk_sys::ANativeWindow_lock(
108-
native_window.ptr().as_ptr() as _,
109-
&mut buf as _,
110-
&mut rect as _,
111-
);
112-
// Note: we don't try and touch the buffer since that
113-
// also requires us to handle various buffer formats
114-
ndk_sys::ANativeWindow_unlockAndPost(native_window.ptr().as_ptr() as _);
115+
let mut lock = native_window.lock(None).unwrap();
116+
let (w, h) = (lock.width(), lock.height());
117+
118+
for (y, line) in lock.lines().unwrap().enumerate() {
119+
let r = y * 255 / h;
120+
for (x, pixels) in line.chunks_mut(4).enumerate() {
121+
let g = x * 255 / w;
122+
pixels[0].write(r as u8);
123+
pixels[1].write(g as u8);
124+
}
115125
}
116126
}
117127

0 commit comments

Comments
 (0)