Skip to content

Commit 784b3bc

Browse files
committed
Use new NativeWindow::lock() API in dummy_render()
1 parent 4ae1ff5 commit 784b3bc

File tree

15 files changed

+66
-62
lines changed

15 files changed

+66
-62
lines changed

agdk-cpal/Cargo.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ name = "agdk-cpal"
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
tracing = "0.1"
108
tracing-subscriber = { version = "0.3", features = [

agdk-eframe/Cargo.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ version = "0.1.0"
44
edition = "2021"
55
resolver = "2"
66

7-
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
8-
97
[dependencies]
108
tracing = "0.1"
119
tracing-subscriber = { version = "0.3", features = [

agdk-egui/Cargo.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ version = "0.1.0"
44
edition = "2021"
55
resolver = "2"
66

7-
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
8-
97
[dependencies]
108
tracing = "0.1"
119
tracing-subscriber = { version = "0.3", features = [

agdk-mainloop/Cargo.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ 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]
97
tracing = "0.1"
108
tracing-subscriber = { version = "0.3", features = [

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
}

agdk-oboe/Cargo.toml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ name = "agdk-oboe"
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.13.1"
@@ -14,4 +12,4 @@ atomic_float = "0.1"
1412

1513
[lib]
1614
name="main"
17-
crate_type=["cdylib"]
15+
crate_type=["cdylib"]

agdk-winit-wgpu/Cargo.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ name = "agdk-winit-wgpu"
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
tracing = "0.1"
108
tracing-subscriber = { version = "0.3", features = [

na-egui/Cargo.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ version = "0.1.0"
44
edition = "2021"
55
resolver = "2"
66

7-
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
8-
97
[dependencies]
108
tracing = "0.1"
119
tracing-subscriber = { version = "0.3", features = [

na-mainloop/Cargo.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ 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"

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
}

0 commit comments

Comments
 (0)