High performance self-hosted photo and video management solution.
TypeScript backup-toolfluttergoogle-photosgoogle-photos-alternativejavascriptmobile-appnestjsnodejsphoto-galleryphotosphotos-managementself-hostedsveltesveltekittypescriptvideos
chore(mobile): move the 10 bit conversion into the native core (#31363)
Santo Shakilb08a8fb56c17
diff --git a/mobile/android/app/CMakeLists.txt b/mobile/android/app/CMakeLists.txtdeleted file mode 100644index 5db3a27332..0000000000--- a/mobile/android/app/CMakeLists.txt+++ /dev/null@@ -1,12 +0,0 @@-cmake_minimum_required(VERSION 3.12)--set(CMAKE_C_STANDARD 17)-set(CMAKE_C_STANDARD_REQUIRED ON)--project(native_buffer LANGUAGES C)--add_library(native_buffer SHARED- src/main/cpp/native_image.c-)--target_link_libraries(native_buffer jnigraphics)diff --git a/mobile/android/app/build.gradle b/mobile/android/app/build.gradleindex 7b3217ef85..f2bb310914 100644--- a/mobile/android/app/build.gradle+++ b/mobile/android/app/build.gradle@@ -78,12 +78,6 @@ android {}}namespace 'app.alextran.immich'-- externalNativeBuild {- cmake {- path "CMakeLists.txt"- }- }}flutter {diff --git a/mobile/android/app/src/main/cpp/native_image.c b/mobile/android/app/src/main/cpp/native_image.cdeleted file mode 100644index ce188e06a3..0000000000--- a/mobile/android/app/src/main/cpp/native_image.c+++ /dev/null@@ -1,68 +0,0 @@-#include <jni.h>-#include <stdlib.h>-#include <stdint.h>-#include <android/bitmap.h>--// Convert an RGBA_1010102 buffer to densely-packed RGBA_8888, matching Skia's-// Bitmap.copy(ARGB_8888) byte-for-byte so it's a drop-in for the intermediate 8888 bitmap.-// Each source pixel is a native (little-endian on every Android ABI) u32 with R in bits 0-9,-// G in 10-19, B in 20-29, A in 30-31 (standard RGB10_A2). Each 10-bit channel maps to 8-bit via-// round(v*255/1023). The 2-bit alpha maps to a*85. Both are kept as plain arithmetic as the whole-// loop auto-vectorizes to NEON and measures faster than a LUT on-device. Output is R,G,B,A bytes-// per pixel, i.e. Android ARGB_8888 memory == Dart PixelFormat.rgba8888.-static void convert_1010102(const uint8_t *src, int srcStride, uint32_t *dst, int w, int h) {- for (int y = 0; y < h; y++) {- const uint32_t *srcRow = (const uint32_t *) (src + (size_t) y * srcStride);- uint32_t *dstRow = dst + (size_t) y * w;- for (int x = 0; x < w; x++) {- uint32_t px = srcRow[x];- uint32_t r = ((px & 0x3FF) * 16336u + 32768u) >> 16;- uint32_t g = (((px >> 10) & 0x3FF) * 16336u + 32768u) >> 16;- uint32_t b = (((px >> 20) & 0x3FF) * 16336u + 32768u) >> 16;- uint32_t a = ((px >> 30) & 0x3) * 85u;- dstRow[x] = r | (g << 8) | (b << 16) | (a << 24);- }- }-}--// Converts an RGBA_1010102 bitmap (what a 10-bit HEIC/AVIF decodes to on API 33+) into a freshly-// malloc'd RGBA_8888 buffer. Fills outInfo with {width, height, rowBytes} and returns the buffer-// address, or 0 (so the caller falls back to a Skia copy) if the bitmap isn't 1010102 or can't be-// locked. Same ownership contract as rotate: free the returned buffer via NativeBuffer.free.-JNIEXPORT jlong JNICALL-Java_app_alextran_immich_NativeImage_convert1010102(- JNIEnv *env, jclass clazz, jobject bitmap, jintArray outInfo) {- AndroidBitmapInfo info;- if (AndroidBitmap_getInfo(env, bitmap, &info) != ANDROID_BITMAP_RESULT_SUCCESS) {- return 0;- }- if (info.format != ANDROID_BITMAP_FORMAT_RGBA_1010102) {- return 0;- }-- int w = (int) info.width;- int h = (int) info.height;-- uint32_t *dst = (uint32_t *) malloc((size_t) w * h * 4);- if (dst == NULL) {- return 0;- }-- void *srcPixels = NULL;- if (AndroidBitmap_lockPixels(env, bitmap, &srcPixels) != ANDROID_BITMAP_RESULT_SUCCESS) {- free(dst);- return 0;- }-- convert_1010102((const uint8_t *) srcPixels, (int) info.stride, dst, w, h);-- AndroidBitmap_unlockPixels(env, bitmap);-- jint dims[3] = {w, h, w * 4};- (*env)->SetIntArrayRegion(env, outInfo, 0, 3, dims);- if ((*env)->ExceptionCheck(env)) {- free(dst);- return 0;- }- return (jlong) dst;-}diff --git a/mobile/android/app/src/main/kotlin/app/alextran/immich/NativeImage.kt b/mobile/android/app/src/main/kotlin/app/alextran/immich/NativeImage.ktindex 73645f1e3d..44812e3911 100644--- a/mobile/android/app/src/main/kotlin/app/alextran/immich/NativeImage.kt+++ b/mobile/android/app/src/main/kotlin/app/alextran/immich/NativeImage.kt@@ -4,7 +4,6 @@ import android.graphics.Bitmapobject NativeImage {init {- System.loadLibrary("native_buffer")System.loadLibrary("immich_core_ffi")}diff --git a/native/.cargo/config.toml b/native/.cargo/config.tomlnew file mode 100644index 0000000000..e7138051c6--- /dev/null+++ b/native/.cargo/config.toml@@ -0,0 +1,10 @@+# Link against minSdk (mobile/android/app/build.gradle) instead of the API 35 that native_toolchain_rust picks.+[target.aarch64-linux-android]+rustflags = ["-C", "link-arg=--target=aarch64-linux-android26"]++# Neon on 32 bit arm is the NDK's default for C, the rust target leaves it off.+[target.armv7-linux-androideabi]+rustflags = ["-C", "link-arg=--target=armv7a-linux-androideabi26", "-C", "target-feature=+neon"]++[target.x86_64-linux-android]+rustflags = ["-C", "link-arg=--target=x86_64-linux-android26"]diff --git a/native/crates/immich_core/src/convert.rs b/native/crates/immich_core/src/convert.rsnew file mode 100644index 0000000000..cda3376129--- /dev/null+++ b/native/crates/immich_core/src/convert.rs@@ -0,0 +1,15 @@+/// Converts `h` rows of `w` RGBA_1010102 pixels, `stride` pixels apart in `src`, to dense RGBA_8888+/// in `dst`.+pub fn convert_1010102(src: &[u32], stride: usize, w: usize, h: usize, dst: &mut [u32]) {+ // Each 10-bit channel rounds v*255/1023 (16.16 fixed point) and alpha maps to a*85. Plain+ // arithmetic on purpose: the loop auto-vectorizes and beat a lookup table on-device.+ for y in 0..h {+ for (out, &px) in dst[y * w..][..w].iter_mut().zip(&src[y * stride..][..w]) {+ let r = ((px & 0x3ff) * 16336 + 32768) >> 16;+ let g = (((px >> 10) & 0x3ff) * 16336 + 32768) >> 16;+ let b = (((px >> 20) & 0x3ff) * 16336 + 32768) >> 16;+ let a = (px >> 30) * 85;+ *out = r | (g << 8) | (b << 16) | (a << 24);+ }+ }+}diff --git a/native/crates/immich_core/src/lib.rs b/native/crates/immich_core/src/lib.rsindex e642f99406..aea1c53633 100644--- a/native/crates/immich_core/src/lib.rs+++ b/native/crates/immich_core/src/lib.rs@@ -1,3 +1,4 @@+pub mod convert;pub mod rotate;pub fn core_version() -> &'static str {diff --git a/native/crates/immich_core/tests/convert.rs b/native/crates/immich_core/tests/convert.rsnew file mode 100644index 0000000000..e6d614613e--- /dev/null+++ b/native/crates/immich_core/tests/convert.rs@@ -0,0 +1,32 @@+use immich_core::convert::convert_1010102;++#[test]+fn channels_and_alpha() {+ for (a, byte) in [(0, 0), (1, 85), (2, 170), (3, 255)] {+ let src: [u32; 3] = [+ (0x0ff << 10) | (0x3ff << 20) | (a << 30),+ 0x0ff | (0x3ff << 10) | (a << 30),+ 0x3ff | (0x0ff << 20) | (a << 30),+ ];+ let mut dst = [0; 3];+ convert_1010102(&src, 3, 3, 1, &mut dst);+ assert_eq!(+ dst,+ [+ 0xff4000 | (byte << 24),+ 0x00ff40 | (byte << 24),+ 0x4000ff | (byte << 24)+ ]+ );+ }+}++#[test]+fn padded_rows() {+ let src: [u32; 6] = [+ 0xc00003ff, 0xc00ffc00, 0xdeadbeef, 0xfff00000, 0xffffffff, 0xdeadbeef,+ ];+ let mut dst = [0; 4];+ convert_1010102(&src, 3, 2, 2, &mut dst);+ assert_eq!(dst, [0xff0000ff, 0xff00ff00, 0xffff0000, 0xffffffff]);+}diff --git a/native/crates/immich_core_ffi/src/bitmap.rs b/native/crates/immich_core_ffi/src/bitmap.rsindex d4f96b3a87..f974e22759 100644--- a/native/crates/immich_core_ffi/src/bitmap.rs+++ b/native/crates/immich_core_ffi/src/bitmap.rs@@ -8,26 +8,27 @@ use jni::objects::{JIntArray, JObject};use jni::sys::{JNIEnv, jlong, jobject};pub const FORMAT_RGBA_8888: i32 = 1;+pub const FORMAT_RGBA_1010102: i32 = 10;-pub const RESULT_SUCCESS: i32 = 0;+const RESULT_SUCCESS: i32 = 0;#[repr(C)]-pub struct Info {- pub width: u32,- pub height: u32,- pub stride: u32,- pub format: i32,- pub flags: u32,+struct Info {+ width: u32,+ height: u32,+ stride: u32,+ format: i32,+ flags: u32,}#[link(name = "jnigraphics")]unsafe extern "C" {#[link_name = "AndroidBitmap_getInfo"]- pub fn get_info(env: *mut JNIEnv, bitmap: jobject, info: *mut Info) -> i32;+ fn get_info(env: *mut JNIEnv, bitmap: jobject, info: *mut Info) -> i32;#[link_name = "AndroidBitmap_lockPixels"]- pub fn lock_pixels(env: *mut JNIEnv, bitmap: jobject, pixels: *mut *mut c_void) -> i32;+ fn lock_pixels(env: *mut JNIEnv, bitmap: jobject, pixels: *mut *mut c_void) -> i32;#[link_name = "AndroidBitmap_unlockPixels"]- pub fn unlock_pixels(env: *mut JNIEnv, bitmap: jobject) -> i32;+ fn unlock_pixels(env: *mut JNIEnv, bitmap: jobject) -> i32;}/// Runs `f(src, stride, width, height, dst)` over the pixels of a `format` bitmap, writing into adiff --git a/native/crates/immich_core_ffi/src/convert.rs b/native/crates/immich_core_ffi/src/convert.rsnew file mode 100644index 0000000000..f25ce3d621--- /dev/null+++ b/native/crates/immich_core_ffi/src/convert.rs@@ -0,0 +1,23 @@+use immich_core::convert::convert_1010102;+use jni::EnvUnowned;+use jni::objects::{JClass, JIntArray, JObject};+use jni::sys::jlong;++use super::bitmap::{self, FORMAT_RGBA_1010102};++#[unsafe(no_mangle)]+pub extern "system" fn Java_app_alextran_immich_NativeImage_convert1010102<'caller>(+ env: EnvUnowned<'caller>,+ _class: JClass<'caller>,+ bitmap: JObject<'caller>,+ out_info: JIntArray<'caller>,+) -> jlong {+ bitmap::transform(+ env,+ bitmap,+ out_info,+ FORMAT_RGBA_1010102,+ |w, h| (w, h),+ convert_1010102,+ )+}diff --git a/native/crates/immich_core_ffi/src/lib.rs b/native/crates/immich_core_ffi/src/lib.rsindex c0e4cd70ff..c4aafa0e5d 100644--- a/native/crates/immich_core_ffi/src/lib.rs+++ b/native/crates/immich_core_ffi/src/lib.rs@@ -8,6 +8,8 @@ mod android;#[cfg(target_os = "android")]mod bitmap;#[cfg(target_os = "android")]+mod convert;+#[cfg(target_os = "android")]mod rotate;/// Returns the core version as a C string. Free it with `immich_core_free_string`.diff --git a/native/immich_native_core/hook/build.dart b/native/immich_native_core/hook/build.dartindex 4ee11e5de3..d60c72a2c4 100644--- a/native/immich_native_core/hook/build.dart+++ b/native/immich_native_core/hook/build.dart@@ -8,6 +8,7 @@ const _crate = '../crates/immich_core_ffi';// Cargo's dep-info only lists source files, so these have to rerun the hook too.const _manifests = [+ '../.cargo/config.toml','../Cargo.toml','../Cargo.lock','../crates/immich_core/Cargo.toml',@@ -24,11 +25,7 @@ void main(List<String> args) async {if (target != null) {await _rustup(['target', 'add', '--toolchain', await _channel(input), target]);}- await RustBuilder(- assetName: 'src/bindings.g.dart',- cratePath: _crate,- extraCargoEnvironmentVariables: {if (code.targetOS == OS.android) 'RUSTFLAGS': _androidRustFlags(code)},- ).run(input: input, output: output);+ await RustBuilder(assetName: 'src/bindings.g.dart', cratePath: _crate).run(input: input, output: output);});}@@ -64,14 +61,3 @@ Future<void> _rustup(List<String> args) async {throw ProcessException('rustup', args, '${result.stderr}', result.exitCode);}}--// Link against minSdk instead of the API 35 that native_toolchain_rust picks.-String _androidRustFlags(CodeConfig code) {- final triple = switch (code.targetArchitecture) {- Architecture.arm => 'armv7a-linux-androideabi',- Architecture.arm64 => 'aarch64-linux-android',- Architecture.x64 => 'x86_64-linux-android',- _ => throw UnsupportedError('Unsupported Android architecture: ${code.targetArchitecture}'),- };- return '-C link-arg=--target=$triple${code.android.targetNdkApi}';-}