library/avatar: convert JNI surface to shared jni macros
- Commit
b93846f745a97e952a04bc2d85cd56c035d044a4- Author
- Vayun Mathur <[email protected]>
- When
- 2026-12-01 20:16 UTC (4 days ago)
- Parents
b356d07
Re-measurement of the Phase 5 skip (0 scaffold users counted) with the full macro family. Fresh count of lib.rs (307 lines, 11 JNI methods): 1 pure with_env method (nativeGetPosesJson), 7 direct _env handle ops, and 3 methods that fit neither macro - nativeInit pokes its mut env before a direct body, nativeSetConfig runs prologue/epilogue around its with_env call, and nativeSurfaceCreated reads env directly for ANativeWindow_fromSurface. Those three stay hand-written. Conversion: 1 jni_fn!, 7 jni_direct! (which adds no with_env/LogErrorAndDefault layer the direct methods never had, so exception behaviour is unchanged); bodies verified token-for-token against HEAD by script. Lines: lib.rs 307 -> 246 (-61, -19.9%), clearing the 10% bar; structure_check lines: OK. Cargo.lock gains only the jni_bridge edge for yuu_avatar. The host build needs the SPIR-V shaders, reproduced with the NDK glslc into the gitignored build/spirv directory. Verified: nm -D on libyuu_avatar.so (aarch64-linux-android) before/after - exported symbol set identical (11/11 Java_ YuuAvatarNative symbols); cargo test -p yuu_avatar 90 passed, identical to baseline; cargo fmt --check -p yuu_avatar clean.
Diff
Cargo.lock
diff --git a/Cargo.lock b/Cargo.lockindex 0e6080fbf4..f03f3e6cc1 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -6907,6 +6907,7 @@ dependencies = [ "glow", "gltf", "jni",+ "jni_bridge", "khronos-egl", "serde", "serde_json",
library/avatar/src/main/rust/Cargo.toml
diff --git a/library/avatar/src/main/rust/Cargo.toml b/library/avatar/src/main/rust/Cargo.tomlindex 56bbd3e62d..932e1c7f6f 100644--- a/library/avatar/src/main/rust/Cargo.toml+++ b/library/avatar/src/main/rust/Cargo.toml@@ -11,6 +11,7 @@ crate-type = ["cdylib", "rlib"] [dependencies] jni.workspace = true+jni_bridge = { path = "../../../../../library/jni-bridge/src/main/rust" } ash = { workspace = true, default-features = false, features = ["loaded"] } glam = { workspace = true, features = ["serde"] } serde = { workspace = true, features = ["derive"] }
library/avatar/src/main/rust/src/lib.rs
diff --git a/library/avatar/src/main/rust/src/lib.rs b/library/avatar/src/main/rust/src/lib.rsindex d4ae29384f..13a5735cb9 100644--- a/library/avatar/src/main/rust/src/lib.rs+++ b/library/avatar/src/main/rust/src/lib.rs@@ -18,6 +18,7 @@ pub mod vulkan; use jni::objects::{JClass, JObject, JString}; use jni::sys::{jboolean, jfloat, jint, jlong, jstring}; use jni::{Env, EnvUnowned};+use jni_bridge::{jni_direct, jni_fn}; #[cfg(target_os = "android")] use std::os::raw::c_void as CVoid;@@ -119,56 +120,25 @@ pub extern "system" fn Java_com_vayunmathur_library_avatar_YuuAvatarNative_nativ } } -/// Surface resized: recreate the swapchain at the new size.-#[no_mangle]-pub extern "system" fn Java_com_vayunmathur_library_avatar_YuuAvatarNative_nativeSurfaceChanged<- 'l,->(- _env: EnvUnowned<'l>,- _class: JClass<'l>,- surface_id: jint,- width: jint,- height: jint,-) {+jni_direct! { /// Surface resized: recreate the swapchain at the new size.+Java_com_vayunmathur_library_avatar_YuuAvatarNative_nativeSurfaceChanged; (_class: JClass<'l>, surface_id: jint, width: jint, height: jint) { engine::resize_renderer(surface_id, width, height);-}+} } -/// Surface gone: destroy that surface's renderer, stop its thread.-#[no_mangle]-pub extern "system" fn Java_com_vayunmathur_library_avatar_YuuAvatarNative_nativeSurfaceDestroyed<- 'l,->(- _env: EnvUnowned<'l>,- _class: JClass<'l>,- surface_id: jint,-) {+jni_direct! { /// Surface gone: destroy that surface's renderer, stop its thread.+Java_com_vayunmathur_library_avatar_YuuAvatarNative_nativeSurfaceDestroyed; (_class: JClass<'l>, surface_id: jint) { engine::destroy_renderer(surface_id);-}+} } -/// Pause or resume drawing (keeps the renderer alive).-#[no_mangle]-pub extern "system" fn Java_com_vayunmathur_library_avatar_YuuAvatarNative_nativeSetPaused<'l>(- _env: EnvUnowned<'l>,- _class: JClass<'l>,- surface_id: jint,- paused: jboolean,-) {+jni_direct! { /// Pause or resume drawing (keeps the renderer alive).+Java_com_vayunmathur_library_avatar_YuuAvatarNative_nativeSetPaused; (_class: JClass<'l>, surface_id: jint, paused: jboolean) { engine::set_paused(surface_id, paused != jni::sys::JNI_FALSE);-}+} } -/// Bundled mesh handoff: read `length` bytes at `offset` from the detached+jni_direct! { /// Bundled mesh handoff: read `length` bytes at `offset` from the detached /// APK fd and store them for the next surface creation. The fd is adopted /// (owned) here — always closed exactly once, on every path.-#[no_mangle]-pub extern "system" fn Java_com_vayunmathur_library_avatar_YuuAvatarNative_nativeSetBundledMesh<- 'l,->(- _env: EnvUnowned<'l>,- _class: JClass<'l>,- fd: jint,- offset: jlong,- length: jlong,-) {+Java_com_vayunmathur_library_avatar_YuuAvatarNative_nativeSetBundledMesh; (_class: JClass<'l>, fd: jint, offset: jlong, length: jlong) { let bytes = read_fd(fd, offset, length); // Adopt-then-close: the fd is ours now regardless of read outcome. close_fd(fd);@@ -177,7 +147,7 @@ pub extern "system" fn Java_com_vayunmathur_library_avatar_YuuAvatarNative_nativ engine::set_bundled_mesh(bytes); } }-}+} } #[cfg(target_os = "android")] fn read_fd(fd: jint, offset: jlong, length: jlong) -> Option<Vec<u8>> {@@ -223,61 +193,30 @@ fn poses_json() -> String { #[cfg(test)] mod lib_tests; -/// Pose preset ids as JSON (`["idle","wave",...]`) for the gallery.-#[no_mangle]-pub extern "system" fn Java_com_vayunmathur_library_avatar_YuuAvatarNative_nativeGetPosesJson<- 'l,->(- mut env: EnvUnowned<'l>,- _class: JClass<'l>,-) -> jstring {- let json = poses_json();- env.with_env(|env| -> jni::errors::Result<jstring> {+jni_fn! { /// Pose preset ids as JSON (`["idle","wave",...]`) for the gallery.+Java_com_vayunmathur_library_avatar_YuuAvatarNative_nativeGetPosesJson -> jstring; (_class: JClass<'l>) |env| { let null = std::ptr::null_mut(); match env.new_string(json) { Ok(s) => Ok(s.into_raw()), Err(_) => Ok(null), }- })- .resolve::<jni::errors::LogErrorAndDefault>()-}+ } } -/// User drag-rotate delta for a surface (radians). Accumulates into the+jni_direct! { /// User drag-rotate delta for a surface (radians). Accumulates into the /// surface's yaw/pitch; pitch clamps native-side.-#[no_mangle]-pub extern "system" fn Java_com_vayunmathur_library_avatar_YuuAvatarNative_nativeOnRotateInput<- 'l,->(- _env: EnvUnowned<'l>,- _class: JClass<'l>,- surface_id: jint,- yaw_delta: jfloat,- pitch_delta: jfloat,-) {+Java_com_vayunmathur_library_avatar_YuuAvatarNative_nativeOnRotateInput; (_class: JClass<'l>, surface_id: jint, yaw_delta: jfloat, pitch_delta: jfloat) { engine::on_rotate_input(surface_id, yaw_delta, pitch_delta);-}+} } -/// Set whether a surface auto-orbits (editor disables on creation).-#[no_mangle]-pub extern "system" fn Java_com_vayunmathur_library_avatar_YuuAvatarNative_nativeSetAutoRotate<- 'l,->(- _env: EnvUnowned<'l>,- _class: JClass<'l>,- surface_id: jint,- auto_rotate: jboolean,-) {+jni_direct! { /// Set whether a surface auto-orbits (editor disables on creation).+Java_com_vayunmathur_library_avatar_YuuAvatarNative_nativeSetAutoRotate; (_class: JClass<'l>, surface_id: jint, auto_rotate: jboolean) { engine::set_auto_rotate(surface_id, auto_rotate != jni::sys::JNI_FALSE);-}+} } -/// Activity gone: destroy everything.-#[no_mangle]-pub extern "system" fn Java_com_vayunmathur_library_avatar_YuuAvatarNative_nativeOnDestroy<'l>(- _env: EnvUnowned<'l>,- _class: JClass<'l>,-) {+jni_direct! { /// Activity gone: destroy everything.+Java_com_vayunmathur_library_avatar_YuuAvatarNative_nativeOnDestroy; (_class: JClass<'l>) { engine::destroy_engine();-}+} } #[cfg(test)] mod tests {