photos: convert JNI surface to shared jni_fn! macro
- Commit
7fef409853c46214e3f043a51d71b44ad5d2643e- Author
- Vayun Mathur <[email protected]>
- When
- 2026-12-01 19:33 UTC (4 days ago)
- Parents
04a336f
LOC Phase 5 rollout. All 10 photos_fx JNI functions (six in lib.rs, four in-place filters in jni_inplace.rs) repeated the EnvUnowned / with_env / LogErrorAndDefault scaffold and convert to jni_bridge::jni_fn!. Bodies are verbatim (verified token-for-token, including the catch_unwind wrappers); handles and null checks stay in the bodies, and the now-unused EnvUnowned imports are dropped. Lines: the two JNI files go 657 -> 521 (-136, -20.7%); structure_check photos module 48,974 after, structure: OK. Cargo.lock gains only the jni_bridge edge for photos_fx. Verified: nm -D on libphotos_fx.so before/after - exported symbol set identical (10/10 Java_ PhotosNative symbols); cargo test -p photos_fx: 69 passed; JVM bridge tests :photos:testDevUnitTestNative AdjustmentPixelsEqualityTest: 4/4 passed (run excludes the pre-existing rustFileLength ratchet); cargo fmt --check -p photos_fx clean.
Diff
Cargo.lock
diff --git a/Cargo.lock b/Cargo.lockindex 9269411764..69d936faeb 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -4433,6 +4433,7 @@ name = "photos_fx" version = "0.1.0" dependencies = [ "jni",+ "jni_bridge", ] [[package]]
photos/src/main/rust/Cargo.toml
diff --git a/photos/src/main/rust/Cargo.toml b/photos/src/main/rust/Cargo.tomlindex 28755d332f..0dbcfdf0c9 100644--- a/photos/src/main/rust/Cargo.toml+++ b/photos/src/main/rust/Cargo.toml@@ -12,5 +12,6 @@ crate-type = ["cdylib", "rlib"] [dependencies] # jni local fork – no walkdir/windows build deps (saves 11 crates) jni.workspace = true+jni_bridge = { path = "../../../../library/jni-bridge/src/main/rust" } # rayon removed – was 6 crates (crossbeam-deque/epoch/utils, either, rayon-core + rayon) # Replaced by std::thread::scope in blur.rs / stylize.rs preserving parallelism
photos/src/main/rust/src/jni_inplace.rs
diff --git a/photos/src/main/rust/src/jni_inplace.rs b/photos/src/main/rust/src/jni_inplace.rsindex 179e093b28..ddbacd03f1 100644--- a/photos/src/main/rust/src/jni_inplace.rs+++ b/photos/src/main/rust/src/jni_inplace.rs@@ -7,7 +7,8 @@ use crate::{blur, sharpen, stylize}; use jni::objects::{JClass, JIntArray}; use jni::sys::jint;-use jni::{Env, EnvUnowned};+use jni::Env;+use jni_bridge::jni_fn; /// Read a Java `int[]` into a `Vec<i32>`; `None` on error. fn read_ints(env: &Env, arr: &JIntArray) -> Option<Vec<i32>> {@@ -26,137 +27,81 @@ fn write_into(env: &Env, pixels: &JIntArray, result: &[i32]) -> bool { env.set_int_array_region(pixels, 0, result).is_ok() } -#[no_mangle]-pub extern "system" fn Java_com_vayunmathur_photos_platform_PhotosNative_stylizeInPlace<'l>(- mut env: EnvUnowned<'l>,- _class: JClass<'l>,- pixels: JIntArray<'l>,- w: jint,- h: jint,- mode: jint,-) -> jni::sys::jboolean {- env.with_env(|env| -> jni::errors::Result<jni::sys::jboolean> {- if w <= 0 || h <= 0 {- return Ok(false);- }- let buf = match read_ints(env, &pixels) {- Some(b) if b.len() == (w as usize) * (h as usize) => b,- _ => return Ok(false),- };- let mut ok = false;- if let Ok(result) = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {- stylize::stylize(&buf, w as usize, h as usize, mode)- })) {- if write_into(env, &pixels, &result) {- ok = true;- }+jni_fn! { Java_com_vayunmathur_photos_platform_PhotosNative_stylizeInPlace -> jni::sys::jboolean; (_class: JClass<'l>, pixels: JIntArray<'l>, w: jint, h: jint, mode: jint) |env| {+ if w <= 0 || h <= 0 {+ return Ok(false);+ }+ let buf = match read_ints(env, &pixels) {+ Some(b) if b.len() == (w as usize) * (h as usize) => b,+ _ => return Ok(false),+ };+ let mut ok = false;+ if let Ok(result) = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {+ stylize::stylize(&buf, w as usize, h as usize, mode)+ })) {+ if write_into(env, &pixels, &result) {+ ok = true; }- Ok(ok)- })- .resolve::<jni::errors::LogErrorAndDefault>()-}+ }+ Ok(ok)+} } -#[no_mangle]-pub extern "system" fn Java_com_vayunmathur_photos_platform_PhotosNative_unsharpInPlace<'l>(- mut env: EnvUnowned<'l>,- _class: JClass<'l>,- pixels: JIntArray<'l>,- w: jint,- h: jint,- amount: jni::sys::jfloat,- radius: jni::sys::jfloat,- threshold: jint,-) -> jni::sys::jboolean {- env.with_env(|env| -> jni::errors::Result<jni::sys::jboolean> {- if w <= 0 || h <= 0 {- return Ok(false);- }- let buf = match read_ints(env, &pixels) {- Some(b) if b.len() == (w as usize) * (h as usize) => b,- _ => return Ok(false),- };- let mut ok = false;- if let Ok(result) = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {- sharpen::unsharp(&buf, w as usize, h as usize, amount, radius, threshold)- })) {- if write_into(env, &pixels, &result) {- ok = true;- }+jni_fn! { Java_com_vayunmathur_photos_platform_PhotosNative_unsharpInPlace -> jni::sys::jboolean; (_class: JClass<'l>, pixels: JIntArray<'l>, w: jint, h: jint, amount: jni::sys::jfloat, radius: jni::sys::jfloat, threshold: jint) |env| {+ if w <= 0 || h <= 0 {+ return Ok(false);+ }+ let buf = match read_ints(env, &pixels) {+ Some(b) if b.len() == (w as usize) * (h as usize) => b,+ _ => return Ok(false),+ };+ let mut ok = false;+ if let Ok(result) = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {+ sharpen::unsharp(&buf, w as usize, h as usize, amount, radius, threshold)+ })) {+ if write_into(env, &pixels, &result) {+ ok = true; }- Ok(ok)- })- .resolve::<jni::errors::LogErrorAndDefault>()-}+ }+ Ok(ok)+} } -#[no_mangle]-pub extern "system" fn Java_com_vayunmathur_photos_platform_PhotosNative_blurParamsInPlace<'l>(- mut env: EnvUnowned<'l>,- _class: JClass<'l>,- pixels: JIntArray<'l>,- w: jint,- h: jint,- mode: jint,- center_x: jni::sys::jfloat,- center_y: jni::sys::jfloat,- radius: jni::sys::jfloat,- intensity: jni::sys::jfloat,- feather: jni::sys::jfloat,- angle: jni::sys::jfloat,-) -> jni::sys::jboolean {- env.with_env(|env| -> jni::errors::Result<jni::sys::jboolean> {- if w <= 0 || h <= 0 {- return Ok(false);- }- let buf = match read_ints(env, &pixels) {- Some(b) if b.len() == (w as usize) * (h as usize) => b,- _ => return Ok(false),- };- let mut ok = false;- if let Ok(result) = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {- blur::blur_params(- &buf, w as usize, h as usize, mode, center_x, center_y, radius, intensity, feather,- angle,- )- })) {- if write_into(env, &pixels, &result) {- ok = true;- }+jni_fn! { Java_com_vayunmathur_photos_platform_PhotosNative_blurParamsInPlace -> jni::sys::jboolean; (_class: JClass<'l>, pixels: JIntArray<'l>, w: jint, h: jint, mode: jint, center_x: jni::sys::jfloat, center_y: jni::sys::jfloat, radius: jni::sys::jfloat, intensity: jni::sys::jfloat, feather: jni::sys::jfloat, angle: jni::sys::jfloat) |env| {+ if w <= 0 || h <= 0 {+ return Ok(false);+ }+ let buf = match read_ints(env, &pixels) {+ Some(b) if b.len() == (w as usize) * (h as usize) => b,+ _ => return Ok(false),+ };+ let mut ok = false;+ if let Ok(result) = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {+ blur::blur_params(+ &buf, w as usize, h as usize, mode, center_x, center_y, radius, intensity, feather,+ angle,+ )+ })) {+ if write_into(env, &pixels, &result) {+ ok = true; }- Ok(ok)- })- .resolve::<jni::errors::LogErrorAndDefault>()-}+ }+ Ok(ok)+} } -#[no_mangle]-pub extern "system" fn Java_com_vayunmathur_photos_platform_PhotosNative_filterBlurInPlace<'l>(- mut env: EnvUnowned<'l>,- _class: JClass<'l>,- pixels: JIntArray<'l>,- w: jint,- h: jint,- mode: jint,- amount: jni::sys::jfloat,- angle: jni::sys::jfloat,- center_x: jni::sys::jfloat,- center_y: jni::sys::jfloat,-) -> jni::sys::jboolean {- env.with_env(|env| -> jni::errors::Result<jni::sys::jboolean> {- if w <= 0 || h <= 0 {- return Ok(false);- }- let buf = match read_ints(env, &pixels) {- Some(b) if b.len() == (w as usize) * (h as usize) => b,- _ => return Ok(false),- };- let mut ok = false;- if let Ok(result) = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {- blur::filter_blur(&buf, w as usize, h as usize, mode, amount, angle, center_x, center_y)- })) {- if write_into(env, &pixels, &result) {- ok = true;- }+jni_fn! { Java_com_vayunmathur_photos_platform_PhotosNative_filterBlurInPlace -> jni::sys::jboolean; (_class: JClass<'l>, pixels: JIntArray<'l>, w: jint, h: jint, mode: jint, amount: jni::sys::jfloat, angle: jni::sys::jfloat, center_x: jni::sys::jfloat, center_y: jni::sys::jfloat) |env| {+ if w <= 0 || h <= 0 {+ return Ok(false);+ }+ let buf = match read_ints(env, &pixels) {+ Some(b) if b.len() == (w as usize) * (h as usize) => b,+ _ => return Ok(false),+ };+ let mut ok = false;+ if let Ok(result) = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {+ blur::filter_blur(&buf, w as usize, h as usize, mode, amount, angle, center_x, center_y)+ })) {+ if write_into(env, &pixels, &result) {+ ok = true; }- Ok(ok)- })- .resolve::<jni::errors::LogErrorAndDefault>()-}+ }+ Ok(ok)+} }
photos/src/main/rust/src/lib.rs
diff --git a/photos/src/main/rust/src/lib.rs b/photos/src/main/rust/src/lib.rsindex 151f744fee..5a17a1811e 100644--- a/photos/src/main/rust/src/lib.rs+++ b/photos/src/main/rust/src/lib.rs@@ -33,7 +33,8 @@ mod jni_bindings { use crate::{blur, inpaint, liquify, sharpen, stylize}; use jni::objects::{JClass, JFloatArray, JIntArray}; use jni::sys::{jint, jintArray};- use jni::{Env, EnvUnowned};+ use jni::Env;+ use jni_bridge::jni_fn; /// Read a Java `int[]` into a `Vec<i32>`; `None` on error. fn read_ints(env: &Env, arr: &JIntArray) -> Option<Vec<i32>> {@@ -67,16 +68,7 @@ mod jni_bindings { out.into_raw() } - #[no_mangle]- pub extern "system" fn Java_com_vayunmathur_photos_platform_PhotosNative_stylize<'l>(- mut env: EnvUnowned<'l>,- _class: JClass<'l>,- pixels: JIntArray<'l>,- w: jint,- h: jint,- mode: jint,- ) -> jintArray {- env.with_env(|env| -> jni::errors::Result<jintArray> {+ jni_fn! { Java_com_vayunmathur_photos_platform_PhotosNative_stylize -> jintArray; (_class: JClass<'l>, pixels: JIntArray<'l>, w: jint, h: jint, mode: jint) |env| { let null = std::ptr::null_mut(); if w <= 0 || h <= 0 { return Ok(null);@@ -92,22 +84,9 @@ mod jni_bindings { Err(_) => return Ok(null), }; Ok(write_ints(env, &result))- })- .resolve::<jni::errors::LogErrorAndDefault>()- }+ } } - #[no_mangle]- pub extern "system" fn Java_com_vayunmathur_photos_platform_PhotosNative_unsharp<'l>(- mut env: EnvUnowned<'l>,- _class: JClass<'l>,- pixels: JIntArray<'l>,- w: jint,- h: jint,- amount: jni::sys::jfloat,- radius: jni::sys::jfloat,- threshold: jint,- ) -> jintArray {- env.with_env(|env| -> jni::errors::Result<jintArray> {+ jni_fn! { Java_com_vayunmathur_photos_platform_PhotosNative_unsharp -> jintArray; (_class: JClass<'l>, pixels: JIntArray<'l>, w: jint, h: jint, amount: jni::sys::jfloat, radius: jni::sys::jfloat, threshold: jint) |env| { let null = std::ptr::null_mut(); if w <= 0 || h <= 0 { return Ok(null);@@ -123,21 +102,9 @@ mod jni_bindings { Err(_) => return Ok(null), }; Ok(write_ints(env, &result))- })- .resolve::<jni::errors::LogErrorAndDefault>()- }+ } } - #[no_mangle]- pub extern "system" fn Java_com_vayunmathur_photos_platform_PhotosNative_liquify<'l>(- mut env: EnvUnowned<'l>,- _class: JClass<'l>,- pixels: JIntArray<'l>,- w: jint,- h: jint,- tools: JIntArray<'l>,- params: JFloatArray<'l>,- ) -> jintArray {- env.with_env(|env| -> jni::errors::Result<jintArray> {+ jni_fn! { Java_com_vayunmathur_photos_platform_PhotosNative_liquify -> jintArray; (_class: JClass<'l>, pixels: JIntArray<'l>, w: jint, h: jint, tools: JIntArray<'l>, params: JFloatArray<'l>) |env| { let null = std::ptr::null_mut(); if w <= 0 || h <= 0 { return Ok(null);@@ -161,26 +128,9 @@ mod jni_bindings { Err(_) => return Ok(null), }; Ok(write_ints(env, &result))- })- .resolve::<jni::errors::LogErrorAndDefault>()- }+ } } - #[no_mangle]- pub extern "system" fn Java_com_vayunmathur_photos_platform_PhotosNative_blurParams<'l>(- mut env: EnvUnowned<'l>,- _class: JClass<'l>,- pixels: JIntArray<'l>,- w: jint,- h: jint,- mode: jint,- center_x: jni::sys::jfloat,- center_y: jni::sys::jfloat,- radius: jni::sys::jfloat,- intensity: jni::sys::jfloat,- feather: jni::sys::jfloat,- angle: jni::sys::jfloat,- ) -> jintArray {- env.with_env(|env| -> jni::errors::Result<jintArray> {+ jni_fn! { Java_com_vayunmathur_photos_platform_PhotosNative_blurParams -> jintArray; (_class: JClass<'l>, pixels: JIntArray<'l>, w: jint, h: jint, mode: jint, center_x: jni::sys::jfloat, center_y: jni::sys::jfloat, radius: jni::sys::jfloat, intensity: jni::sys::jfloat, feather: jni::sys::jfloat, angle: jni::sys::jfloat) |env| { let null = std::ptr::null_mut(); if w <= 0 || h <= 0 { return Ok(null);@@ -199,24 +149,9 @@ mod jni_bindings { Err(_) => return Ok(null), }; Ok(write_ints(env, &result))- })- .resolve::<jni::errors::LogErrorAndDefault>()- }+ } } - #[no_mangle]- pub extern "system" fn Java_com_vayunmathur_photos_platform_PhotosNative_filterBlur<'l>(- mut env: EnvUnowned<'l>,- _class: JClass<'l>,- pixels: JIntArray<'l>,- w: jint,- h: jint,- mode: jint,- amount: jni::sys::jfloat,- angle: jni::sys::jfloat,- center_x: jni::sys::jfloat,- center_y: jni::sys::jfloat,- ) -> jintArray {- env.with_env(|env| -> jni::errors::Result<jintArray> {+ jni_fn! { Java_com_vayunmathur_photos_platform_PhotosNative_filterBlur -> jintArray; (_class: JClass<'l>, pixels: JIntArray<'l>, w: jint, h: jint, mode: jint, amount: jni::sys::jfloat, angle: jni::sys::jfloat, center_x: jni::sys::jfloat, center_y: jni::sys::jfloat) |env| { let null = std::ptr::null_mut(); if w <= 0 || h <= 0 { return Ok(null);@@ -234,23 +169,9 @@ mod jni_bindings { Err(_) => return Ok(null), }; Ok(write_ints(env, &result))- })- .resolve::<jni::errors::LogErrorAndDefault>()- }+ } } - #[no_mangle]- pub extern "system" fn Java_com_vayunmathur_photos_platform_PhotosNative_inpaint<'l>(- mut env: EnvUnowned<'l>,- _class: JClass<'l>,- pixels: JIntArray<'l>,- w: jint,- h: jint,- hole_mask: JFloatArray<'l>,- mask_w: jint,- mask_h: jint,- passes: jint,- ) -> jintArray {- env.with_env(|env| -> jni::errors::Result<jintArray> {+ jni_fn! { Java_com_vayunmathur_photos_platform_PhotosNative_inpaint -> jintArray; (_class: JClass<'l>, pixels: JIntArray<'l>, w: jint, h: jint, hole_mask: JFloatArray<'l>, mask_w: jint, mask_h: jint, passes: jint) |env| { let null = std::ptr::null_mut(); if w <= 0 || h <= 0 || mask_w <= 0 || mask_h <= 0 { return Ok(null);@@ -278,9 +199,7 @@ mod jni_bindings { Err(_) => return Ok(null), }; Ok(write_ints(env, &result))- })- .resolve::<jni::errors::LogErrorAndDefault>()- }+ } } } #[cfg(test)]