From b29e144649a2d58af05dcdf14dd3537dc9405608 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/CMakeLists.txt | 2 +- java/com/facebook/yoga/YogaNative.java | 4 ++ java/jni/YGJNI.cpp | 5 ++- java/jni/YGJNIVanilla.cpp | 55 ++++++++++++++++++++++++++ java/jni/YGJNIVanilla.h | 9 +++++ 5 files changed, 73 insertions(+), 2 deletions(-) create mode 100644 java/jni/YGJNIVanilla.cpp create mode 100644 java/jni/YGJNIVanilla.h diff --git a/java/CMakeLists.txt b/java/CMakeLists.txt index 12e69eb0..c55c6bd9 100644 --- a/java/CMakeLists.txt +++ b/java/CMakeLists.txt @@ -29,7 +29,7 @@ add_compile_options( -Wall -std=c++11) -add_library(yoga SHARED jni/YGJNI.cpp jni/YGJTypes.cpp) +add_library(yoga SHARED jni/YGJNI.cpp jni/YGJTypes.cpp jni/YGJNIVanilla.cpp) target_include_directories(yoga PRIVATE ${libfb_DIR}/include diff --git a/java/com/facebook/yoga/YogaNative.java b/java/com/facebook/yoga/YogaNative.java index 5b294898..88595f38 100644 --- a/java/com/facebook/yoga/YogaNative.java +++ b/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/java/jni/YGJNI.cpp b/java/jni/YGJNI.cpp index f23d0f2b..88e325d2 100644 --- a/java/jni/YGJNI.cpp +++ b/java/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/java/jni/YGJNIVanilla.cpp b/java/jni/YGJNIVanilla.cpp new file mode 100644 index 00000000..d169d0e5 --- /dev/null +++ b/java/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/java/jni/YGJNIVanilla.h b/java/jni/YGJNIVanilla.h new file mode 100644 index 00000000..7797a678 --- /dev/null +++ b/java/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); +};