Modern Apps git

← Modern-Apps.git

games/kart: convert JNI surface to shared jni macros

Commit
b356d07ffe1672b8f3533f66c623a2bdcd7a9b70
Author
Vayun Mathur <[email protected]>
When
2026-12-01 20:16 UTC (4 days ago)
Parents
247ba26
Re-measurement of the Phase 5 measured skip (-4.1%,
jni_fn!-only) with the full macro family. Fresh count of
lib.rs (219 lines, 10 JNI methods): 1 pure with_env method
(nativeGetStandingsJson), 6 direct _env handle ops, and 3
methods that fit neither macro - nativeInit pokes its
mut env before a direct body, nativeStartRace runs
prologue/epilogue around its with_env call, and
nativeSurfaceCreated reads env directly for
ANativeWindow_fromSurface. Those three stay hand-written;
moving any of them would restructure the body.

Conversion: 1 jni_fn!, 6 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 219 -> 176 (-43, -19.6%), clearing the
10% bar; structure_check lines reports games/kart module
counted in the tree total, structure: OK. Cargo.lock
gains only the jni_bridge edge for kart_engine. The host
build needs the SPIR-V shaders, reproduced with the NDK
glslc into the gitignored build/spirv directory.

Verified: nm -D on libkart_engine.so
(aarch64-linux-android) before/after - exported symbol
set identical (10/10 Java_ KartNative symbols); cargo
test -p kart_engine 129 passed, identical to baseline
(the global-lifecycle coverage test is order-flaky and
fails intermittently on the unconverted baseline too);
cargo fmt --check -p kart_engine clean.

Diff

Cargo.lock
diff --git a/Cargo.lock b/Cargo.lockindex 846c1d5ccf..0e6080fbf4 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -3021,6 +3021,7 @@ dependencies = [  "glam",  "glow",  "jni",+ "jni_bridge",  "khronos-egl",  "serde",  "serde_json",
games/kart/src/main/rust/Cargo.toml
diff --git a/games/kart/src/main/rust/Cargo.toml b/games/kart/src/main/rust/Cargo.tomlindex 6d89baa8a6..fb55ce482c 100644--- a/games/kart/src/main/rust/Cargo.toml+++ b/games/kart/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"] }
games/kart/src/main/rust/src/lib.rs
diff --git a/games/kart/src/main/rust/src/lib.rs b/games/kart/src/main/rust/src/lib.rsindex bd2b6824c6..08abd47848 100644--- a/games/kart/src/main/rust/src/lib.rs+++ b/games/kart/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, jstring}; use jni::{Env, EnvUnowned};+use jni_bridge::{jni_direct, jni_fn};  #[cfg(target_os = "android")] use std::os::raw::c_void as CVoid;@@ -66,64 +67,36 @@ pub extern "system" fn Java_com_vayunmathur_games_kart_util_KartNative_nativeSta     engine::start_race(ids); } -/// One player's drive sample for this tick (throttle/steering/brake+jni_direct! { /// One player's drive sample for this tick (throttle/steering/brake /// normalized; drift and item arrive via their own setters and merge).-#[no_mangle]-pub extern "system" fn Java_com_vayunmathur_games_kart_util_KartNative_nativeOnDriveInput<'l>(-    _env: EnvUnowned<'l>,-    _class: JClass<'l>,-    player: jint,-    throttle: jfloat,-    steering: jfloat,-    brake: jfloat,-) {+Java_com_vayunmathur_games_kart_util_KartNative_nativeOnDriveInput; (_class: JClass<'l>, player: jint, throttle: jfloat, steering: jfloat, brake: jfloat) {     if player >= 0 {         engine::on_drive_input(player as usize, throttle, steering, brake);     }-}+} } -/// Drift shoulder held (sticky until the tick consumes the frame).-#[no_mangle]-pub extern "system" fn Java_com_vayunmathur_games_kart_util_KartNative_nativeSetDrift<'l>(-    _env: EnvUnowned<'l>,-    _class: JClass<'l>,-    player: jint,-    held: jboolean,-) {+jni_direct! { /// Drift shoulder held (sticky until the tick consumes the frame).+Java_com_vayunmathur_games_kart_util_KartNative_nativeSetDrift; (_class: JClass<'l>, player: jint, held: jboolean) {     if player >= 0 {         engine::set_drift(player as usize, held != jni::sys::JNI_FALSE);     }-}+} } -/// Fire the held item (edge-triggered).-#[no_mangle]-pub extern "system" fn Java_com_vayunmathur_games_kart_util_KartNative_nativeUseItem<'l>(-    _env: EnvUnowned<'l>,-    _class: JClass<'l>,-    player: jint,-) {+jni_direct! { /// Fire the held item (edge-triggered).+Java_com_vayunmathur_games_kart_util_KartNative_nativeUseItem; (_class: JClass<'l>, player: jint) {     if player >= 0 {         engine::use_item(player as usize);     }-}+} } -/// Snapshot for the HUD/standings: JSON `[{player,lap,progress,place}]`.-#[no_mangle]-pub extern "system" fn Java_com_vayunmathur_games_kart_util_KartNative_nativeGetStandingsJson<-    'l,->(-    mut env: EnvUnowned<'l>,-    _class: JClass<'l>,-) -> jstring {-    env.with_env(|env| -> jni::errors::Result<jstring> {+jni_fn! { /// Snapshot for the HUD/standings: JSON `[{player,lap,progress,place}]`.+Java_com_vayunmathur_games_kart_util_KartNative_nativeGetStandingsJson -> jstring; (_class: JClass<'l>) |env| {         let null = std::ptr::null_mut();         match env.new_string(engine::standings_json()) {             Ok(s) => Ok(s.into_raw()),             Err(_) => Ok(null),         }-    })-    .resolve::<jni::errors::LogErrorAndDefault>()-}+    } }  /// Surface ready: adopt the window, build the renderer, start the 60 Hz /// render thread. A second call (rotation) rebuilds against the new surface.@@ -172,36 +145,20 @@ pub extern "system" fn Java_com_vayunmathur_games_kart_util_KartNative_nativeSur     } } -/// Surface resized: recreate the swapchain at the new size.-#[no_mangle]-pub extern "system" fn Java_com_vayunmathur_games_kart_util_KartNative_nativeSurfaceChanged<'l>(-    _env: EnvUnowned<'l>,-    _class: JClass<'l>,-    width: jint,-    height: jint,-) {+jni_direct! { /// Surface resized: recreate the swapchain at the new size.+Java_com_vayunmathur_games_kart_util_KartNative_nativeSurfaceChanged; (_class: JClass<'l>, width: jint, height: jint) {     engine::resize_renderer(width, height);-}+} } -/// Surface gone: destroy the renderer, stop the thread. The race survives.-#[no_mangle]-pub extern "system" fn Java_com_vayunmathur_games_kart_util_KartNative_nativeSurfaceDestroyed<-    'l,->(-    _env: EnvUnowned<'l>,-    _class: JClass<'l>,-) {+jni_direct! { /// Surface gone: destroy the renderer, stop the thread. The race survives.+Java_com_vayunmathur_games_kart_util_KartNative_nativeSurfaceDestroyed; (_class: JClass<'l>) {     engine::destroy_renderer();-}+} } -/// Activity gone: destroy everything.-#[no_mangle]-pub extern "system" fn Java_com_vayunmathur_games_kart_util_KartNative_nativeOnDestroy<'l>(-    _env: EnvUnowned<'l>,-    _class: JClass<'l>,-) {+jni_direct! { /// Activity gone: destroy everything.+Java_com_vayunmathur_games_kart_util_KartNative_nativeOnDestroy; (_class: JClass<'l>) {     engine::destroy_engine();-}+} }  #[cfg(test)] mod tests {