From 34b71e4fb4be461aa53c2b0de190f267567218d5 Mon Sep 17 00:00:00 2001 From: Sidharth Guglani Date: Thu, 26 Sep 2019 17:30:28 -0700 Subject: [PATCH] Add separate classes to implement JNI methods suing vanilla JNI Summary: This diffs adds a separate file YGJNIVanilla.cpp to add jni methods which uses vanilla JNI instead of FBJNI. In this diff only one method has been added to setup the experiment boolean setup. At the end of this diff stack , we will be able to experiment between fbjni and vanilla jni in yoga and finally get rid of fbjni which saves us around 300Kb per architecture in yoga binary size. Reviewed By: Andrey-Mishanin Differential Revision: D17601591 fbshipit-source-id: a88520c625bd8b5d9ffcf8ab5f02fc71dc800081 --- .../java/com/facebook/yoga/YogaNative.java | 4 ++ .../main/jni/first-party/yogajni/Android.mk | 3 +- .../jni/first-party/yogajni/jni/YGJNI.cpp | 5 +- .../first-party/yogajni/jni/YGJNIVanilla.cpp | 55 +++++++++++++++++++ .../first-party/yogajni/jni/YGJNIVanilla.h | 9 +++ 5 files changed, 74 insertions(+), 2 deletions(-) create mode 100644 ReactAndroid/src/main/jni/first-party/yogajni/jni/YGJNIVanilla.cpp create mode 100644 ReactAndroid/src/main/jni/first-party/yogajni/jni/YGJNIVanilla.h diff --git a/ReactAndroid/src/main/java/com/facebook/yoga/YogaNative.java b/ReactAndroid/src/main/java/com/facebook/yoga/YogaNative.java index 5b29489851e..88595f38b90 100644 --- a/ReactAndroid/src/main/java/com/facebook/yoga/YogaNative.java +++ b/ReactAndroid/src/main/java/com/facebook/yoga/YogaNative.java @@ -111,4 +111,8 @@ public class YogaNative { static native void jni_YGNodePrint(long nativePointer); static native void jni_YGNodeSetStyleInputs(long nativePointer, float[] styleInputsArray, int size); static native long jni_YGNodeClone(long nativePointer); + + + // JNI methods that use Vanilla JNI + public static native void jni_YGNodeStyleSetFlexJNI(long nativePointer, float flex); } diff --git a/ReactAndroid/src/main/jni/first-party/yogajni/Android.mk b/ReactAndroid/src/main/jni/first-party/yogajni/Android.mk index 7fa2b376b37..77bdf57424d 100644 --- a/ReactAndroid/src/main/jni/first-party/yogajni/Android.mk +++ b/ReactAndroid/src/main/jni/first-party/yogajni/Android.mk @@ -11,7 +11,8 @@ LOCAL_MODULE := yoga LOCAL_SRC_FILES := \ jni/YGJNI.cpp \ - jni/YGJTypes.cpp + jni/YGJTypes.cpp \ + jni/YGJNIVanilla.cpp LOCAL_C_INCLUDES := $(LOCAL_PATH)/jni diff --git a/ReactAndroid/src/main/jni/first-party/yogajni/jni/YGJNI.cpp b/ReactAndroid/src/main/jni/first-party/yogajni/jni/YGJNI.cpp index f23d0f2b952..88e325d29b3 100644 --- a/ReactAndroid/src/main/jni/first-party/yogajni/jni/YGJNI.cpp +++ b/ReactAndroid/src/main/jni/first-party/yogajni/jni/YGJNI.cpp @@ -15,6 +15,7 @@ #include #include "YGJTypes.h" +#include "YGJNIVanilla.h" using namespace facebook::jni; using namespace std; @@ -893,7 +894,7 @@ void jni_YGNodeStyleSetBorder(jlong nativePointer, jint edge, jfloat border) { makeCriticalNativeMethod_DO_NOT_USE_OR_YOU_WILL_BE_FIRED(#name, name) jint JNI_OnLoad(JavaVM* vm, void*) { - return initialize(vm, [] { + jint ret = initialize(vm, [] { registerNatives( "com/facebook/yoga/YogaNative", { @@ -993,4 +994,6 @@ jint JNI_OnLoad(JavaVM* vm, void*) { jni_YGConfigSetShouldDiffLayoutWithoutLegacyStretchBehaviour), }); }); + YGJNIVanilla::registerNatives(Environment::current()); + return ret; } diff --git a/ReactAndroid/src/main/jni/first-party/yogajni/jni/YGJNIVanilla.cpp b/ReactAndroid/src/main/jni/first-party/yogajni/jni/YGJNIVanilla.cpp new file mode 100644 index 00000000000..d169d0e5073 --- /dev/null +++ b/ReactAndroid/src/main/jni/first-party/yogajni/jni/YGJNIVanilla.cpp @@ -0,0 +1,55 @@ +/* + * Copyright (c) Facebook, Inc. and its affiliates. + * + * This source code is licensed under the MIT license found in the LICENSE + * file in the root directory of this source tree. + */ +#include "jni.h" +#include "YGJNIVanilla.h" +#include + +static inline YGNodeRef _jlong2YGNodeRef(jlong addr) { + return reinterpret_cast(static_cast(addr)); +} + +void jni_YGNodeStyleSetFlexJNI( + JNIEnv* env, + jobject obj, + jlong nativePointer, + jfloat value) { + YGNodeStyleSetFlex( + _jlong2YGNodeRef(nativePointer), static_cast(value)); +} + +void assertNoPendingJniException(JNIEnv* env) { + // This method cannot call any other method of the library, since other + // methods of the library use it to check for exceptions too + if (env->ExceptionCheck()) { + env->ExceptionDescribe(); + } +} + +void registerNativeMethods( + JNIEnv* env, + const char* className, + JNINativeMethod methods[], + size_t numMethods) { + jclass clazz = env->FindClass(className); + + assertNoPendingJniException(env); + + env->RegisterNatives(clazz, methods, numMethods); + + assertNoPendingJniException(env); +} + +static JNINativeMethod methods[] = { + {"jni_YGNodeStyleSetFlexJNI", "(JF)V", (void*) jni_YGNodeStyleSetFlexJNI}}; + +void YGJNIVanilla::registerNatives(JNIEnv* env) { + registerNativeMethods( + env, + "com/facebook/yoga/YogaNative", + methods, + sizeof(methods) / sizeof(JNINativeMethod)); +} diff --git a/ReactAndroid/src/main/jni/first-party/yogajni/jni/YGJNIVanilla.h b/ReactAndroid/src/main/jni/first-party/yogajni/jni/YGJNIVanilla.h new file mode 100644 index 00000000000..7797a67857a --- /dev/null +++ b/ReactAndroid/src/main/jni/first-party/yogajni/jni/YGJNIVanilla.h @@ -0,0 +1,9 @@ +/* + * Copyright (c) Facebook, Inc. and its affiliates. + * + * This source code is licensed under the MIT license found in the LICENSE + * file in the root directory of this source tree. + */ +namespace YGJNIVanilla { +void registerNatives(JNIEnv* env); +};