Remove JNI Binding usage of layoutContext (#1377)
Summary: X-link: https://github.com/facebook/react-native/pull/39402 Pull Request resolved: https://github.com/facebook/yoga/pull/1377 To avoid keeping a per-node mapping on native Yoga nodes to Java nodes, a per-layout context was added, to be able to pass information from the start of the layout, to measure functions, log functions, etc. The way this was done was super invasive, and added quite a few private APIs used only by the JNI functions. This change removes the context-using functions from the JNI bindings in favor of it managing its own context. Next diff removes all the cruft. Reviewed By: javache Differential Revision: D49179243 fbshipit-source-id: 7e4944bead864e6b73fd2208a47c5725c18ff2b0
This commit is contained in:
committed by
Facebook GitHub Bot
parent
700be8c8ad
commit
b1e0140aaa
34
java/jni/LayoutContext.cpp
Normal file
34
java/jni/LayoutContext.cpp
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
||||||
|
*
|
||||||
|
* This source code is licensed under the MIT license found in the
|
||||||
|
* LICENSE file in the root directory of this source tree.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stack>
|
||||||
|
|
||||||
|
#include "LayoutContext.h"
|
||||||
|
|
||||||
|
namespace facebook::yoga::vanillajni {
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
std::stack<PtrJNodeMapVanilla*>& getContexts() {
|
||||||
|
static thread_local std::stack<PtrJNodeMapVanilla*> contexts;
|
||||||
|
return contexts;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
LayoutContext::Provider::Provider(PtrJNodeMapVanilla* data) {
|
||||||
|
getContexts().push(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
LayoutContext::Provider::~Provider() {
|
||||||
|
getContexts().pop();
|
||||||
|
}
|
||||||
|
|
||||||
|
/*static*/ PtrJNodeMapVanilla* LayoutContext::getNodeMap() {
|
||||||
|
return getContexts().empty() ? nullptr : getContexts().top();
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace facebook::yoga::vanillajni
|
29
java/jni/LayoutContext.h
Normal file
29
java/jni/LayoutContext.h
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
||||||
|
*
|
||||||
|
* This source code is licensed under the MIT license found in the
|
||||||
|
* LICENSE file in the root directory of this source tree.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <yoga/Yoga.h>
|
||||||
|
#include "YGJTypesVanilla.h"
|
||||||
|
|
||||||
|
namespace facebook::yoga::vanillajni {
|
||||||
|
|
||||||
|
// TODO: This should not be exported or used outside of the JNI bindings
|
||||||
|
class YG_EXPORT LayoutContext {
|
||||||
|
public:
|
||||||
|
// Sets a context on the current thread for the duration of the Provider's
|
||||||
|
// lifetime. This context should be set during the layout process to allow
|
||||||
|
// layout callbacks to access context-data specific to the layout pass.
|
||||||
|
struct Provider {
|
||||||
|
explicit Provider(PtrJNodeMapVanilla* data);
|
||||||
|
~Provider();
|
||||||
|
};
|
||||||
|
|
||||||
|
static PtrJNodeMapVanilla* getNodeMap();
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace facebook::yoga::vanillajni
|
@@ -14,22 +14,17 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include "YogaJniException.h"
|
#include "YogaJniException.h"
|
||||||
|
#include "LayoutContext.h"
|
||||||
|
|
||||||
#include <yoga/Yoga-internal.h>
|
#include <yoga/Yoga-internal.h>
|
||||||
#include <yoga/bits/BitCast.h>
|
#include <yoga/bits/BitCast.h>
|
||||||
|
|
||||||
// TODO: Reconcile missing layoutContext functionality from callbacks in the C
|
|
||||||
// API and use that
|
|
||||||
#include <yoga/node/Node.h>
|
|
||||||
|
|
||||||
using namespace facebook;
|
using namespace facebook;
|
||||||
using namespace facebook::yoga;
|
using namespace facebook::yoga;
|
||||||
using namespace facebook::yoga::vanillajni;
|
using namespace facebook::yoga::vanillajni;
|
||||||
|
|
||||||
static inline ScopedLocalRef<jobject> YGNodeJobject(
|
static inline ScopedLocalRef<jobject> YGNodeJobject(YGNodeConstRef node) {
|
||||||
YGNodeConstRef node,
|
return LayoutContext::getNodeMap()->ref(node);
|
||||||
void* layoutContext) {
|
|
||||||
return reinterpret_cast<PtrJNodeMapVanilla*>(layoutContext)->ref(node);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline YGNodeRef _jlong2YGNodeRef(jlong addr) {
|
static inline YGNodeRef _jlong2YGNodeRef(jlong addr) {
|
||||||
@@ -129,7 +124,6 @@ static int YGJNILogFunc(
|
|||||||
const YGConfigConstRef config,
|
const YGConfigConstRef config,
|
||||||
const YGNodeConstRef /*node*/,
|
const YGNodeConstRef /*node*/,
|
||||||
YGLogLevel level,
|
YGLogLevel level,
|
||||||
void* /*layoutContext*/,
|
|
||||||
const char* format,
|
const char* format,
|
||||||
va_list args) {
|
va_list args) {
|
||||||
va_list argsCopy;
|
va_list argsCopy;
|
||||||
@@ -187,7 +181,7 @@ static void jni_YGConfigSetLoggerJNI(
|
|||||||
}
|
}
|
||||||
|
|
||||||
*context = newGlobalRef(env, logger);
|
*context = newGlobalRef(env, logger);
|
||||||
static_cast<yoga::Config*>(config)->setLogger(YGJNILogFunc);
|
YGConfigSetLogger(config, YGJNILogFunc);
|
||||||
} else {
|
} else {
|
||||||
if (context != nullptr) {
|
if (context != nullptr) {
|
||||||
delete context;
|
delete context;
|
||||||
@@ -278,12 +272,11 @@ static void jni_YGNodeRemoveChildJNI(
|
|||||||
static void YGTransferLayoutOutputsRecursive(
|
static void YGTransferLayoutOutputsRecursive(
|
||||||
JNIEnv* env,
|
JNIEnv* env,
|
||||||
jobject thiz,
|
jobject thiz,
|
||||||
YGNodeRef root,
|
YGNodeRef root) {
|
||||||
void* layoutContext) {
|
|
||||||
if (!YGNodeGetHasNewLayout(root)) {
|
if (!YGNodeGetHasNewLayout(root)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
auto obj = YGNodeJobject(root, layoutContext);
|
auto obj = YGNodeJobject(root);
|
||||||
if (!obj) {
|
if (!obj) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -351,8 +344,7 @@ static void YGTransferLayoutOutputsRecursive(
|
|||||||
YGNodeSetHasNewLayout(root, false);
|
YGNodeSetHasNewLayout(root, false);
|
||||||
|
|
||||||
for (size_t i = 0; i < YGNodeGetChildCount(root); i++) {
|
for (size_t i = 0; i < YGNodeGetChildCount(root); i++) {
|
||||||
YGTransferLayoutOutputsRecursive(
|
YGTransferLayoutOutputsRecursive(env, thiz, YGNodeGetChild(root, i));
|
||||||
env, thiz, YGNodeGetChild(root, i), layoutContext);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -366,21 +358,22 @@ static void jni_YGNodeCalculateLayoutJNI(
|
|||||||
jobjectArray javaNodes) {
|
jobjectArray javaNodes) {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
void* layoutContext = nullptr;
|
PtrJNodeMapVanilla* layoutContext = nullptr;
|
||||||
auto map = PtrJNodeMapVanilla{};
|
auto map = PtrJNodeMapVanilla{};
|
||||||
if (nativePointers) {
|
if (nativePointers) {
|
||||||
map = PtrJNodeMapVanilla{nativePointers, javaNodes};
|
map = PtrJNodeMapVanilla{nativePointers, javaNodes};
|
||||||
layoutContext = ↦
|
layoutContext = ↦
|
||||||
}
|
}
|
||||||
|
|
||||||
|
LayoutContext::Provider contextProvider(layoutContext);
|
||||||
|
|
||||||
const YGNodeRef root = _jlong2YGNodeRef(nativePointer);
|
const YGNodeRef root = _jlong2YGNodeRef(nativePointer);
|
||||||
YGNodeCalculateLayoutWithContext(
|
YGNodeCalculateLayout(
|
||||||
root,
|
root,
|
||||||
static_cast<float>(width),
|
static_cast<float>(width),
|
||||||
static_cast<float>(height),
|
static_cast<float>(height),
|
||||||
YGNodeStyleGetDirection(_jlong2YGNodeRef(nativePointer)),
|
YGNodeStyleGetDirection(_jlong2YGNodeRef(nativePointer)));
|
||||||
layoutContext);
|
YGTransferLayoutOutputsRecursive(env, obj, root);
|
||||||
YGTransferLayoutOutputsRecursive(env, obj, root, layoutContext);
|
|
||||||
} catch (const YogaJniException& jniException) {
|
} catch (const YogaJniException& jniException) {
|
||||||
ScopedLocalRef<jthrowable> throwable = jniException.getThrowable();
|
ScopedLocalRef<jthrowable> throwable = jniException.getThrowable();
|
||||||
if (throwable.get()) {
|
if (throwable.get()) {
|
||||||
@@ -647,9 +640,8 @@ static YGSize YGJNIMeasureFunc(
|
|||||||
float width,
|
float width,
|
||||||
YGMeasureMode widthMode,
|
YGMeasureMode widthMode,
|
||||||
float height,
|
float height,
|
||||||
YGMeasureMode heightMode,
|
YGMeasureMode heightMode) {
|
||||||
void* layoutContext) {
|
if (auto obj = YGNodeJobject(node)) {
|
||||||
if (auto obj = YGNodeJobject(node, layoutContext)) {
|
|
||||||
YGTransferLayoutDirection(node, obj.get());
|
YGTransferLayoutDirection(node, obj.get());
|
||||||
JNIEnv* env = getCurrentEnv();
|
JNIEnv* env = getCurrentEnv();
|
||||||
auto objectClass = facebook::yoga::vanillajni::make_local_ref(
|
auto objectClass = facebook::yoga::vanillajni::make_local_ref(
|
||||||
@@ -683,16 +675,13 @@ static void jni_YGNodeSetHasMeasureFuncJNI(
|
|||||||
jobject /*obj*/,
|
jobject /*obj*/,
|
||||||
jlong nativePointer,
|
jlong nativePointer,
|
||||||
jboolean hasMeasureFunc) {
|
jboolean hasMeasureFunc) {
|
||||||
static_cast<yoga::Node*>(_jlong2YGNodeRef(nativePointer))
|
YGNodeSetMeasureFunc(
|
||||||
->setMeasureFunc(hasMeasureFunc ? YGJNIMeasureFunc : nullptr);
|
_jlong2YGNodeRef(nativePointer),
|
||||||
|
hasMeasureFunc ? YGJNIMeasureFunc : nullptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
static float YGJNIBaselineFunc(
|
static float YGJNIBaselineFunc(YGNodeConstRef node, float width, float height) {
|
||||||
YGNodeConstRef node,
|
if (auto obj = YGNodeJobject(node)) {
|
||||||
float width,
|
|
||||||
float height,
|
|
||||||
void* layoutContext) {
|
|
||||||
if (auto obj = YGNodeJobject(node, layoutContext)) {
|
|
||||||
JNIEnv* env = getCurrentEnv();
|
JNIEnv* env = getCurrentEnv();
|
||||||
auto objectClass = facebook::yoga::vanillajni::make_local_ref(
|
auto objectClass = facebook::yoga::vanillajni::make_local_ref(
|
||||||
env, env->GetObjectClass(obj.get()));
|
env, env->GetObjectClass(obj.get()));
|
||||||
@@ -710,8 +699,9 @@ static void jni_YGNodeSetHasBaselineFuncJNI(
|
|||||||
jobject /*obj*/,
|
jobject /*obj*/,
|
||||||
jlong nativePointer,
|
jlong nativePointer,
|
||||||
jboolean hasBaselineFunc) {
|
jboolean hasBaselineFunc) {
|
||||||
static_cast<yoga::Node*>(_jlong2YGNodeRef(nativePointer))
|
YGNodeSetBaselineFunc(
|
||||||
->setBaselineFunc(hasBaselineFunc ? YGJNIBaselineFunc : nullptr);
|
_jlong2YGNodeRef(nativePointer),
|
||||||
|
hasBaselineFunc ? YGJNIBaselineFunc : nullptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void jni_YGNodePrintJNI(
|
static void jni_YGNodePrintJNI(
|
||||||
|
@@ -5,6 +5,8 @@
|
|||||||
* LICENSE file in the root directory of this source tree.
|
* LICENSE file in the root directory of this source tree.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user