Skip to content

Commit

Permalink
Remove JNI Binding usage of layoutContext (facebook#1377)
Browse files Browse the repository at this point in the history
Summary:
X-link: facebook/react-native#39402

Pull Request resolved: facebook#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.

Differential Revision: D49179243

fbshipit-source-id: 802b9b1f1ebbb56dd4f6e4d91e9b8f2bddc2312c
  • Loading branch information
NickGerleman authored and facebook-github-bot committed Sep 12, 2023
1 parent c03d908 commit 5acf575
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 33 deletions.
14 changes: 14 additions & 0 deletions java/jni/LayoutContext.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* 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 "LayoutContext.h"

namespace facebook::yoga::vanillajni {

thread_local std::stack<PtrJNodeMapVanilla*> LayoutContext::contexts_;

} // namespace facebook::yoga::vanillajni
32 changes: 32 additions & 0 deletions java/jni/LayoutContext.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* 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 <stack>
#include "YGJTypesVanilla.h"

namespace facebook::yoga::vanillajni {

class LayoutContext {
public:
struct Holder {
explicit Holder(PtrJNodeMapVanilla* data) {
LayoutContext::contexts_.push(data);
}
~Holder() { LayoutContext::contexts_.pop(); }
};

static PtrJNodeMapVanilla* getCurrent() {
return contexts_.empty() ? nullptr : contexts_.top();
}

private:
static thread_local std::stack<PtrJNodeMapVanilla*> contexts_;
};

} // namespace facebook::yoga::vanillajni
56 changes: 23 additions & 33 deletions java/jni/YGJNIVanilla.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,17 @@
#include <iostream>
#include <memory>
#include "YogaJniException.h"
#include "LayoutContext.h"

#include <yoga/Yoga-internal.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::yoga;
using namespace facebook::yoga::vanillajni;

static inline ScopedLocalRef<jobject> YGNodeJobject(
YGNodeConstRef node,
void* layoutContext) {
return reinterpret_cast<PtrJNodeMapVanilla*>(layoutContext)->ref(node);
static inline ScopedLocalRef<jobject> YGNodeJobject(YGNodeConstRef node) {
return LayoutContext::getCurrent()->ref(node);
}

static inline YGNodeRef _jlong2YGNodeRef(jlong addr) {
Expand Down Expand Up @@ -129,7 +124,6 @@ static int YGJNILogFunc(
const YGConfigConstRef config,
const YGNodeConstRef /*node*/,
YGLogLevel level,
void* /*layoutContext*/,
const char* format,
va_list args) {
va_list argsCopy;
Expand Down Expand Up @@ -187,7 +181,7 @@ static void jni_YGConfigSetLoggerJNI(
}

*context = newGlobalRef(env, logger);
static_cast<yoga::Config*>(config)->setLogger(YGJNILogFunc);
YGConfigSetLogger(config, YGJNILogFunc);
} else {
if (context != nullptr) {
delete context;
Expand Down Expand Up @@ -278,12 +272,11 @@ static void jni_YGNodeRemoveChildJNI(
static void YGTransferLayoutOutputsRecursive(
JNIEnv* env,
jobject thiz,
YGNodeRef root,
void* layoutContext) {
YGNodeRef root) {
if (!YGNodeGetHasNewLayout(root)) {
return;
}
auto obj = YGNodeJobject(root, layoutContext);
auto obj = YGNodeJobject(root);
if (!obj) {
return;
}
Expand Down Expand Up @@ -351,8 +344,7 @@ static void YGTransferLayoutOutputsRecursive(
YGNodeSetHasNewLayout(root, false);

for (size_t i = 0; i < YGNodeGetChildCount(root); i++) {
YGTransferLayoutOutputsRecursive(
env, thiz, YGNodeGetChild(root, i), layoutContext);
YGTransferLayoutOutputsRecursive(env, thiz, YGNodeGetChild(root, i));
}
}

Expand All @@ -366,21 +358,22 @@ static void jni_YGNodeCalculateLayoutJNI(
jobjectArray javaNodes) {

try {
void* layoutContext = nullptr;
PtrJNodeMapVanilla* layoutContext = nullptr;
auto map = PtrJNodeMapVanilla{};
if (nativePointers) {
map = PtrJNodeMapVanilla{nativePointers, javaNodes};
layoutContext = &map;
}

LayoutContext::Holder holder(layoutContext);

const YGNodeRef root = _jlong2YGNodeRef(nativePointer);
YGNodeCalculateLayoutWithContext(
YGNodeCalculateLayout(
root,
static_cast<float>(width),
static_cast<float>(height),
YGNodeStyleGetDirection(_jlong2YGNodeRef(nativePointer)),
layoutContext);
YGTransferLayoutOutputsRecursive(env, obj, root, layoutContext);
YGNodeStyleGetDirection(_jlong2YGNodeRef(nativePointer)));
YGTransferLayoutOutputsRecursive(env, obj, root);
} catch (const YogaJniException& jniException) {
ScopedLocalRef<jthrowable> throwable = jniException.getThrowable();
if (throwable.get()) {
Expand Down Expand Up @@ -647,9 +640,8 @@ static YGSize YGJNIMeasureFunc(
float width,
YGMeasureMode widthMode,
float height,
YGMeasureMode heightMode,
void* layoutContext) {
if (auto obj = YGNodeJobject(node, layoutContext)) {
YGMeasureMode heightMode) {
if (auto obj = YGNodeJobject(node)) {
YGTransferLayoutDirection(node, obj.get());
JNIEnv* env = getCurrentEnv();
auto objectClass = facebook::yoga::vanillajni::make_local_ref(
Expand Down Expand Up @@ -683,16 +675,13 @@ static void jni_YGNodeSetHasMeasureFuncJNI(
jobject /*obj*/,
jlong nativePointer,
jboolean hasMeasureFunc) {
static_cast<yoga::Node*>(_jlong2YGNodeRef(nativePointer))
->setMeasureFunc(hasMeasureFunc ? YGJNIMeasureFunc : nullptr);
YGNodeSetMeasureFunc(
_jlong2YGNodeRef(nativePointer),
hasMeasureFunc ? YGJNIMeasureFunc : nullptr);
}

static float YGJNIBaselineFunc(
YGNodeConstRef node,
float width,
float height,
void* layoutContext) {
if (auto obj = YGNodeJobject(node, layoutContext)) {
static float YGJNIBaselineFunc(YGNodeConstRef node, float width, float height) {
if (auto obj = YGNodeJobject(node)) {
JNIEnv* env = getCurrentEnv();
auto objectClass = facebook::yoga::vanillajni::make_local_ref(
env, env->GetObjectClass(obj.get()));
Expand All @@ -710,8 +699,9 @@ static void jni_YGNodeSetHasBaselineFuncJNI(
jobject /*obj*/,
jlong nativePointer,
jboolean hasBaselineFunc) {
static_cast<yoga::Node*>(_jlong2YGNodeRef(nativePointer))
->setBaselineFunc(hasBaselineFunc ? YGJNIBaselineFunc : nullptr);
YGNodeSetBaselineFunc(
_jlong2YGNodeRef(nativePointer),
hasBaselineFunc ? YGJNIBaselineFunc : nullptr);
}

static void jni_YGNodePrintJNI(
Expand Down
2 changes: 2 additions & 0 deletions java/jni/YGJTypesVanilla.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* LICENSE file in the root directory of this source tree.
*/

#pragma once

#include <map>
#include <vector>

Expand Down

0 comments on commit 5acf575

Please sign in to comment.