Skip to content

Commit cfe8795

Browse files
committed
feat: port style transfer to C++
1 parent 7051552 commit cfe8795

File tree

236 files changed

+129925
-2026
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

236 files changed

+129925
-2026
lines changed

android/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ string(APPEND CMAKE_CXX_FLAGS " -DRCT_NEW_ARCH_ENABLED")
1111

1212
set(ANDROID_CPP_DIR "${CMAKE_SOURCE_DIR}/src/main/cpp")
1313
set(COMMON_CPP_DIR "${CMAKE_SOURCE_DIR}/../common")
14-
set(ET_LIB_DIR "${CMAKE_SOURCE_DIR}/../third-party/android/libs")
15-
set(ET_INCLUDE_DIR "${CMAKE_SOURCE_DIR}/../third-party/include")
14+
set(LIBS_DIR "${CMAKE_SOURCE_DIR}/../third-party/android/libs")
15+
set(INCLUDE_DIR "${CMAKE_SOURCE_DIR}/../third-party/include")
1616

1717
add_subdirectory("${ANDROID_CPP_DIR}")

android/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ android {
111111
targetSdkVersion getExtOrIntegerDefault("targetSdkVersion")
112112
externalNativeBuild {
113113
cmake {
114-
cppFlags "-O2 -frtti -fexceptions -Wall -fstack-protector-all"
114+
cppFlags "-O2 -frtti -fexceptions -Wall -fstack-protector-all -fopenmp -static-openmp"
115115
abiFilters (*reactNativeArchitectures())
116116
arguments "-DANDROID_STL=c++_shared",
117117
"-DREACT_NATIVE_DIR=${toPlatformFileString(reactNativeRootDir.path)}"
@@ -162,7 +162,7 @@ dependencies {
162162
implementation 'com.facebook.fbjni:fbjni:0.6.0'
163163
implementation 'org.opencv:opencv:4.10.0'
164164
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
165-
implementation(files("../third-party/android/libs/executorch.aar"))
165+
implementation(files("../third-party/android/libs/executorch/executorch.aar"))
166166
implementation 'org.opencv:opencv:4.10.0'
167167
implementation("com.squareup.okhttp3:okhttp:4.9.2")
168168
}

android/src/main/cpp/CMakeLists.txt

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,10 @@ target_include_directories(
1313
PUBLIC
1414
"${COMMON_CPP_DIR}"
1515
"${ANDROID_CPP_DIR}"
16-
"${ET_INCLUDE_DIR}"
16+
"${INCLUDE_DIR}"
1717
"${REACT_NATIVE_DIR}/ReactCommon"
1818
"${REACT_NATIVE_DIR}/ReactAndroid/src/main/jni/react/turbomodule"
1919
"${REACT_NATIVE_DIR}/ReactCommon/callinvoker"
20-
"${BUILD_DIR}/generated/source/codegen/jni/react/renderer/components/RnExecutorchSpec"
2120
)
2221

2322
set(LINK_LIBRARIES
@@ -31,14 +30,42 @@ set(RN_VERSION_LINK_LIBRARIES
3130
ReactAndroid::reactnative
3231
)
3332

33+
# Dependencies:
34+
# ------- Executorch -------
35+
3436
add_library(executorch SHARED IMPORTED)
3537

3638
set_target_properties(executorch PROPERTIES
37-
IMPORTED_LOCATION "${ET_LIB_DIR}/${ANDROID_ABI}/libexecutorch.so")
39+
IMPORTED_LOCATION "${LIBS_DIR}/executorch/${ANDROID_ABI}/libexecutorch.so")
40+
41+
# ------- OpenCV -------
42+
43+
set(OPENCV_LIBS
44+
"${LIBS_DIR}/opencv/${ANDROID_ABI}/libopencv_core.a"
45+
"${LIBS_DIR}/opencv/${ANDROID_ABI}/libopencv_features2d.a"
46+
"${LIBS_DIR}/opencv/${ANDROID_ABI}/libopencv_highgui.a"
47+
"${LIBS_DIR}/opencv/${ANDROID_ABI}/libopencv_imgproc.a"
48+
"${LIBS_DIR}/opencv/${ANDROID_ABI}/libopencv_photo.a"
49+
"${LIBS_DIR}/opencv/${ANDROID_ABI}/libopencv_video.a"
50+
)
51+
52+
if(ANDROID_ABI STREQUAL "arm64-v8a")
53+
set(OPENCV_THIRD_PARTY_LIBS
54+
"${LIBS_DIR}/opencv-third-party/${ANDROID_ABI}/libkleidicv_hal.a"
55+
"${LIBS_DIR}/opencv-third-party/${ANDROID_ABI}/libkleidicv_thread.a"
56+
"${LIBS_DIR}/opencv-third-party/${ANDROID_ABI}/libkleidicv.a"
57+
)
58+
elseif(ANDROID_ABI STREQUAL "x86_64")
59+
set(OPENCV_THIRD_PARTY_LIBS "")
60+
endif()
61+
62+
# --------------
3863

3964
target_link_libraries(
4065
react-native-executorch
4166
${LINK_LIBRARIES}
4267
${RN_VERSION_LINK_LIBRARIES}
68+
${OPENCV_LIBS}
69+
${OPENCV_THIRD_PARTY_LIBS}
4370
executorch
4471
)

android/src/main/cpp/ETInstallerModule.cpp

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
#include "ETInstallerModule.h"
2-
#include "RnExecutorchInstaller.h"
32

4-
namespace rnexecutorch {
3+
#include <rnexecutorch/RnExecutorchInstaller.h>
4+
5+
#include <jni.h>
6+
#include <jsi/jsi.h>
57

8+
namespace rnexecutorch {
9+
JavaVM *java_machine;
610
using namespace facebook::jni;
711

812
ETInstallerModule::ETInstallerModule(
@@ -30,6 +34,39 @@ void ETInstallerModule::registerNatives() {
3034
}
3135

3236
void ETInstallerModule::injectJSIBindings() {
33-
RnExecutorchInstaller::injectJSIBindings(jsiRuntime_, jsCallInvoker_);
37+
// Grab a function for fetching images via URL from Java
38+
auto fetchDataByUrl = [](std::string url) {
39+
// Attaching Current Thread to JVM
40+
41+
JNIEnv *env = nullptr;
42+
int getEnvStat = java_machine->GetEnv((void **)&env, JNI_VERSION_1_6);
43+
if (getEnvStat == JNI_EDETACHED) {
44+
if (java_machine->AttachCurrentThread(&env, nullptr) != 0) {
45+
throw std::runtime_error("Failed to attach thread to JVM");
46+
}
47+
}
48+
static jclass cls = javaClassStatic().get();
49+
static jmethodID method = env->GetStaticMethodID(
50+
cls, "fetchByteDataFromUrl", "(Ljava/lang/String;)[B");
51+
52+
jstring jUrl = env->NewStringUTF(url.c_str());
53+
jbyteArray byteData =
54+
(jbyteArray)env->CallStaticObjectMethod(cls, method, jUrl);
55+
56+
int size = env->GetArrayLength(byteData);
57+
jbyte *bytes = env->GetByteArrayElements(byteData, JNI_FALSE);
58+
std::byte *dataBytePtr = reinterpret_cast<std::byte *>(bytes);
59+
60+
return std::vector<std::byte>(dataBytePtr, dataBytePtr + size);
61+
};
62+
63+
RnExecutorchInstaller::injectJSIBindings(jsiRuntime_, jsCallInvoker_,
64+
fetchDataByUrl);
3465
}
35-
} // namespace rnexecutorch
66+
} // namespace rnexecutorch
67+
68+
JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *) {
69+
rnexecutorch::java_machine = vm;
70+
return facebook::jni::initialize(
71+
vm, [] { rnexecutorch::ETInstallerModule::registerNatives(); });
72+
}

android/src/main/cpp/OnLoad.cpp

Lines changed: 0 additions & 10 deletions
This file was deleted.

android/src/main/java/com/swmansion/rnexecutorch/ETInstaller.kt

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package com.swmansion.rnexecutorch
22

33
import com.facebook.jni.HybridData
4+
import com.facebook.proguard.annotations.DoNotStrip
45
import com.facebook.react.bridge.ReactApplicationContext
56
import com.facebook.react.bridge.ReactMethod
67
import com.facebook.react.common.annotations.FrameworkAPI
78
import com.facebook.react.module.annotations.ReactModule
89
import com.facebook.react.turbomodule.core.CallInvokerHolderImpl
10+
import java.io.InputStream
11+
import java.net.URL
912

1013
@OptIn(FrameworkAPI::class)
1114
@ReactModule(name = ETInstaller.NAME)
@@ -14,6 +17,21 @@ class ETInstaller(
1417
) : NativeETInstallerSpec(reactContext) {
1518
companion object {
1619
const val NAME = NativeETInstallerSpec.NAME
20+
21+
@JvmStatic
22+
@DoNotStrip
23+
@Throws(Exception::class)
24+
fun fetchByteDataFromUrl(source: String): ByteArray {
25+
val url = URL(source)
26+
val connection = url.openConnection()
27+
connection.connect()
28+
29+
val inputStream: InputStream = connection.getInputStream()
30+
val data = inputStream.readBytes()
31+
inputStream.close()
32+
33+
return data
34+
}
1735
}
1836

1937
private val mHybridData: HybridData
@@ -32,7 +50,7 @@ class ETInstaller(
3250
val jsCallInvokerHolder = reactContext.jsCallInvokerHolder as CallInvokerHolderImpl
3351
mHybridData = initHybrid(reactContext.javaScriptContextHolder!!.get(), jsCallInvokerHolder)
3452
} catch (exception: UnsatisfiedLinkError) {
35-
throw RuntimeException("Could not load native module Install", exception)
53+
throw RuntimeException("Could not load native module ETInstaller", exception)
3654
}
3755
}
3856

android/src/main/java/com/swmansion/rnexecutorch/RnExecutorchPackage.kt

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ class RnExecutorchPackage : TurboReactPackage() {
1818
LLM(reactContext)
1919
} else if (name == ETModule.NAME) {
2020
ETModule(reactContext)
21-
} else if (name == StyleTransfer.NAME) {
22-
StyleTransfer(reactContext)
2321
} else if (name == Classification.NAME) {
2422
Classification(reactContext)
2523
} else if (name == ObjectDetection.NAME) {
@@ -66,17 +64,6 @@ class RnExecutorchPackage : TurboReactPackage() {
6664
true,
6765
)
6866

69-
moduleInfos[StyleTransfer.NAME] =
70-
ReactModuleInfo(
71-
StyleTransfer.NAME,
72-
StyleTransfer.NAME,
73-
false, // canOverrideExistingModule
74-
false, // needsEagerInit
75-
true, // hasConstants
76-
false, // isCxxModule
77-
true,
78-
)
79-
8067
moduleInfos[Classification.NAME] =
8168
ReactModuleInfo(
8269
Classification.NAME,

android/src/main/java/com/swmansion/rnexecutorch/StyleTransfer.kt

Lines changed: 0 additions & 54 deletions
This file was deleted.

android/src/main/java/com/swmansion/rnexecutorch/models/styleTransfer/StyleTransferModel.kt

Lines changed: 0 additions & 43 deletions
This file was deleted.

0 commit comments

Comments
 (0)