High performance self-hosted photo and video management solution.
TypeScript backup-toolfluttergoogle-photosgoogle-photos-alternativejavascriptmobile-appnestjsnodejsphoto-galleryphotosphotos-managementself-hostedsveltesveltekittypescriptvideos
chore(mobile): move the android byte buffer into the native core (#31361)
Santo Shakil4c51bfed1d4b
diff --git a/mobile/android/app/CMakeLists.txt b/mobile/android/app/CMakeLists.txtindex 9cad7b915a..5db3a27332 100644--- a/mobile/android/app/CMakeLists.txt+++ b/mobile/android/app/CMakeLists.txt@@ -6,7 +6,6 @@ set(CMAKE_C_STANDARD_REQUIRED ON)project(native_buffer LANGUAGES C)add_library(native_buffer SHARED- src/main/cpp/native_buffer.csrc/main/cpp/native_image.c)diff --git a/mobile/android/app/src/main/cpp/native_buffer.c b/mobile/android/app/src/main/cpp/native_buffer.cdeleted file mode 100644index bed1045382..0000000000--- a/mobile/android/app/src/main/cpp/native_buffer.c+++ /dev/null@@ -1,52 +0,0 @@-#include <jni.h>-#include <stdlib.h>-#include <string.h>--JNIEXPORT jlong JNICALL-Java_app_alextran_immich_NativeBuffer_allocate(- JNIEnv *env, jclass clazz, jint size) {- void *ptr = malloc(size);- return (jlong) ptr;-}--JNIEXPORT void JNICALL-Java_app_alextran_immich_NativeBuffer_free(- JNIEnv *env, jclass clazz, jlong address) {- free((void *) address);-}--JNIEXPORT jlong JNICALL-Java_app_alextran_immich_NativeBuffer_realloc(- JNIEnv *env, jclass clazz, jlong address, jint size) {- void *ptr = realloc((void *) address, size);- return (jlong) ptr;-}--JNIEXPORT jobject JNICALL-Java_app_alextran_immich_NativeBuffer_wrap(- JNIEnv *env, jclass clazz, jlong address, jint capacity) {- return (*env)->NewDirectByteBuffer(env, (void *) address, capacity);-}--JNIEXPORT void JNICALL-Java_app_alextran_immich_NativeBuffer_copy(- JNIEnv *env, jclass clazz, jobject buffer, jlong destAddress, jint offset, jint length) {- void *src = (*env)->GetDirectBufferAddress(env, buffer);- if (src != NULL) {- memcpy((void *) destAddress, (char *) src + offset, length);- }-}--/**- * Creates a JNI global reference to the given object and returns its address.- * The caller is responsible for deleting the global reference when it's no longer needed.- */-JNIEXPORT jlong JNICALL-Java_app_alextran_immich_NativeBuffer_createGlobalRef(JNIEnv *env, jobject clazz, jobject obj) {- if (obj == NULL) {- return 0;- }-- jobject globalRef = (*env)->NewGlobalRef(env, obj);- return (jlong) globalRef;-}diff --git a/mobile/android/app/src/main/kotlin/app/alextran/immich/NativeBuffer.kt b/mobile/android/app/src/main/kotlin/app/alextran/immich/NativeBuffer.ktindex 74f0241850..e80bb0a463 100644--- a/mobile/android/app/src/main/kotlin/app/alextran/immich/NativeBuffer.kt+++ b/mobile/android/app/src/main/kotlin/app/alextran/immich/NativeBuffer.kt@@ -6,7 +6,7 @@ const val INITIAL_BUFFER_SIZE = 32 * 1024object NativeBuffer {init {- System.loadLibrary("native_buffer")+ System.loadLibrary("immich_core_ffi")}@JvmStaticdiff --git a/native/Cargo.lock b/native/Cargo.lockindex 51aa6feb2e..b727b345a5 100644--- a/native/Cargo.lock+++ b/native/Cargo.lock@@ -8,6 +8,12 @@ version = "2.13.1"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "b588b76d00fde79687d7646a9b5bdf3cc0f655e0bbd080335a95d7e96f3587da"+[[package]]+name = "bytes"+version = "1.12.1"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "fc652a48c352aef3ea3aed32080501cf3ef6ed5da78602a020c991775b0aff04"+[[package]]name = "cbindgen"version = "0.29.4"@@ -32,6 +38,16 @@ version = "1.0.4"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801"+[[package]]+name = "combine"+version = "4.6.8"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "cfc320937d09e6de266b31b9afb480f197d7a861be86be7cb2ea7e5d1bfffc5e"+dependencies = [+ "bytes",+ "memchr",+]+[[package]]name = "equivalent"version = "1.0.2"@@ -87,6 +103,8 @@ version = "0.1.0"dependencies = ["cbindgen","immich_core",+ "jni",+ "libc",][[package]]@@ -105,6 +123,55 @@ version = "1.0.18"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "8f42a60cbdf9a97f5d2305f08a87dc4e09308d1276d28c869c684d7777685682"+[[package]]+name = "jni"+version = "0.22.4"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "5efd9a482cf3a427f00d6b35f14332adc7902ce91efb778580e180ff90fa3498"+dependencies = [+ "cfg-if",+ "combine",+ "jni-macros",+ "jni-sys",+ "log",+ "simd_cesu8",+ "thiserror",+ "walkdir",+ "windows-link",+]++[[package]]+name = "jni-macros"+version = "0.22.4"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "a00109accc170f0bdb141fed3e393c565b6f5e072365c3bd58f5b062591560a3"+dependencies = [+ "proc-macro2",+ "quote",+ "rustc_version",+ "simd_cesu8",+ "syn 2.0.119",+]++[[package]]+name = "jni-sys"+version = "0.4.1"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "c6377a88cb3910bee9b0fa88d4f42e1d2da8e79915598f65fb0c7ee14c878af2"+dependencies = [+ "jni-sys-macros",+]++[[package]]+name = "jni-sys-macros"+version = "0.4.1"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "38c0b942f458fe50cdac086d2f946512305e5631e720728f2a61aabcd47a6264"+dependencies = [+ "quote",+ "syn 2.0.119",+]+[[package]]name = "libc"version = "0.2.189"@@ -159,6 +226,15 @@ version = "6.0.0"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "f8dcc9c7d52a811697d2151c701e0d08956f92b0e24136cf4cf27b57a6a0d9bf"+[[package]]+name = "rustc_version"+version = "0.4.1"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "cfcb3a22ef46e85b45de6ee7e79d063319ebb6594faafcf1c225ea92ab6e9b92"+dependencies = [+ "semver",+]+[[package]]name = "rustix"version = "1.1.4"@@ -172,6 +248,21 @@ dependencies = ["windows-sys",]+[[package]]+name = "same-file"+version = "1.0.6"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502"+dependencies = [+ "winapi-util",+]++[[package]]+name = "semver"+version = "1.0.28"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "8a7852d02fc848982e0c167ef163aaff9cd91dc640ba85e263cb1ce46fae51cd"+[[package]]name = "serde"version = "1.0.229"@@ -224,6 +315,22 @@ dependencies = ["serde_core",]+[[package]]+name = "simd_cesu8"+version = "1.2.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "11031e251abf8611c80f460e19dbdeb54a66db918e49c65a7065b46ac7aec520"+dependencies = [+ "rustc_version",+ "simdutf8",+]++[[package]]+name = "simdutf8"+version = "0.1.5"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "e3a9fe34e3e7a50316060351f37187a3f546bce95496156754b601a5fa71b76e"+[[package]]name = "syn"version = "2.0.119"@@ -259,6 +366,26 @@ dependencies = ["windows-sys",]+[[package]]+name = "thiserror"+version = "2.0.20"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "ec86235f5fcc2a73650310756d2ac5b138a5780bbbdfae3eeccec992c435ba4f"+dependencies = [+ "thiserror-impl",+]++[[package]]+name = "thiserror-impl"+version = "2.0.20"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "bc04cd3e1236dd4a98afca4569f2deb3f120e5422a4023be2cb683f8486292af"+dependencies = [+ "proc-macro2",+ "quote",+ "syn 3.0.4",+]+[[package]]name = "toml"version = "0.9.12+spec-1.1.0"@@ -304,6 +431,25 @@ version = "1.0.24"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75"+[[package]]+name = "walkdir"+version = "2.5.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "29790946404f91d9c5d06f9874efddea1dc06c5efe94541a7d6863108e3a5e4b"+dependencies = [+ "same-file",+ "winapi-util",+]++[[package]]+name = "winapi-util"+version = "0.1.11"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22"+dependencies = [+ "windows-sys",+]+[[package]]name = "windows-link"version = "0.2.1"diff --git a/native/Cargo.toml b/native/Cargo.tomlindex 16024f72e5..87591be4dc 100644--- a/native/Cargo.toml+++ b/native/Cargo.toml@@ -9,6 +9,8 @@ license = "AGPL-3.0-only"[workspace.dependencies]cbindgen = { version = "0.29", default-features = false }+jni = { version = "0.22", default-features = false }+libc = { version = "0.2", default-features = false }[workspace.lints.clippy]undocumented_unsafe_blocks = "deny"diff --git a/native/README.md b/native/README.mdindex 40fb2f4a29..253eb10e4a 100644--- a/native/README.md+++ b/native/README.md@@ -8,5 +8,6 @@ fetches the pinned toolchain itself.- `crates/immich_core`: the shared logic- `crates/immich_core_ffi`: the C ABI, cbindgen writes `include/immich_core.h`- `immich_native_core`: Flutter package with the build hook and the ffigen bindings+ (`NOTICES` carries the licenses of the crates compiled into the core, update it with the dependencies)`mise run build`, `test`, `lint`, and `fmt`. The header and the Dart bindings come from `mise run //mobile:codegen:native`.diff --git a/native/crates/immich_core_ffi/Cargo.toml b/native/crates/immich_core_ffi/Cargo.tomlindex 908be070e1..be37d949bd 100644--- a/native/crates/immich_core_ffi/Cargo.toml+++ b/native/crates/immich_core_ffi/Cargo.toml@@ -11,6 +11,10 @@ crate-type = ["cdylib", "staticlib"][dependencies]immich_core = { path = "../immich_core" }+libc = { workspace = true }++[target.'cfg(target_os = "android")'.dependencies]+jni = { workspace = true }[build-dependencies]cbindgen = { workspace = true }diff --git a/native/crates/immich_core_ffi/src/android.rs b/native/crates/immich_core_ffi/src/android.rsnew file mode 100644index 0000000000..05778e0a41--- /dev/null+++ b/native/crates/immich_core_ffi/src/android.rs@@ -0,0 +1,83 @@+use jni::EnvUnowned;+use jni::errors::ThrowRuntimeExAndDefault;+use jni::objects::{JByteBuffer, JClass, JObject};+use jni::sys::{jint, jlong, jobject};++#[unsafe(no_mangle)]+pub extern "system" fn Java_app_alextran_immich_NativeBuffer_allocate<'caller>(+ _env: EnvUnowned<'caller>,+ _class: JClass<'caller>,+ size: jint,+) -> jlong {+ // SAFETY: malloc accepts any size.+ unsafe { libc::malloc(size as usize) as jlong }+}++#[unsafe(no_mangle)]+pub extern "system" fn Java_app_alextran_immich_NativeBuffer_free<'caller>(+ _env: EnvUnowned<'caller>,+ _class: JClass<'caller>,+ address: jlong,+) {+ // SAFETY: the caller passes null or a live C allocation it owns.+ unsafe { libc::free(address as *mut _) };+}++#[unsafe(no_mangle)]+pub extern "system" fn Java_app_alextran_immich_NativeBuffer_realloc<'caller>(+ _env: EnvUnowned<'caller>,+ _class: JClass<'caller>,+ address: jlong,+ size: jint,+) -> jlong {+ // SAFETY: the caller passes null or a live C allocation it owns.+ unsafe { libc::realloc(address as *mut _, size as usize) as jlong }+}++#[unsafe(no_mangle)]+pub extern "system" fn Java_app_alextran_immich_NativeBuffer_wrap<'caller>(+ env: EnvUnowned<'caller>,+ _class: JClass<'caller>,+ address: jlong,+ capacity: jint,+) -> jobject {+ // jni's new_direct_byte_buffer rejects null and clears the java exception, the C did neither.+ let raw = env.as_raw();+ // SAFETY: raw is the VM's JNIEnv and android JNI is at least 1.6, so the 1.4 table exists.+ unsafe { ((**raw).v1_4.NewDirectByteBuffer)(raw, address as *mut _, capacity as jlong) }+}++#[unsafe(no_mangle)]+pub extern "system" fn Java_app_alextran_immich_NativeBuffer_copy<'caller>(+ mut env: EnvUnowned<'caller>,+ _class: JClass<'caller>,+ buffer: JByteBuffer<'caller>,+ dest: jlong,+ offset: jint,+ length: jint,+) {+ env.with_env(|env| -> jni::errors::Result<()> {+ if let Ok(src) = env.get_direct_buffer_address(&buffer) {+ // SAFETY: the caller supplies valid, non-overlapping ranges for the copy.+ unsafe {+ let src = src.offset(offset as isize).cast();+ libc::memcpy(dest as *mut _, src, length as usize);+ }+ }+ Ok(())+ })+ .resolve::<ThrowRuntimeExAndDefault>();+}++#[unsafe(no_mangle)]+pub extern "system" fn Java_app_alextran_immich_NativeBuffer_createGlobalRef<'caller>(+ mut env: EnvUnowned<'caller>,+ _class: JClass<'caller>,+ obj: JObject<'caller>,+) -> jlong {+ env.with_env(|env| -> jni::errors::Result<_> {+ // Transfer the global reference to the caller without deleting it.+ Ok(env.new_global_ref(obj).map_or(0, |r| r.into_raw() as jlong))+ })+ .resolve::<ThrowRuntimeExAndDefault>()+}diff --git a/native/crates/immich_core_ffi/src/lib.rs b/native/crates/immich_core_ffi/src/lib.rsindex 6df4ff83b0..3680db52af 100644--- a/native/crates/immich_core_ffi/src/lib.rs+++ b/native/crates/immich_core_ffi/src/lib.rs@@ -2,6 +2,9 @@ use std::ffi::{CString, c_char};use std::panic::{self, UnwindSafe};use std::ptr;+#[cfg(target_os = "android")]+mod android;+/// Returns the core version as a C string. Free it with `immich_core_free_string`.#[unsafe(no_mangle)]pub extern "C" fn immich_core_version() -> *mut c_char {diff --git a/native/immich_native_core/NOTICES b/native/immich_native_core/NOTICESnew file mode 100644index 0000000000..d26bf7933a--- /dev/null+++ b/native/immich_native_core/NOTICES@@ -0,0 +1,38 @@+bytes+cfg-if+combine+jni+jni-sys+libc+log+memchr+simd_cesu8+simdutf8+thiserror++Copyright (c) 2018 Carl Lerche+Copyright (c) 2014 Alex Crichton+Copyright (c) 2015 Markus Westerlind+Copyright (c) 2016 Prevoty, Inc. and jni-rs contributors+Copyright (c) 2015 The rust-jni-sys Developers+Copyright (c) The Rust Project Developers+Copyright (c) 2014 The Rust Project Developers+Copyright (c) 2015 Andrew Gallant++Permission is hereby granted, free of charge, to any person obtaining a copy+of this software and associated documentation files (the "Software"), to deal+in the Software without restriction, including without limitation the rights+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell+copies of the Software, and to permit persons to whom the Software is+furnished to do so, subject to the following conditions:++The above copyright notice and this permission notice shall be included in+all copies or substantial portions of the Software.++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN+THE SOFTWARE.