From ecfba1f05ab1b39f9352f4ab7d63a17f6835d8b3 Mon Sep 17 00:00:00 2001 From: Ryan Tremblay Date: Tue, 12 May 2020 17:35:37 -0700 Subject: [PATCH 01/13] Add support for consuming JSI and passing it down to Babylon Native --- .../react-native-babylon/android/CMakeLists.txt | 15 +++++++++++---- .../react-native-babylon/android/build.gradle | 3 ++- .../android/src/main/cpp/BabylonNativeInterop.cpp | 7 ++++--- .../com/reactlibrary/BabylonNativeInterop.java | 6 +++--- .../react-native-babylon/submodules/BabylonNative | 2 +- 5 files changed, 21 insertions(+), 12 deletions(-) diff --git a/Apps/Playground/node_modules/react-native-babylon/android/CMakeLists.txt b/Apps/Playground/node_modules/react-native-babylon/android/CMakeLists.txt index 57c02abee..2309bcad6 100644 --- a/Apps/Playground/node_modules/react-native-babylon/android/CMakeLists.txt +++ b/Apps/Playground/node_modules/react-native-babylon/android/CMakeLists.txt @@ -25,10 +25,16 @@ set(CMAKE_CXX_EXTENSIONS OFF) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17") # configure Babylon Native to use JSC (specifically the one that is part of the React Native application) -set(NAPI_JAVASCRIPT_ENGINE "JavaScriptCore" CACHE STRING "The JavaScript engine to power N-API") -add_library(javascript_engine SHARED IMPORTED GLOBAL) -set_target_properties(javascript_engine PROPERTIES IMPORTED_LOCATION "${ANDROID_JSC_LIBPATH}/${ANDROID_ABI}/libjsc.so") -target_include_directories(javascript_engine INTERFACE "${ANDROID_JSC_INCPATH}") +# set(NAPI_JAVASCRIPT_ENGINE "JavaScriptCore" CACHE STRING "The JavaScript engine to power N-API") +# add_library(javascript_engine SHARED IMPORTED GLOBAL) +# set_target_properties(javascript_engine PROPERTIES IMPORTED_LOCATION "${ANDROID_JSC_LIBPATH}/${ANDROID_ABI}/libjsc.so") +# target_include_directories(javascript_engine INTERFACE "${ANDROID_JSC_INCPATH}") +set(NAPI_JAVASCRIPT_ENGINE "JavaScriptInterface" CACHE STRING "The JavaScript engine to power N-API") +add_subdirectory(${REACTNATIVE_DIR}/ReactCommon/jsi/jsi ${CMAKE_CURRENT_BINARY_DIR}/jsi) +target_include_directories(jsi INTERFACE ${REACTNATIVE_DIR}/ReactCommon/jsi) +add_library(javascript_engine INTERFACE) +target_link_libraries(javascript_engine INTERFACE jsi) +#target_link_to_dependencies(javascript_engine INTERFACE jsi) list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/src/") @@ -47,6 +53,7 @@ target_link_libraries(BabylonNative log -lz arcana + jsi JsRuntime NativeWindow NativeEngine diff --git a/Apps/Playground/node_modules/react-native-babylon/android/build.gradle b/Apps/Playground/node_modules/react-native-babylon/android/build.gradle index 9ad424f72..606ba2a1c 100644 --- a/Apps/Playground/node_modules/react-native-babylon/android/build.gradle +++ b/Apps/Playground/node_modules/react-native-babylon/android/build.gradle @@ -64,7 +64,8 @@ android { '-DSPIRV_CROSS_CLI=OFF', "-DANDROID_JSC_LIBPATH=${extractedLibDir}/jni", "-DANDROID_JSC_INCPATH=${jscIncDir}", - "-DARCORE_LIBPATH=${extractedLibDir}/jni" + "-DARCORE_LIBPATH=${extractedLibDir}/jni", + "-DREACTNATIVE_DIR=${rootDir}/../node_modules/react-native/" } } ndk { diff --git a/Apps/Playground/node_modules/react-native-babylon/android/src/main/cpp/BabylonNativeInterop.cpp b/Apps/Playground/node_modules/react-native-babylon/android/src/main/cpp/BabylonNativeInterop.cpp index cc99c5f21..9c4c3064c 100644 --- a/Apps/Playground/node_modules/react-native-babylon/android/src/main/cpp/BabylonNativeInterop.cpp +++ b/Apps/Playground/node_modules/react-native-babylon/android/src/main/cpp/BabylonNativeInterop.cpp @@ -8,7 +8,8 @@ #include -#include +//#include +#include #include #include @@ -32,7 +33,7 @@ namespace Babylon { public: // This class must be constructed from the JavaScript thread - Native(JSGlobalContextRef jsContext, ANativeWindow* windowPtr) + Native(facebook::jsi::Runtime* jsContext, ANativeWindow* windowPtr) : m_env{ Napi::Attach(jsContext) } { auto looper_scheduler = std::make_shared(looper_scheduler_t::get_for_current_thread()); @@ -100,7 +101,7 @@ namespace Babylon extern "C" JNIEXPORT jlong JNICALL Java_com_reactlibrary_BabylonNativeInterop_create(JNIEnv* env, jclass obj, jlong jsContextRef, jobject surface) { - auto jsContext = reinterpret_cast(jsContextRef); + auto jsContext = reinterpret_cast(jsContextRef); ANativeWindow* windowPtr = ANativeWindow_fromSurface(env, surface); auto native = new Babylon::Native(jsContext, windowPtr); return reinterpret_cast(native); diff --git a/Apps/Playground/node_modules/react-native-babylon/android/src/main/java/com/reactlibrary/BabylonNativeInterop.java b/Apps/Playground/node_modules/react-native-babylon/android/src/main/java/com/reactlibrary/BabylonNativeInterop.java index bb5fecf91..bd2d6bd48 100644 --- a/Apps/Playground/node_modules/react-native-babylon/android/src/main/java/com/reactlibrary/BabylonNativeInterop.java +++ b/Apps/Playground/node_modules/react-native-babylon/android/src/main/java/com/reactlibrary/BabylonNativeInterop.java @@ -43,10 +43,10 @@ static void setView(ReactContext reactContext, Surface surface) { if (jsContextHandle == 0) { instanceRefFuture.complete(0L); } else { - GetJsGlobalContextRef getJsGlobalContextRef = new GetJsGlobalContextRef(); - long jsContextRef = getJsGlobalContextRef.GetJsGlobalContextRef(jsContextHandle); + //GetJsGlobalContextRef getJsGlobalContextRef = new GetJsGlobalContextRef(); + //long jsContextRef = getJsGlobalContextRef.GetJsGlobalContextRef(jsContextHandle); - instanceRef = BabylonNativeInterop.create(jsContextRef, surface); + instanceRef = BabylonNativeInterop.create(jsContextHandle, surface); final long finalInstanceRef = instanceRef; reactContext.addLifecycleEventListener(new LifecycleEventListener() { diff --git a/Apps/Playground/node_modules/react-native-babylon/submodules/BabylonNative b/Apps/Playground/node_modules/react-native-babylon/submodules/BabylonNative index f6e52e3a5..c8c7a1595 160000 --- a/Apps/Playground/node_modules/react-native-babylon/submodules/BabylonNative +++ b/Apps/Playground/node_modules/react-native-babylon/submodules/BabylonNative @@ -1 +1 @@ -Subproject commit f6e52e3a51177e7d7ef33ef4ab97e4bd81822d0e +Subproject commit c8c7a1595e490924bddd76c41d44d8ab547cf45a From 80affefe2b941b24604be3910d67b477633188d9 Mon Sep 17 00:00:00 2001 From: Gary Hsu Date: Mon, 15 Jun 2020 15:58:38 -0700 Subject: [PATCH 02/13] Test changes for napi-jsi --- Apps/Playground/App.tsx | 51 +- .../android/CMakeLists.txt | 20 +- .../src/main/cpp/BabylonNativeInterop.cpp | 86 +- .../android/src/main/cpp/JSCRuntime.cpp | 1409 +++++++++++++++++ .../android/src/main/cpp/JSCRuntime.h | 21 + .../reactlibrary/BabylonNativeInterop.java | 8 +- .../submodules/BabylonNative | 2 +- 7 files changed, 1556 insertions(+), 41 deletions(-) create mode 100644 Apps/Playground/node_modules/react-native-babylon/android/src/main/cpp/JSCRuntime.cpp create mode 100644 Apps/Playground/node_modules/react-native-babylon/android/src/main/cpp/JSCRuntime.h diff --git a/Apps/Playground/App.tsx b/Apps/Playground/App.tsx index 6b00611fb..d1a3fa484 100644 --- a/Apps/Playground/App.tsx +++ b/Apps/Playground/App.tsx @@ -9,7 +9,7 @@ import React, { useState, FunctionComponent, useEffect } from 'react'; import { SafeAreaView, StatusBar, Button, View, Text, ViewProps } from 'react-native'; import { EngineView, useEngine } from 'react-native-babylon'; -import { Scene, Vector3, Mesh, ArcRotateCamera, Engine, Camera, PBRMetallicRoughnessMaterial, Color3 } from '@babylonjs/core'; +import { Scene, Vector3, Mesh, ArcRotateCamera, Engine, Camera, PBRMetallicRoughnessMaterial, Color3, TransformNode } from '@babylonjs/core'; import Slider from '@react-native-community/slider'; const EngineScreen: FunctionComponent = (props: ViewProps) => { @@ -24,22 +24,45 @@ const EngineScreen: FunctionComponent = (props: ViewProps) => { useEffect(() => { if (engine) { const scene = new Scene(engine); - scene.createDefaultCamera(true); - (scene.activeCamera as ArcRotateCamera).beta -= Math.PI / 8; - setCamera(scene.activeCamera!); - scene.createDefaultLight(true); - const box = Mesh.CreateBox("box", 0.3, scene); - setBox(box); - const mat = new PBRMetallicRoughnessMaterial("mat", scene); - mat.metallic = 1; - mat.roughness = 0.5; - mat.baseColor = Color3.Red(); - box.material = mat; + var root = new TransformNode("root", scene); + + var size = 2; + for (var i = -size; i <= size; i++) { + for (var j = -size; j <= size; j++) { + for (var k = -size; k <= size; k++) { + var sphere = Mesh.CreateSphere("sphere" + i + j + k, 32, 0.9, scene); + sphere.position.x = i; + sphere.position.y = j; + sphere.position.z = k; + sphere.parent = root; + } + } + } scene.beforeRender = function () { - box.rotate(Vector3.Up(), 0.005 * scene.getAnimationRatio()); + root.rotate(Vector3.Up(), 0.005 * scene.getAnimationRatio()); }; + + scene.createDefaultCamera(true); + setCamera(scene.activeCamera!); + scene.createDefaultLight(); + + // (scene.activeCamera as ArcRotateCamera).beta -= Math.PI / 8; + // setCamera(scene.activeCamera!); + // scene.createDefaultLight(true); + + // const box = Mesh.CreateBox("box", 0.3, scene); + // setBox(box); + // const mat = new PBRMetallicRoughnessMaterial("mat", scene); + // mat.metallic = 1; + // mat.roughness = 0.5; + // mat.baseColor = Color3.Red(); + // box.material = mat; + + // scene.beforeRender = function () { + // box.rotate(Vector3.Up(), 0.005 * scene.getAnimationRatio()); + // }; } }, [engine]); @@ -55,7 +78,7 @@ const EngineScreen: FunctionComponent = (props: ViewProps) => {