Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Per-node pointScaleFactor #1379

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
3 changes: 1 addition & 2 deletions tests/EventsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ struct TypedEventTestData {};

template <>
struct TypedEventTestData<Event::LayoutPassEnd> {
void* layoutContext;
LayoutData layoutData;
};

Expand Down Expand Up @@ -329,7 +328,7 @@ void EventTest::listen(
case Event::LayoutPassEnd: {
auto& eventData = data.get<Event::LayoutPassEnd>();
events.push_back(createArgs<Event::LayoutPassEnd>(
node, data, {eventData.layoutContext, *eventData.layoutData}));
node, data, {*eventData.layoutData}));
break;
}

Expand Down
14 changes: 2 additions & 12 deletions tests/YGConfigTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ TEST_F(ConfigCloningTest, uses_values_provided_by_cloning_callback) {
config->setCloneNodeCallback(cloneNode);

yoga::Node node{}, owner{};
auto clone = config->cloneNode(&node, &owner, 0, nullptr);
auto clone = config->cloneNode(&node, &owner, 0);

ASSERT_EQ(clone, &clonedNode);
}
Expand All @@ -44,22 +44,12 @@ TEST_F(
config->setCloneNodeCallback(doNotClone);

yoga::Node node{}, owner{};
auto clone = config->cloneNode(&node, &owner, 0, nullptr);
auto clone = config->cloneNode(&node, &owner, 0);

ASSERT_NE(clone, nullptr);
YGNodeFree(clone);
}

TEST_F(ConfigCloningTest, can_clone_with_context) {
config->setCloneNodeCallback(
[](YGNodeConstRef, YGNodeConstRef, size_t, void* context) {
return (YGNodeRef) context;
});

yoga::Node node{}, owner{}, clone{};
ASSERT_EQ(config->cloneNode(&node, &owner, 0, &clone), &clone);
}

void ConfigCloningTest::SetUp() {
config = {static_cast<yoga::Config*>(YGConfigNew()), YGConfigFree};
}
Expand Down
71 changes: 2 additions & 69 deletions tests/YGNodeCallbackTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,38 +38,7 @@ TEST(Node, measure_with_measure_fn) {
});

ASSERT_EQ(
n.measure(23, YGMeasureModeExactly, 24, YGMeasureModeAtMost, nullptr),
(YGSize{23, 12}));
}

TEST(Node, measure_with_context_measure_fn) {
auto n = Node{};
n.setMeasureFunc([](YGNodeConstRef,
float,
YGMeasureMode,
float,
YGMeasureMode,
void* ctx) { return *(YGSize*) ctx; });

auto result = YGSize{123.4f, -56.7f};
ASSERT_EQ(
n.measure(0, YGMeasureModeUndefined, 0, YGMeasureModeUndefined, &result),
result);
}

TEST(Node, switching_measure_fn_types) {
auto n = Node{};
n.setMeasureFunc(
[](YGNodeConstRef, float, YGMeasureMode, float, YGMeasureMode, void*) {
return YGSize{};
});
n.setMeasureFunc(
[](YGNodeConstRef, float w, YGMeasureMode wm, float h, YGMeasureMode hm) {
return YGSize{w * static_cast<float>(wm), h / static_cast<float>(hm)};
});

ASSERT_EQ(
n.measure(23, YGMeasureModeExactly, 24, YGMeasureModeAtMost, nullptr),
n.measure(23, YGMeasureModeExactly, 24, YGMeasureModeAtMost),
(YGSize{23, 12}));
}

Expand All @@ -84,17 +53,6 @@ TEST(Node, hasMeasureFunc_after_unset) {
ASSERT_FALSE(n.hasMeasureFunc());
}

TEST(Node, hasMeasureFunc_after_unset_context) {
auto n = Node{};
n.setMeasureFunc(
[](YGNodeConstRef, float, YGMeasureMode, float, YGMeasureMode, void*) {
return YGSize{};
});

n.setMeasureFunc(nullptr);
ASSERT_FALSE(n.hasMeasureFunc());
}

TEST(Node, hasBaselineFunc_initial) {
auto n = Node{};
ASSERT_FALSE(n.hasBaselineFunc());
Expand All @@ -110,17 +68,7 @@ TEST(Node, baseline_with_baseline_fn) {
auto n = Node{};
n.setBaselineFunc([](YGNodeConstRef, float w, float h) { return w + h; });

ASSERT_EQ(n.baseline(1.25f, 2.5f, nullptr), 3.75f);
}

TEST(Node, baseline_with_context_baseline_fn) {
auto n = Node{};
n.setBaselineFunc([](YGNodeConstRef, float w, float h, void* ctx) {
return w + h + *(float*) ctx;
});

auto ctx = -10.0f;
ASSERT_EQ(n.baseline(1.25f, 2.5f, &ctx), -6.25f);
ASSERT_EQ(n.baseline(1.25f, 2.5f), 3.75f);
}

TEST(Node, hasBaselineFunc_after_unset) {
Expand All @@ -130,18 +78,3 @@ TEST(Node, hasBaselineFunc_after_unset) {
n.setBaselineFunc(nullptr);
ASSERT_FALSE(n.hasBaselineFunc());
}

TEST(Node, hasBaselineFunc_after_unset_context) {
auto n = Node{};
n.setBaselineFunc([](YGNodeConstRef, float, float, void*) { return 0.0f; });

n.setMeasureFunc(nullptr);
ASSERT_FALSE(n.hasMeasureFunc());
}

TEST(Node, switching_baseline_fn_types) {
auto n = Node{};
n.setBaselineFunc([](YGNodeConstRef, float, float, void*) { return 0.0f; });
n.setBaselineFunc([](YGNodeConstRef, float, float) { return 1.0f; });
ASSERT_EQ(n.baseline(1, 2, nullptr), 1.0f);
}
42 changes: 42 additions & 0 deletions tests/YGRoundingFunctionTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,45 @@ TEST(YogaTest, consistent_rounding_during_repeated_layouts) {

YGConfigFree(config);
}

TEST(YogaTest, per_node_point_scale_factor) {
const YGConfigRef config1 = YGConfigNew();
YGConfigSetPointScaleFactor(config1, 2);

const YGConfigRef config2 = YGConfigNew();
YGConfigSetPointScaleFactor(config2, 1);

const YGConfigRef config3 = YGConfigNew();
YGConfigSetPointScaleFactor(config3, 0.5f);

const YGNodeRef root = YGNodeNewWithConfig(config1);
YGNodeStyleSetWidth(root, 11.5);
YGNodeStyleSetHeight(root, 11.5);

const YGNodeRef node0 = YGNodeNewWithConfig(config2);
YGNodeStyleSetWidth(node0, 9.5);
YGNodeStyleSetHeight(node0, 9.5);
YGNodeInsertChild(root, node0, 0);

const YGNodeRef node1 = YGNodeNewWithConfig(config3);
YGNodeStyleSetWidth(node1, 7);
YGNodeStyleSetHeight(node1, 7);
YGNodeInsertChild(node0, node1, 0);

YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);

ASSERT_EQ(YGNodeLayoutGetWidth(root), 11.5);
ASSERT_EQ(YGNodeLayoutGetHeight(root), 11.5);

ASSERT_EQ(YGNodeLayoutGetWidth(node0), 10);
ASSERT_EQ(YGNodeLayoutGetHeight(node0), 10);

ASSERT_EQ(YGNodeLayoutGetWidth(node1), 8);
ASSERT_EQ(YGNodeLayoutGetHeight(node1), 8);

YGNodeFreeRecursive(root);

YGConfigFree(config1);
YGConfigFree(config2);
YGConfigFree(config3);
}
Loading
Loading