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
This commit is contained in:
Sidharth Guglani
2019-09-26 17:30:28 -07:00
committed by Facebook Github Bot
parent f00116c3a6
commit b29e144649
5 changed files with 73 additions and 2 deletions

View File

@@ -29,7 +29,7 @@ add_compile_options(
-Wall -Wall
-std=c++11) -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 target_include_directories(yoga PRIVATE
${libfb_DIR}/include ${libfb_DIR}/include

View File

@@ -111,4 +111,8 @@ public class YogaNative {
static native void jni_YGNodePrint(long nativePointer); static native void jni_YGNodePrint(long nativePointer);
static native void jni_YGNodeSetStyleInputs(long nativePointer, float[] styleInputsArray, int size); static native void jni_YGNodeSetStyleInputs(long nativePointer, float[] styleInputsArray, int size);
static native long jni_YGNodeClone(long nativePointer); static native long jni_YGNodeClone(long nativePointer);
// JNI methods that use Vanilla JNI
public static native void jni_YGNodeStyleSetFlexJNI(long nativePointer, float flex);
} }

View File

@@ -15,6 +15,7 @@
#include <map> #include <map>
#include "YGJTypes.h" #include "YGJTypes.h"
#include "YGJNIVanilla.h"
using namespace facebook::jni; using namespace facebook::jni;
using namespace std; 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) makeCriticalNativeMethod_DO_NOT_USE_OR_YOU_WILL_BE_FIRED(#name, name)
jint JNI_OnLoad(JavaVM* vm, void*) { jint JNI_OnLoad(JavaVM* vm, void*) {
return initialize(vm, [] { jint ret = initialize(vm, [] {
registerNatives( registerNatives(
"com/facebook/yoga/YogaNative", "com/facebook/yoga/YogaNative",
{ {
@@ -993,4 +994,6 @@ jint JNI_OnLoad(JavaVM* vm, void*) {
jni_YGConfigSetShouldDiffLayoutWithoutLegacyStretchBehaviour), jni_YGConfigSetShouldDiffLayoutWithoutLegacyStretchBehaviour),
}); });
}); });
YGJNIVanilla::registerNatives(Environment::current());
return ret;
} }

55
java/jni/YGJNIVanilla.cpp Normal file
View File

@@ -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 <yoga/YGNode.h>
static inline YGNodeRef _jlong2YGNodeRef(jlong addr) {
return reinterpret_cast<YGNodeRef>(static_cast<intptr_t>(addr));
}
void jni_YGNodeStyleSetFlexJNI(
JNIEnv* env,
jobject obj,
jlong nativePointer,
jfloat value) {
YGNodeStyleSetFlex(
_jlong2YGNodeRef(nativePointer), static_cast<float>(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));
}

9
java/jni/YGJNIVanilla.h Normal file
View File

@@ -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);
};