Compare commits

..

41 Commits

Author SHA1 Message Date
David Aurelio
88994129ae 1.12.0-pre.1 (#865)
Summary:
Push version in podspec. We will use `-SNAPSHOT` for bintray
Pull Request resolved: https://github.com/facebook/yoga/pull/865

Differential Revision: D14299474

Pulled By: davidaurelio

fbshipit-source-id: e788479677e1b3c4155a24a336e625508e085f86
2019-03-04 01:34:36 -08:00
David Aurelio
888892885e Encapsulate all used java classes
Summary:
@public

Moves all Java classes used from C++ to their own header + implementation file. In `YGJNI.cpp`, we only call methods on these objects deriving from `jni::JavaClass`.

This is only a bit of cleanup, no functional change.

Reviewed By: SidharthGuglani

Differential Revision: D14261043

fbshipit-source-id: 2db1d81cb7f56a4cdc24b182b2166e1d7e24ba3c
2019-03-04 01:30:15 -08:00
David Aurelio
de79f2a1d4 for experimentation: configurable measure cache size
Summary:
@public

Allows to limit the number of measure cache entries used. This is purely for experimentation.

The measure cache uses about half of every `YGNode`. Reducing its size would allow us to reduce resident memory used by Yoga.

Reviewed By: SidharthGuglani

Differential Revision: D14279027

fbshipit-source-id: e0d22138230bee7fb129c193eb6e4085de02a36e
2019-03-02 12:43:01 -08:00
David Aurelio
6368416178 clean up config context
Summary: `YGConfigContext` was using `global_ref` instances for the config, leading to the config object never being freed. Since we no longer need it after getting rid of cloning, we can make the context a pointer to a `global_ref` to the logger.

Reviewed By: SidharthGuglani

Differential Revision: D14258571

fbshipit-source-id: cce632499839a680eef00a3854f61ab74ae2a87a
2019-03-01 05:45:15 -08:00
David Aurelio
a935a222b5 YGNode remove assignment operators
Summary:
@public

Having assignment operators for `YGNode` means that existing children on the node assigned to would have to be handled somehow.

Deallocating might be incorrect. Ignoring might leak.

Here, we `delete` copy assignment, and make move assignment private (it is used in `YGNode::reset()`).

Copy and move constructors *can* be implemented. The move constructor has to take ownership of the children, while the copy constructor leaves ownership untouched. Since children are copied lazily during layout, this does not expose true value semantics. We should consider removing the copy constructor, too.

Reviewed By: SidharthGuglani

Differential Revision: D14241663

fbshipit-source-id: 39ffdb07f1028bfcf2710c0674a06cdebf3bd650
2019-03-01 04:22:04 -08:00
Sidharth Guglani
7c4da0a341 added positive tests for all style inputs in batching API
Summary: Positive tests for all style input properties using JNI batching API

Reviewed By: davidaurelio

Differential Revision: D14207552

fbshipit-source-id: b7165c2115ace67bf131c76929d37df438921bf2
2019-02-28 12:52:01 -08:00
Sidharth Guglani
b047af25ca Setting flags for margin, padding, position and border
Summary:
We set flags to true when setting margin, padding, border, position individually
Doing the same for batching API

Reviewed By: davidaurelio

Differential Revision: D14207550

fbshipit-source-id: ddfdcd5056bea0dd76bd6762f47e90370e26c9e1
2019-02-28 12:52:01 -08:00
Sidharth Guglani
2abd9dd91b Added getters for flexWrap and flex style inputs in java bindings
Summary: Added getters fro flexWrap an flex style input properties in java bindings

Reviewed By: davidaurelio

Differential Revision: D14207517

fbshipit-source-id: e8d2ad17b5edbdf02e69011c6d95a4de076bd366
2019-02-28 12:52:01 -08:00
Sidharth Guglani
afadc5cf6b JNI batching API using float array
Summary:
Added a new API `YGNodeSetLayoutStyleInputs` to pass layout style inputs from java to native code.
All the style inputs are passed in a float array in [key1, key2, value2, key3, value3a, value3b .....] format over JNI layer.
There are three types of style inputs
- do not need any value to be passed along with them like WidthAuto, HeightAuto
- need one value to be passed like Width, Height
- need two values to be passed like Margin, Padding (edge value and actual margin, padding value)

Reviewed By: davidaurelio

Differential Revision: D14166948

fbshipit-source-id: 4bea64d6a429959c3962c87e337914dcd99199fd
2019-02-28 12:52:01 -08:00
Sidharth Guglani
8823cc357a added mEdgeSetFlags inside YogaNodeContext and using it YGJNI in getters/setters of margin, padding, border
Summary:
We have now create a yoga node context which consists of weak reference to java yoga node object and flag which tells us whether margin, padding , border are set or not.

This flag was initially in java layer and we have moved this to native layer as it will help us with enabling the bacthing API for setting style inputs.

Reviewed By: davidaurelio

Differential Revision: D14243378

fbshipit-source-id: fed935ef18c1abf2b07e5d69d9ca79ced51699f2
2019-02-28 12:52:01 -08:00
Sidharth Guglani
8b8d9a05b7 not setting flags when setting margin, padding, border and position on yoga node
Summary: We are now not setting flags when we set style inputs margin, padding, border and position on yoga node.

Reviewed By: davidaurelio

Differential Revision: D14224000

fbshipit-source-id: deef4c1ab1a60fbc4909183bc2aa59fa23939d43
2019-02-28 12:52:01 -08:00
David Aurelio
15668aceb6 Move reset logic to YGNode::reset()
Summary:
@public

Moving logic from free C functions to the C++ layer.

This will allow us to get rid of the dangerous copy / move assignment operators of `YGNode`.

Reviewed By: SidharthGuglani

Differential Revision: D14241564

fbshipit-source-id: aae9f2a7ffd23bb839f1747e4a0694578bae86ae
2019-02-28 06:31:34 -08:00
David Aurelio
e25fe994b3 Breaking remove YogaNode#clone
Summary:
@public

The cloning features of YogaNode don’t seem to be used. Let’s remove them.

Reviewed By: SidharthGuglani

Differential Revision: D14165624

fbshipit-source-id: 5b710964a4abf1b35f3bcc25b143ffc719a03cec
2019-02-27 04:43:34 -08:00
Chris Sarbora
47abe1c482 Rename makeCriticalNativeMethod to discourage over-use
Summary: "Critical" or "Fast" JNI methods are enticing by their name, but carry dangers that are not trivially visible.

Reviewed By: davidaurelio

Differential Revision: D14184560

fbshipit-source-id: 89ec70f53bb2cb89ff568d8b1fe222ede86c9824
2019-02-22 14:30:35 -08:00
David Aurelio
95169c3150 Don't call jni_YGNodeFree as fast native method
Summary:
@public

If `jni_YGNodeFree` is called while GC is running, the weak reference table lock is held by the GC, leading to deadlock.

Here, we revert the method to being a regular native method, solving that problem.

Reviewed By: SidharthGuglani

Differential Revision: D14184220

fbshipit-source-id: 2882fa10586617cea2df99550a7dd8885376d11e
2019-02-22 08:59:35 -08:00
David Aurelio
7331de78bd setBaseLine -> setBaseline
Summary:
@public

`setBaseLine` was the only place where we used a capitalised *L.* Fixed here.

Reviewed By: SidharthGuglani

Differential Revision: D14152320

fbshipit-source-id: abf54fe7d6088e03775968baa8421c4bf43d6a6e
2019-02-21 05:38:45 -08:00
David Aurelio
05f36a835a Allow to use JNI without global refs
Summary:
@public

Adds the ability to opt into avoiding global weak JNI refs via `YogaConfig`.

Note that only homogeneous trees are supported, i.e. **mixing weak-ref and non-weak-ref nodes will break!**

Not using JNI refs hopefully will help with avoiding JNI reference table overflows, and will help creating trees on multiple threads, as no lock has to be acquired at any time.

Reviewed By: SidharthGuglani

Differential Revision: D14151037

fbshipit-source-id: 56d94713d39aee080d54be4cb4cdf5e3eccb473a
2019-02-21 05:38:45 -08:00
Michael Lee
dcd9438488 Clean up unnecessary header glob
Reviewed By: davidaurelio

Differential Revision: D13509119

fbshipit-source-id: 35d32c82b4fbaf7529828982e69b15e1c61b9e25
2019-02-20 16:53:40 -08:00
David Aurelio
d38db1292c Use YGNodeRemoveAllChildren where appropriate
Summary:
@public

`YGNodeRemoveAllChildren` can be used instead of removing children of a yoga node in a loop.

Reviewed By: zats

Differential Revision: D14131679

fbshipit-source-id: 6ee31f1e288b9b161c641b5bca4f2c1156d58c38
2019-02-20 12:35:27 -08:00
David Aurelio
4463fe0856 Add config for avoiding JNI refs
Summary:
@public

Adds `YogaConfig#avoidGlobalJNIRefs` to control whether nodes created with a config will use weak global JNI refs. Used for experimentation.

Reviewed By: SidharthGuglani

Differential Revision: D14149982

fbshipit-source-id: c777c8b3af2167d96154db5aa6afec1476dac35b
2019-02-20 12:01:15 -08:00
David Aurelio
2156de5fb5 Add support for context-aware cloning functions
Summary:
@public

Context-aware cloning functions are an internal Yoga feature that will be used for Yoga’s JNI code.

It will be possible to specify a context when calculating layout, which will be passed on to cloning functions. This will be a private feature.

Reviewed By: SidharthGuglani

Differential Revision: D14149470

fbshipit-source-id: 1d11106e65f9d872d10f191763da001f8d158a61
2019-02-20 12:01:14 -08:00
David Aurelio
b1c749075d Clone children only during layout, allow mixing shared + owned children
Summary:
@public

Limit child cloning to layout calculation. This also allows for mixing shared and owned children.

Rationale:
We do allow for shared children if the caller manages themselves. The single known use case is React Native.

So far, we have cloned children eagerly whenever child lists are mutated, or layout is run. This was to allow for a quick check of the owner of any first child, assuming that either *all* or *no* child of a node are shared.

For Yoga/Java, we want to get rid of global weak JNI refs, and these are also used to invoke clone callbacks. We can achieve that goal by switching to an alternative approach, passing additional data to the layout pass. This additional data has to be passed to any configured cloning callback. Therefore, it is desirable to **only call cloning functions during the layout pass.**

The obvious solution seems to be to not uphold the invariant of the first child determining shared/owned state of all siblings, and allow for a mix of shared and own children.

Reviewed By: shergin

Differential Revision: D14136223

fbshipit-source-id: 34490cfeeb2170c99d6ed1b9bdcbcedb316813af
2019-02-20 12:01:14 -08:00
David Aurelio
367a93de88 Move node cloning to YGConfig
Summary:
@public

Encapsulates node cloning within `YGConfig`.
This is necessary for allowing for context-aware cloning functions, which will ultimately allow for removal of weak global JNI references.

Reviewed By: shergin

Differential Revision: D14132608

fbshipit-source-id: 0dec114c8e172b1e34a4b7fd146c43f13c151ade
2019-02-20 12:01:14 -08:00
David Aurelio
2643b96589 Extract abstract class YogaNode
Summary:
@public

Here, we extract an abstract class from `YogaNode`, in order to allow for experimentation with different implementations.

The reason for not choosing an interface is to keep ABI compatibility for `YogaNode.create()`.

Reviewed By: pasqualeanatriello

Differential Revision: D14044990

fbshipit-source-id: f8eb246338b55f34f0401198c0655abfcb7c9f37
2019-02-19 11:19:10 -08:00
David Aurelio
016a10df26 Switch to YogaNode.create()
Summary:
@public

Switches instance creation from `new YogaNode()` to `YogaNode.create()`.

This allows for experimentation with different implementations, while maintaining API + ABI compatibility internally at FB, as well as for dependent projects in open source and elsewhere.

Reviewed By: amir-shalem

Differential Revision: D14122975

fbshipit-source-id: f194b146b7cd693dba1a7dafdf92d350e54cb179
2019-02-19 11:19:10 -08:00
David Aurelio
cbcf07f08a Add support for context-aware print functions
Summary:
@public

Context-aware print functions are an internal Yoga feature that will be used for Yoga’s JNI code.

It will be possible to specify a context when calculating layout, which will be passed on to baseline and measure functions. This will be a private feature.

Reviewed By: SidharthGuglani

Differential Revision: D14131098

fbshipit-source-id: 7a9da307274ceccba9f7debba581b70c1ebf2c98
2019-02-19 09:58:43 -08:00
David Aurelio
59bcac3289 Make node printing private
Summary:
@public

Removes `YGNodeGetPrintFunc`, and encapsulates node printing within `YGNode`.
This is necessary for allowing for context-aware callback functions, which will ultimately allow for removal of weak global JNI references.

On a side node, the printing logic does not seem to be well thought through: print functions print as a side effect to whatever output they choose. Printing that uses callbacks is printing to different output streams or strings, though.

We need to consolidate Yoga debugging, and make it all more stringent.

Reviewed By: SidharthGuglani

Differential Revision: D14131024

fbshipit-source-id: 68704682dab3e7dfba61930bb03003d7d4723b80
2019-02-19 09:58:43 -08:00
David Aurelio
bd90192df9 Allow to calculate layout with context
Summary:
publc

Adds the ability to calculate layout with a context pointer set.

The context is passed through to measure and baseline functions of individual nodes.

This will be used to remove the necessity of holding weak global JNI references for each node.

Reviewed By: SidharthGuglani

Differential Revision: D14101426

fbshipit-source-id: 25047e1e44af48feb22ea686285d70803e8961bb
2019-02-19 09:58:42 -08:00
David Aurelio
e7fcf1ee65 Add support for context-aware measure and baseline functions
Summary:
@public

Context-aware measure and baseline functions are an internal Yoga feature that will be used for Yoga’s JNI code.

It will be possible to specify a context when calculating layout, which will be passed on to baseline and measure functions. This will be a private feature.

Reviewed By: SidharthGuglani

Differential Revision: D14100509

fbshipit-source-id: acf4a030549b2e38d5ce0cd5dbe837864e5ffd81
2019-02-19 09:58:42 -08:00
David Aurelio
f86c74ce7e Call measure and baseline fns within YGNode
Summary:
@public

Stricter encapsulation of baseline and measure callbacks withing `YGNode`.

Instead of invoking these callbacks directly (`node->getBaseline()(...)`), they are invoked via methods on `YGNode` (`node->baseline(...)`).

This change will allow us to add the concept of a *Layout Context,* where measure and baseline functions will be able to receive an additional `void *` argument if configured accordingly. This API will be used internally for Yoga’s JNI bindings, to avoid storing a weak JNI reference for each node, and avoid reference table overflows.

Changed API:

- `YGNodeGetMeasureFunc()` -> `YGNodeHasMeasureFunc()`
- `YGNodeGetBaselineFunc()` -> `YGNodeHasBaselineFunc()`
- `YGNode::getMeasure()` -> `YGNode::hasMeasureFunc()` + `YGNode::measure()`
- `YGNpde::getBaseline()` -> `YGNode::hasBaselineFunc()` + `YGNode::baseline()`

Reviewed By: SidharthGuglani

Differential Revision: D14099550

fbshipit-source-id: 2653ab36acc252a9747986bc88d21dac22d8c91b
2019-02-19 09:58:42 -08:00
David Aurelio
d5ad51bccc Add support for context-aware logging functions
Summary:
@public

Context-aware logging functions are an internal Yoga feature that will be used for Yoga’s JNI code.

It will be possible to specify a context when calculating layout, which will be passed on to baseline and measure functions. This will be a private feature.

Reviewed By: SidharthGuglani

Differential Revision: D14123482

fbshipit-source-id: 8ba3b6c493bf79fe09831f22d2b6da44f09e3d95
2019-02-19 09:58:42 -08:00
David Aurelio
0bdf36f5d1 Call logger from within YGConfig
Summary:
@public

Stricter encapsulation of logging callbacks within `YGConfig`.

Instead of invoking the logging callback directly (`node->logger(...)`), callers now have to go through `YGConfig::log()`.

This change will allow us to add the concept of a *Layout Context,* where logging functions will be able to receive an additional `void *` argument if configured accordingly. This API will be used internally for Yoga’s JNI bindings, to avoid storing a weak JNI reference for each node, and avoid reference table overflows.

Changed API:

- `YGConfig::logger()` -> `YGConfig::log()`

Reviewed By: SidharthGuglani

Differential Revision: D14123483

fbshipit-source-id: 87b8bb7de0e4346b6a41e57a70ac4eb8d79b24af
2019-02-19 09:58:42 -08:00
David Aurelio
1b9053bc5d Make logging private
Summary:
@public

Makes logging implementation internal to Yoga.

Breaking changes: removed  `YGLog` and `YGLogWithConfig`.

The upcoming changes to the JNI layer (removal of weak global refs for each node) requires adding additional parameters to the logging functions that will only be available when calculating layout.

Reviewed By: SidharthGuglani

Differential Revision: D14123390

fbshipit-source-id: 468e4a240c190342868ffbb5f8beb92324cdfdd6
2019-02-19 09:58:42 -08:00
Yuichi ONO
446101a168 flexWrap should be kebab-case inside inline-style (#761)
Summary:
Inside css inline style, it should be spelled as `flex-wrap`.
Pull Request resolved: https://github.com/facebook/yoga/pull/761

Reviewed By: davidaurelio

Differential Revision: D14100508

Pulled By: SidharthGuglani

fbshipit-source-id: 65ebc984d959555107d58ad16ca27b2b8b4d0874
2019-02-19 04:56:36 -08:00
Leandro Ostera
f0324035bf Fix small typos (#784)
Summary:
Hello! Just found some typos on the docs. Fixing them here :)
Pull Request resolved: https://github.com/facebook/yoga/pull/784

Reviewed By: davidaurelio

Differential Revision: D14100968

Pulled By: SidharthGuglani

fbshipit-source-id: 1462216a0d5315075871f2fb11b6b1279ae0097a
2019-02-19 04:48:35 -08:00
David Aurelio
a2f94cbf9a Add -SNAPSHOT to version
Summary: Adds the `-SNAPSHOT` specifier back to the version number in `gradle.properties`

Reviewed By: SidharthGuglani

Differential Revision: D14123308

fbshipit-source-id: e5d4c4dd9552557dc42440f0892c1452dd0195a5
2019-02-18 05:35:54 -08:00
David Aurelio
d21fa68033 Bump version to 1.12.0
Summary:
@public

For `YogaNode.create()`

Reviewed By: amir-shalem

Differential Revision: D14122976

fbshipit-source-id: 5e2fe4f2039d700be6eefff2401b4333fdca3dd2
2019-02-18 03:08:14 -08:00
David Aurelio
5a6d84abc9 Add YogaNode.create()
Summary:
Adds a factory method to `YogaNode`.

While this is purely redundant at the moment, it will allow experimentation in follow-up diffs. We will have concrete implementations deriving from `YogaNode` (which will be abstract).

Going through `YogaNode.create()` means that we can maintain ABI compatibility.

Reviewed By: amir-shalem

Differential Revision: D14122974

fbshipit-source-id: 15d92f296d91cc8bbd79a196f370d2dbb69b3f92
2019-02-18 03:08:14 -08:00
Sidharth Guglani
61ae484316 Pin cmake version to 3.6
Summary: Pull Request resolved: https://github.com/facebook/yoga/pull/861

Reviewed By: davidaurelio

Differential Revision: D14081865

Pulled By: SidharthGuglani

fbshipit-source-id: 98bce25cfdd397f48beee64f114c85460b6d81fe
2019-02-14 09:35:02 -08:00
Sidharth Guglani
42657017bd bump version to 1.11.1-SNAPSHOT
Summary: bump version 1.11.1-SNAPSHOT

Reviewed By: davidaurelio

Differential Revision: D14042030

fbshipit-source-id: 0d9ef91013454311d038c23124571d1093471c9b
2019-02-12 06:17:36 -08:00
Sidharth Guglani
9b9a344934 updated gradle build tool version to 3.1.0 and gradle to 4.6
Summary: Upgraded gradle build tool version to 3.1.0 to solve issue "No toolchains found in the NDK toolchains folder for ABI with prefix: mips-linux-android"

Reviewed By: davidaurelio

Differential Revision: D14042022

fbshipit-source-id: c615127fa296f30f589b183bffc8f2a95f7b943b
2019-02-12 05:35:32 -08:00
42 changed files with 3077 additions and 1537 deletions

1
BUCK
View File

@@ -20,7 +20,6 @@ TEST_COMPILER_FLAGS = BASE_COMPILER_FLAGS + GMOCK_OVERRIDE_FLAGS + [
yoga_cxx_library(
name = "yoga",
srcs = glob(["yoga/*.cpp"]),
headers = subdir_glob([("", "yoga/**/*.h")]),
header_namespace = "",
exported_headers = subdir_glob([("", "yoga/*.h")]),
compiler_flags = COMPILER_FLAGS,

View File

@@ -6,7 +6,7 @@
#
Pod::Spec.new do |spec|
spec.name = 'Yoga'
spec.version = '1.9.0'
spec.version = '1.12.0-pre.1'
spec.license = { :type => 'MIT', :file => "LICENSE" }
spec.homepage = 'https://yogalayout.com/'
spec.documentation_url = 'https://yogalayout.com/docs'

View File

@@ -6,7 +6,7 @@
#
podspec = Pod::Spec.new do |spec|
spec.name = 'YogaKit'
spec.version = '1.10.0'
spec.version = '1.12.0'
spec.license = { :type => 'MIT', :file => "LICENSE" }
spec.homepage = 'https://facebook.github.io/yoga/'
spec.documentation_url = 'https://facebook.github.io/yoga/docs/api/yogakit/'

View File

@@ -180,7 +180,7 @@ static YGConfigRef globalConfig;
// the measure function. Since we already know that this is a leaf,
// this *should* be fine. Forgive me Hack Gods.
const YGNodeRef node = self.node;
if (YGNodeGetMeasureFunc(node) == NULL) {
if (YGNodeHasMeasureFunc(node)) {
YGNodeSetMeasureFunc(node, YGMeasureView);
}
@@ -425,9 +425,7 @@ static void YGRemoveAllChildren(const YGNodeRef node)
return;
}
while (YGNodeGetChildCount(node) > 0) {
YGNodeRemoveChild(node, YGNodeGetChild(node, YGNodeGetChildCount(node) - 1));
}
YGNodeRemoveAllChildren(node);
}
static CGFloat YGRoundPixelValue(CGFloat value)

View File

@@ -33,7 +33,7 @@ public class VirtualYogaLayout extends ViewGroup {
final private List<View> mChildren = new LinkedList<>();
final private Map<View, YogaNode> mYogaNodes = new HashMap<>();
final private YogaNode mYogaNode = new YogaNode();
final private YogaNode mYogaNode = YogaNode.create();
public VirtualYogaLayout(Context context) {
super(context);
@@ -73,7 +73,7 @@ public class VirtualYogaLayout extends ViewGroup {
return;
}
YogaNode node = new YogaNode();
YogaNode node = YogaNode.create();
YogaLayout.LayoutParams lp = new YogaLayout.LayoutParams(params);
YogaLayout.applyLayoutParams(lp, node, child);
node.setData(child);

View File

@@ -78,7 +78,7 @@ public class YogaLayout extends ViewGroup {
public YogaLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
mYogaNode = new YogaNode();
mYogaNode = YogaNode.create();
mYogaNodes = new HashMap<>();
mYogaNode.setData(this);
@@ -155,7 +155,7 @@ public class YogaLayout extends ViewGroup {
if(mYogaNodes.containsKey(child)) {
childNode = mYogaNodes.get(child);
} else {
childNode = new YogaNode();
childNode = YogaNode.create();
}
childNode.setData(child);

View File

@@ -9,11 +9,12 @@
buildscript {
repositories {
google()
jcenter()
maven { url 'https://maven.google.com/' }
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.0'
classpath 'com.android.tools.build:gradle:3.1.0'
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7.3'
classpath 'com.github.dcendents:android-maven-gradle-plugin:2.0'
@@ -24,6 +25,7 @@ buildscript {
allprojects {
repositories {
google()
jcenter()
}
}

View File

@@ -9,7 +9,7 @@
org.gradle.jvmargs=-Xmx1536M
VERSION_NAME=1.11.0
VERSION_NAME=1.12.0-SNAPSHOT
POM_URL=https://github.com/facebook/yoga
POM_SCM_URL=https://github.com/facebook/yoga.git
POM_SCM_CONNECTION=scm:git:https://github.com/facebook/yoga.git

View File

@@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.2-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-bin.zip

View File

@@ -29,7 +29,7 @@ add_compile_options(
-Wall
-std=c++11)
add_library(yoga SHARED jni/YGJNI.cpp)
add_library(yoga SHARED jni/YGJNI.cpp jni/YGJTypes.cpp)
target_include_directories(yoga PRIVATE
${libfb_DIR}/include

View File

@@ -35,6 +35,7 @@ android {
externalNativeBuild {
cmake {
path 'CMakeLists.txt'
version '3.6.0-rc2'
}
}

View File

@@ -22,6 +22,7 @@ public class YogaConfig {
long mNativePointer;
private YogaLogger mLogger;
private YogaNodeCloneFunction mYogaNodeCloneFunction;
public boolean avoidGlobalJNIRefs = false;
private native long jni_YGConfigNew();
public YogaConfig() {
@@ -97,16 +98,4 @@ public class YogaConfig {
public YogaLogger getLogger() {
return mLogger;
}
private native void jni_YGConfigSetHasCloneNodeFunc(long nativePointer, boolean hasClonedFunc);
public void setOnCloneNode(YogaNodeCloneFunction cloneYogaNodeFunction) {
mYogaNodeCloneFunction = cloneYogaNodeFunction;
jni_YGConfigSetHasCloneNodeFunc(mNativePointer, cloneYogaNodeFunction != null);
}
@DoNotStrip
private final YogaNode cloneNode(YogaNode oldNode, YogaNode parent, int childIndex) {
return mYogaNodeCloneFunction.cloneNode(oldNode, parent, childIndex);
}
}

View File

@@ -6,250 +6,30 @@
*/
package com.facebook.yoga;
import com.facebook.proguard.annotations.DoNotStrip;
import com.facebook.soloader.SoLoader;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Nullable;
@DoNotStrip
public class YogaNode implements Cloneable {
static {
SoLoader.loadLibrary("yoga");
public abstract class YogaNode {
public static YogaNode create() {
return new YogaNodeJNI();
}
/**
* Get native instance count. Useful for testing only.
*/
static native int jni_YGNodeGetInstanceCount();
private YogaNode mOwner;
@Nullable private List<YogaNode> mChildren;
private YogaMeasureFunction mMeasureFunction;
private YogaBaselineFunction mBaselineFunction;
private long mNativePointer;
private Object mData;
/* Those flags needs be in sync with YGJNI.cpp */
private static final int MARGIN = 1;
private static final int PADDING = 2;
private static final int BORDER = 4;
@DoNotStrip
private int mEdgeSetFlag = 0;
private boolean mHasSetPosition = false;
@DoNotStrip
private float mWidth = YogaConstants.UNDEFINED;
@DoNotStrip
private float mHeight = YogaConstants.UNDEFINED;
@DoNotStrip
private float mTop = YogaConstants.UNDEFINED;
@DoNotStrip
private float mLeft = YogaConstants.UNDEFINED;
@DoNotStrip
private float mMarginLeft = 0;
@DoNotStrip
private float mMarginTop = 0;
@DoNotStrip
private float mMarginRight = 0;
@DoNotStrip
private float mMarginBottom = 0;
@DoNotStrip
private float mPaddingLeft = 0;
@DoNotStrip
private float mPaddingTop = 0;
@DoNotStrip
private float mPaddingRight = 0;
@DoNotStrip
private float mPaddingBottom = 0;
@DoNotStrip
private float mBorderLeft = 0;
@DoNotStrip
private float mBorderTop = 0;
@DoNotStrip
private float mBorderRight = 0;
@DoNotStrip
private float mBorderBottom = 0;
@DoNotStrip
private int mLayoutDirection = 0;
@DoNotStrip
private boolean mHasNewLayout = true;
@DoNotStrip private boolean mDoesLegacyStretchFlagAffectsLayout = false;
private native long jni_YGNodeNew();
public YogaNode() {
mNativePointer = jni_YGNodeNew();
if (mNativePointer == 0) {
throw new IllegalStateException("Failed to allocate native memory");
}
public static YogaNode create(YogaConfig config) {
return new YogaNodeJNI(config);
}
private native long jni_YGNodeNewWithConfig(long configPointer);
public YogaNode(YogaConfig config) {
mNativePointer = jni_YGNodeNewWithConfig(config.mNativePointer);
if (mNativePointer == 0) {
throw new IllegalStateException("Failed to allocate native memory");
}
}
public abstract void reset();
@Override
protected void finalize() throws Throwable {
try {
freeNatives();
} finally {
super.finalize();
}
}
public abstract int getChildCount();
private static native void jni_YGNodeFree(long nativePointer);
public abstract YogaNode getChildAt(int i);
/* frees the native underlying YGNode. Useful for testing. */
public void freeNatives() {
if (mNativePointer > 0) {
long nativePointer = mNativePointer;
mNativePointer = 0;
jni_YGNodeFree(nativePointer);
}
}
public abstract void addChildAt(YogaNode child, int i);
private static native void jni_YGNodeReset(long nativePointer);
public void reset() {
mEdgeSetFlag = 0;
mHasSetPosition = false;
mHasNewLayout = true;
public abstract void setIsReferenceBaseline(boolean isReferenceBaseline);
mWidth = YogaConstants.UNDEFINED;
mHeight = YogaConstants.UNDEFINED;
mTop = YogaConstants.UNDEFINED;
mLeft = YogaConstants.UNDEFINED;
mMarginLeft = 0;
mMarginTop = 0;
mMarginRight = 0;
mMarginBottom = 0;
mPaddingLeft = 0;
mPaddingTop = 0;
mPaddingRight = 0;
mPaddingBottom = 0;
mBorderLeft = 0;
mBorderTop = 0;
mBorderRight = 0;
mBorderBottom = 0;
mLayoutDirection = 0;
public abstract boolean isReferenceBaseline();
mMeasureFunction = null;
mBaselineFunction = null;
mData = null;
mDoesLegacyStretchFlagAffectsLayout = false;
jni_YGNodeReset(mNativePointer);
}
public int getChildCount() {
return mChildren == null ? 0 : mChildren.size();
}
public YogaNode getChildAt(int i) {
if (mChildren == null) {
throw new IllegalStateException("YogaNode does not have children");
}
return mChildren.get(i);
}
private static native void jni_YGNodeInsertChild(long nativePointer, long childPointer, int index);
public void addChildAt(YogaNode child, int i) {
if (child.mOwner != null) {
throw new IllegalStateException("Child already has a parent, it must be removed first.");
}
if (mChildren == null) {
mChildren = new ArrayList<>(4);
}
mChildren.add(i, child);
child.mOwner = this;
jni_YGNodeInsertChild(mNativePointer, child.mNativePointer, i);
}
private static native void jni_YGNodeSetIsReferenceBaseline(long nativePointer, boolean isReferenceBaseline);
public void setIsReferenceBaseline(boolean isReferenceBaseline) {
jni_YGNodeSetIsReferenceBaseline(mNativePointer, isReferenceBaseline);
}
private static native boolean jni_YGNodeIsReferenceBaseline(long nativePointer);
public boolean isReferenceBaseline() {
return jni_YGNodeIsReferenceBaseline(mNativePointer);
}
private static native void jni_YGNodeSetOwner(long nativePointer, long newOwnerNativePointer);
private native long jni_YGNodeClone(long nativePointer, Object newNode);
@Override
public YogaNode clone() {
try {
YogaNode clonedYogaNode = (YogaNode) super.clone();
long clonedNativePointer = jni_YGNodeClone(mNativePointer, clonedYogaNode);
if (mChildren != null) {
for (YogaNode child : mChildren) {
YogaNode.jni_YGNodeSetOwner(child.mNativePointer, 0);
child.mOwner = null;
}
}
clonedYogaNode.mNativePointer = clonedNativePointer;
clonedYogaNode.mOwner = null;
clonedYogaNode.mChildren =
mChildren != null ? (List<YogaNode>) ((ArrayList) mChildren).clone() : null;
if (clonedYogaNode.mChildren != null) {
for (YogaNode child : clonedYogaNode.mChildren) {
child.mOwner = null;
}
}
return clonedYogaNode;
} catch (CloneNotSupportedException ex) {
// This class implements Cloneable, this should not happen
throw new RuntimeException(ex);
}
}
public YogaNode cloneWithNewChildren() {
try {
YogaNode clonedYogaNode = (YogaNode) super.clone();
long clonedNativePointer = jni_YGNodeClone(mNativePointer, clonedYogaNode);
clonedYogaNode.mOwner = null;
clonedYogaNode.mNativePointer = clonedNativePointer;
clonedYogaNode.clearChildren();
return clonedYogaNode;
} catch (CloneNotSupportedException ex) {
// This class implements Cloneable, this should not happen
throw new RuntimeException(ex);
}
}
private static native void jni_YGNodeClearChildren(long nativePointer);
private void clearChildren() {
mChildren = null;
jni_YGNodeClearChildren(mNativePointer);
}
private static native void jni_YGNodeRemoveChild(long nativePointer, long childPointer);
public YogaNode removeChildAt(int i) {
if (mChildren == null) {
throw new IllegalStateException(
"Trying to remove a child of a YogaNode that does not have children");
}
final YogaNode child = mChildren.remove(i);
child.mOwner = null;
jni_YGNodeRemoveChild(mNativePointer, child.mNativePointer);
return child;
}
public abstract YogaNode removeChildAt(int i);
/**
* @returns the {@link YogaNode} that owns this {@link YogaNode}.
@@ -260,539 +40,182 @@ public class YogaNode implements Cloneable {
* {@link YogaNode} is shared between two or more YogaTrees.
*/
@Nullable
public YogaNode getOwner() {
return mOwner;
}
public abstract YogaNode getOwner();
/** @deprecated Use #getOwner() instead. This will be removed in the next version. */
@Deprecated
@Nullable
public YogaNode getParent() {
return getOwner();
}
public int indexOf(YogaNode child) {
return mChildren == null ? -1 : mChildren.indexOf(child);
}
private static native void jni_YGNodeCalculateLayout(long nativePointer, float width, float height);
public void calculateLayout(float width, float height) {
jni_YGNodeCalculateLayout(mNativePointer, width, height);
}
public boolean hasNewLayout() {
return mHasNewLayout;
}
private static native void jni_YGNodeMarkDirty(long nativePointer);
public void dirty() {
jni_YGNodeMarkDirty(mNativePointer);
}
private static native void jni_YGNodeMarkDirtyAndPropogateToDescendants(long nativePointer);
public void dirtyAllDescendants() {
jni_YGNodeMarkDirtyAndPropogateToDescendants(mNativePointer);
}
private static native boolean jni_YGNodeIsDirty(long nativePointer);
public boolean isDirty() {
return jni_YGNodeIsDirty(mNativePointer);
}
private static native void jni_YGNodeCopyStyle(long dstNativePointer, long srcNativePointer);
public void copyStyle(YogaNode srcNode) {
jni_YGNodeCopyStyle(mNativePointer, srcNode.mNativePointer);
}
public void markLayoutSeen() {
mHasNewLayout = false;
}
private static native int jni_YGNodeStyleGetDirection(long nativePointer);
public YogaDirection getStyleDirection() {
return YogaDirection.fromInt(jni_YGNodeStyleGetDirection(mNativePointer));
}
private static native void jni_YGNodeStyleSetDirection(long nativePointer, int direction);
public void setDirection(YogaDirection direction) {
jni_YGNodeStyleSetDirection(mNativePointer, direction.intValue());
}
private static native int jni_YGNodeStyleGetFlexDirection(long nativePointer);
public YogaFlexDirection getFlexDirection() {
return YogaFlexDirection.fromInt(jni_YGNodeStyleGetFlexDirection(mNativePointer));
}
private static native void jni_YGNodeStyleSetFlexDirection(long nativePointer, int flexDirection);
public void setFlexDirection(YogaFlexDirection flexDirection) {
jni_YGNodeStyleSetFlexDirection(mNativePointer, flexDirection.intValue());
}
private static native int jni_YGNodeStyleGetJustifyContent(long nativePointer);
public YogaJustify getJustifyContent() {
return YogaJustify.fromInt(jni_YGNodeStyleGetJustifyContent(mNativePointer));
}
private static native void jni_YGNodeStyleSetJustifyContent(long nativePointer, int justifyContent);
public void setJustifyContent(YogaJustify justifyContent) {
jni_YGNodeStyleSetJustifyContent(mNativePointer, justifyContent.intValue());
}
private static native int jni_YGNodeStyleGetAlignItems(long nativePointer);
public YogaAlign getAlignItems() {
return YogaAlign.fromInt(jni_YGNodeStyleGetAlignItems(mNativePointer));
}
private static native void jni_YGNodeStyleSetAlignItems(long nativePointer, int alignItems);
public void setAlignItems(YogaAlign alignItems) {
jni_YGNodeStyleSetAlignItems(mNativePointer, alignItems.intValue());
}
private static native int jni_YGNodeStyleGetAlignSelf(long nativePointer);
public YogaAlign getAlignSelf() {
return YogaAlign.fromInt(jni_YGNodeStyleGetAlignSelf(mNativePointer));
}
private static native void jni_YGNodeStyleSetAlignSelf(long nativePointer, int alignSelf);
public void setAlignSelf(YogaAlign alignSelf) {
jni_YGNodeStyleSetAlignSelf(mNativePointer, alignSelf.intValue());
}
private static native int jni_YGNodeStyleGetAlignContent(long nativePointer);
public YogaAlign getAlignContent() {
return YogaAlign.fromInt(jni_YGNodeStyleGetAlignContent(mNativePointer));
}
private static native void jni_YGNodeStyleSetAlignContent(long nativePointer, int alignContent);
public void setAlignContent(YogaAlign alignContent) {
jni_YGNodeStyleSetAlignContent(mNativePointer, alignContent.intValue());
}
private static native int jni_YGNodeStyleGetPositionType(long nativePointer);
public YogaPositionType getPositionType() {
return YogaPositionType.fromInt(jni_YGNodeStyleGetPositionType(mNativePointer));
}
private static native void jni_YGNodeStyleSetPositionType(long nativePointer, int positionType);
public void setPositionType(YogaPositionType positionType) {
jni_YGNodeStyleSetPositionType(mNativePointer, positionType.intValue());
}
private static native void jni_YGNodeStyleSetFlexWrap(long nativePointer, int wrapType);
public void setWrap(YogaWrap flexWrap) {
jni_YGNodeStyleSetFlexWrap(mNativePointer, flexWrap.intValue());
}
private static native int jni_YGNodeStyleGetOverflow(long nativePointer);
public YogaOverflow getOverflow() {
return YogaOverflow.fromInt(jni_YGNodeStyleGetOverflow(mNativePointer));
}
private static native void jni_YGNodeStyleSetOverflow(long nativePointer, int overflow);
public void setOverflow(YogaOverflow overflow) {
jni_YGNodeStyleSetOverflow(mNativePointer, overflow.intValue());
}
private static native int jni_YGNodeStyleGetDisplay(long nativePointer);
public YogaDisplay getDisplay() {
return YogaDisplay.fromInt(jni_YGNodeStyleGetDisplay(mNativePointer));
}
private static native void jni_YGNodeStyleSetDisplay(long nativePointer, int display);
public void setDisplay(YogaDisplay display) {
jni_YGNodeStyleSetDisplay(mNativePointer, display.intValue());
}
private static native void jni_YGNodeStyleSetFlex(long nativePointer, float flex);
public void setFlex(float flex) {
jni_YGNodeStyleSetFlex(mNativePointer, flex);
}
private static native float jni_YGNodeStyleGetFlexGrow(long nativePointer);
public float getFlexGrow() {
return jni_YGNodeStyleGetFlexGrow(mNativePointer);
}
private static native void jni_YGNodeStyleSetFlexGrow(long nativePointer, float flexGrow);
public void setFlexGrow(float flexGrow) {
jni_YGNodeStyleSetFlexGrow(mNativePointer, flexGrow);
}
private static native float jni_YGNodeStyleGetFlexShrink(long nativePointer);
public float getFlexShrink() {
return jni_YGNodeStyleGetFlexShrink(mNativePointer);
}
private static native void jni_YGNodeStyleSetFlexShrink(long nativePointer, float flexShrink);
public void setFlexShrink(float flexShrink) {
jni_YGNodeStyleSetFlexShrink(mNativePointer, flexShrink);
}
private static native Object jni_YGNodeStyleGetFlexBasis(long nativePointer);
public YogaValue getFlexBasis() {
return (YogaValue) jni_YGNodeStyleGetFlexBasis(mNativePointer);
}
private static native void jni_YGNodeStyleSetFlexBasis(long nativePointer, float flexBasis);
public void setFlexBasis(float flexBasis) {
jni_YGNodeStyleSetFlexBasis(mNativePointer, flexBasis);
}
private static native void jni_YGNodeStyleSetFlexBasisPercent(long nativePointer, float percent);
public void setFlexBasisPercent(float percent) {
jni_YGNodeStyleSetFlexBasisPercent(mNativePointer, percent);
}
private static native void jni_YGNodeStyleSetFlexBasisAuto(long nativePointer);
public void setFlexBasisAuto() {
jni_YGNodeStyleSetFlexBasisAuto(mNativePointer);
}
private static native Object jni_YGNodeStyleGetMargin(long nativePointer, int edge);
public YogaValue getMargin(YogaEdge edge) {
if (!((mEdgeSetFlag & MARGIN) == MARGIN)) {
return YogaValue.UNDEFINED;
}
return (YogaValue) jni_YGNodeStyleGetMargin(mNativePointer, edge.intValue());
}
private static native void jni_YGNodeStyleSetMargin(long nativePointer, int edge, float margin);
public void setMargin(YogaEdge edge, float margin) {
mEdgeSetFlag |= MARGIN;
jni_YGNodeStyleSetMargin(mNativePointer, edge.intValue(), margin);
}
private static native void jni_YGNodeStyleSetMarginPercent(long nativePointer, int edge, float percent);
public void setMarginPercent(YogaEdge edge, float percent) {
mEdgeSetFlag |= MARGIN;
jni_YGNodeStyleSetMarginPercent(mNativePointer, edge.intValue(), percent);
}
private static native void jni_YGNodeStyleSetMarginAuto(long nativePointer, int edge);
public void setMarginAuto(YogaEdge edge) {
mEdgeSetFlag |= MARGIN;
jni_YGNodeStyleSetMarginAuto(mNativePointer, edge.intValue());
}
private static native Object jni_YGNodeStyleGetPadding(long nativePointer, int edge);
public YogaValue getPadding(YogaEdge edge) {
if (!((mEdgeSetFlag & PADDING) == PADDING)) {
return YogaValue.UNDEFINED;
}
return (YogaValue) jni_YGNodeStyleGetPadding(mNativePointer, edge.intValue());
}
private static native void jni_YGNodeStyleSetPadding(long nativePointer, int edge, float padding);
public void setPadding(YogaEdge edge, float padding) {
mEdgeSetFlag |= PADDING;
jni_YGNodeStyleSetPadding(mNativePointer, edge.intValue(), padding);
}
private static native void jni_YGNodeStyleSetPaddingPercent(long nativePointer, int edge, float percent);
public void setPaddingPercent(YogaEdge edge, float percent) {
mEdgeSetFlag |= PADDING;
jni_YGNodeStyleSetPaddingPercent(mNativePointer, edge.intValue(), percent);
}
private static native float jni_YGNodeStyleGetBorder(long nativePointer, int edge);
public float getBorder(YogaEdge edge) {
if (!((mEdgeSetFlag & BORDER) == BORDER)) {
return YogaConstants.UNDEFINED;
}
return jni_YGNodeStyleGetBorder(mNativePointer, edge.intValue());
}
private static native void jni_YGNodeStyleSetBorder(long nativePointer, int edge, float border);
public void setBorder(YogaEdge edge, float border) {
mEdgeSetFlag |= BORDER;
jni_YGNodeStyleSetBorder(mNativePointer, edge.intValue(), border);
}
private static native Object jni_YGNodeStyleGetPosition(long nativePointer, int edge);
public YogaValue getPosition(YogaEdge edge) {
if (!mHasSetPosition) {
return YogaValue.UNDEFINED;
}
return (YogaValue) jni_YGNodeStyleGetPosition(mNativePointer, edge.intValue());
}
private static native void jni_YGNodeStyleSetPosition(long nativePointer, int edge, float position);
public void setPosition(YogaEdge edge, float position) {
mHasSetPosition = true;
jni_YGNodeStyleSetPosition(mNativePointer, edge.intValue(), position);
}
private static native void jni_YGNodeStyleSetPositionPercent(long nativePointer, int edge, float percent);
public void setPositionPercent(YogaEdge edge, float percent) {
mHasSetPosition = true;
jni_YGNodeStyleSetPositionPercent(mNativePointer, edge.intValue(), percent);
}
private static native Object jni_YGNodeStyleGetWidth(long nativePointer);
public YogaValue getWidth() {
return (YogaValue) jni_YGNodeStyleGetWidth(mNativePointer);
}
private static native void jni_YGNodeStyleSetWidth(long nativePointer, float width);
public void setWidth(float width) {
jni_YGNodeStyleSetWidth(mNativePointer, width);
}
private static native void jni_YGNodeStyleSetWidthPercent(long nativePointer, float percent);
public void setWidthPercent(float percent) {
jni_YGNodeStyleSetWidthPercent(mNativePointer, percent);
}
private static native void jni_YGNodeStyleSetWidthAuto(long nativePointer);
public void setWidthAuto() {
jni_YGNodeStyleSetWidthAuto(mNativePointer);
}
private static native Object jni_YGNodeStyleGetHeight(long nativePointer);
public YogaValue getHeight() {
return (YogaValue) jni_YGNodeStyleGetHeight(mNativePointer);
}
private static native void jni_YGNodeStyleSetHeight(long nativePointer, float height);
public void setHeight(float height) {
jni_YGNodeStyleSetHeight(mNativePointer, height);
}
private static native void jni_YGNodeStyleSetHeightPercent(long nativePointer, float percent);
public void setHeightPercent(float percent) {
jni_YGNodeStyleSetHeightPercent(mNativePointer, percent);
}
private static native void jni_YGNodeStyleSetHeightAuto(long nativePointer);
public void setHeightAuto() {
jni_YGNodeStyleSetHeightAuto(mNativePointer);
}
private static native Object jni_YGNodeStyleGetMinWidth(long nativePointer);
public YogaValue getMinWidth() {
return (YogaValue) jni_YGNodeStyleGetMinWidth(mNativePointer);
}
private static native void jni_YGNodeStyleSetMinWidth(long nativePointer, float minWidth);
public void setMinWidth(float minWidth) {
jni_YGNodeStyleSetMinWidth(mNativePointer, minWidth);
}
private static native void jni_YGNodeStyleSetMinWidthPercent(long nativePointer, float percent);
public void setMinWidthPercent(float percent) {
jni_YGNodeStyleSetMinWidthPercent(mNativePointer, percent);
}
private static native Object jni_YGNodeStyleGetMinHeight(long nativePointer);
public YogaValue getMinHeight() {
return (YogaValue) jni_YGNodeStyleGetMinHeight(mNativePointer);
}
private static native void jni_YGNodeStyleSetMinHeight(long nativePointer, float minHeight);
public void setMinHeight(float minHeight) {
jni_YGNodeStyleSetMinHeight(mNativePointer, minHeight);
}
private static native void jni_YGNodeStyleSetMinHeightPercent(long nativePointer, float percent);
public void setMinHeightPercent(float percent) {
jni_YGNodeStyleSetMinHeightPercent(mNativePointer, percent);
}
private static native Object jni_YGNodeStyleGetMaxWidth(long nativePointer);
public YogaValue getMaxWidth() {
return (YogaValue) jni_YGNodeStyleGetMaxWidth(mNativePointer);
}
private static native void jni_YGNodeStyleSetMaxWidth(long nativePointer, float maxWidth);
public void setMaxWidth(float maxWidth) {
jni_YGNodeStyleSetMaxWidth(mNativePointer, maxWidth);
}
private static native void jni_YGNodeStyleSetMaxWidthPercent(long nativePointer, float percent);
public void setMaxWidthPercent(float percent) {
jni_YGNodeStyleSetMaxWidthPercent(mNativePointer, percent);
}
private static native Object jni_YGNodeStyleGetMaxHeight(long nativePointer);
public YogaValue getMaxHeight() {
return (YogaValue) jni_YGNodeStyleGetMaxHeight(mNativePointer);
}
private static native void jni_YGNodeStyleSetMaxHeight(long nativePointer, float maxheight);
public void setMaxHeight(float maxheight) {
jni_YGNodeStyleSetMaxHeight(mNativePointer, maxheight);
}
private static native void jni_YGNodeStyleSetMaxHeightPercent(long nativePointer, float percent);
public void setMaxHeightPercent(float percent) {
jni_YGNodeStyleSetMaxHeightPercent(mNativePointer, percent);
}
private static native float jni_YGNodeStyleGetAspectRatio(long nativePointer);
public float getAspectRatio() {
return jni_YGNodeStyleGetAspectRatio(mNativePointer);
}
private static native void jni_YGNodeStyleSetAspectRatio(long nativePointer, float aspectRatio);
public void setAspectRatio(float aspectRatio) {
jni_YGNodeStyleSetAspectRatio(mNativePointer, aspectRatio);
}
public float getLayoutX() {
return mLeft;
}
public float getLayoutY() {
return mTop;
}
public float getLayoutWidth() {
return mWidth;
}
public float getLayoutHeight() {
return mHeight;
}
public boolean getDoesLegacyStretchFlagAffectsLayout() {
return mDoesLegacyStretchFlagAffectsLayout;
}
public float getLayoutMargin(YogaEdge edge) {
switch (edge) {
case LEFT:
return mMarginLeft;
case TOP:
return mMarginTop;
case RIGHT:
return mMarginRight;
case BOTTOM:
return mMarginBottom;
case START:
return getLayoutDirection() == YogaDirection.RTL ? mMarginRight : mMarginLeft;
case END:
return getLayoutDirection() == YogaDirection.RTL ? mMarginLeft : mMarginRight;
default:
throw new IllegalArgumentException("Cannot get layout margins of multi-edge shorthands");
}
}
public float getLayoutPadding(YogaEdge edge) {
switch (edge) {
case LEFT:
return mPaddingLeft;
case TOP:
return mPaddingTop;
case RIGHT:
return mPaddingRight;
case BOTTOM:
return mPaddingBottom;
case START:
return getLayoutDirection() == YogaDirection.RTL ? mPaddingRight : mPaddingLeft;
case END:
return getLayoutDirection() == YogaDirection.RTL ? mPaddingLeft : mPaddingRight;
default:
throw new IllegalArgumentException("Cannot get layout paddings of multi-edge shorthands");
}
}
public float getLayoutBorder(YogaEdge edge) {
switch (edge) {
case LEFT:
return mBorderLeft;
case TOP:
return mBorderTop;
case RIGHT:
return mBorderRight;
case BOTTOM:
return mBorderBottom;
case START:
return getLayoutDirection() == YogaDirection.RTL ? mBorderRight : mBorderLeft;
case END:
return getLayoutDirection() == YogaDirection.RTL ? mBorderLeft : mBorderRight;
default:
throw new IllegalArgumentException("Cannot get layout border of multi-edge shorthands");
}
}
public YogaDirection getLayoutDirection() {
return YogaDirection.fromInt(mLayoutDirection);
}
private static native void jni_YGNodeSetHasMeasureFunc(long nativePointer, boolean hasMeasureFunc);
public void setMeasureFunction(YogaMeasureFunction measureFunction) {
mMeasureFunction = measureFunction;
jni_YGNodeSetHasMeasureFunc(mNativePointer, measureFunction != null);
}
// Implementation Note: Why this method needs to stay final
//
// We cache the jmethodid for this method in Yoga code. This means that even if a subclass
// were to override measure, we'd still call this implementation from layout code since the
// overriding method will have a different jmethodid. This is final to prevent that mistake.
@DoNotStrip
public final long measure(float width, int widthMode, float height, int heightMode) {
if (!isMeasureDefined()) {
throw new RuntimeException("Measure function isn't defined!");
}
return mMeasureFunction.measure(
this,
width,
YogaMeasureMode.fromInt(widthMode),
height,
YogaMeasureMode.fromInt(heightMode));
}
private static native void jni_YGNodeSetHasBaselineFunc(long nativePointer, boolean hasMeasureFunc);
public void setBaselineFunction(YogaBaselineFunction baselineFunction) {
mBaselineFunction = baselineFunction;
jni_YGNodeSetHasBaselineFunc(mNativePointer, baselineFunction != null);
}
@DoNotStrip
public final float baseline(float width, float height) {
return mBaselineFunction.baseline(this, width, height);
}
public boolean isMeasureDefined() {
return mMeasureFunction != null;
}
public void setData(Object data) {
mData = data;
}
public Object getData() {
return mData;
}
private static native void jni_YGNodePrint(long nativePointer);
/**
* Use the set logger (defaults to adb log) to print out the styles, children, and computed
* layout of the tree rooted at this node.
*/
public void print() {
jni_YGNodePrint(mNativePointer);
}
/**
* This method replaces the child at childIndex position with the newNode received by parameter.
* This is different than calling removeChildAt and addChildAt because this method ONLY replaces
* the child in the mChildren datastructure. @DoNotStrip: called from JNI
*
* @return the nativePointer of the newNode {@linl YogaNode}
*/
@DoNotStrip
private final long replaceChild(YogaNode newNode, int childIndex) {
if (mChildren == null) {
throw new IllegalStateException("Cannot replace child. YogaNode does not have children");
}
mChildren.remove(childIndex);
mChildren.add(childIndex, newNode);
newNode.mOwner = this;
return newNode.mNativePointer;
}
public abstract YogaNode getParent();
public abstract int indexOf(YogaNode child);
public abstract void calculateLayout(float width, float height);
public abstract boolean hasNewLayout();
public abstract void dirty();
public abstract boolean isDirty();
public abstract void copyStyle(YogaNode srcNode);
public abstract void markLayoutSeen();
public abstract YogaDirection getStyleDirection();
public abstract void setDirection(YogaDirection direction);
public abstract YogaFlexDirection getFlexDirection();
public abstract void setFlexDirection(YogaFlexDirection flexDirection);
public abstract YogaJustify getJustifyContent();
public abstract void setJustifyContent(YogaJustify justifyContent);
public abstract YogaAlign getAlignItems();
public abstract void setAlignItems(YogaAlign alignItems);
public abstract YogaAlign getAlignSelf();
public abstract void setAlignSelf(YogaAlign alignSelf);
public abstract YogaAlign getAlignContent();
public abstract void setAlignContent(YogaAlign alignContent);
public abstract YogaPositionType getPositionType();
public abstract void setPositionType(YogaPositionType positionType);
public abstract YogaWrap getWrap();
public abstract void setWrap(YogaWrap flexWrap);
public abstract YogaOverflow getOverflow();
public abstract void setOverflow(YogaOverflow overflow);
public abstract YogaDisplay getDisplay();
public abstract void setDisplay(YogaDisplay display);
public abstract float getFlex();
public abstract void setFlex(float flex);
public abstract float getFlexGrow();
public abstract void setFlexGrow(float flexGrow);
public abstract float getFlexShrink();
public abstract void setFlexShrink(float flexShrink);
public abstract YogaValue getFlexBasis();
public abstract void setFlexBasis(float flexBasis);
public abstract void setFlexBasisPercent(float percent);
public abstract void setFlexBasisAuto();
public abstract YogaValue getMargin(YogaEdge edge);
public abstract void setMargin(YogaEdge edge, float margin);
public abstract void setMarginPercent(YogaEdge edge, float percent);
public abstract void setMarginAuto(YogaEdge edge);
public abstract YogaValue getPadding(YogaEdge edge);
public abstract void setPadding(YogaEdge edge, float padding);
public abstract void setPaddingPercent(YogaEdge edge, float percent);
public abstract float getBorder(YogaEdge edge);
public abstract void setBorder(YogaEdge edge, float border);
public abstract YogaValue getPosition(YogaEdge edge);
public abstract void setPosition(YogaEdge edge, float position);
public abstract void setPositionPercent(YogaEdge edge, float percent);
public abstract YogaValue getWidth();
public abstract void setWidth(float width);
public abstract void setWidthPercent(float percent);
public abstract void setWidthAuto();
public abstract YogaValue getHeight();
public abstract void setHeight(float height);
public abstract void setHeightPercent(float percent);
public abstract void setHeightAuto();
public abstract YogaValue getMinWidth();
public abstract void setMinWidth(float minWidth);
public abstract void setMinWidthPercent(float percent);
public abstract YogaValue getMinHeight();
public abstract void setMinHeight(float minHeight);
public abstract void setMinHeightPercent(float percent);
public abstract YogaValue getMaxWidth();
public abstract void setMaxWidth(float maxWidth);
public abstract void setMaxWidthPercent(float percent);
public abstract YogaValue getMaxHeight();
public abstract void setMaxHeight(float maxheight);
public abstract void setMaxHeightPercent(float percent);
public abstract float getAspectRatio();
public abstract void setAspectRatio(float aspectRatio);
public abstract float getLayoutX();
public abstract float getLayoutY();
public abstract float getLayoutWidth();
public abstract float getLayoutHeight();
public abstract float getLayoutMargin(YogaEdge edge);
public abstract float getLayoutPadding(YogaEdge edge);
public abstract float getLayoutBorder(YogaEdge edge);
public abstract YogaDirection getLayoutDirection();
public abstract void setMeasureFunction(YogaMeasureFunction measureFunction);
public abstract void setBaselineFunction(YogaBaselineFunction baselineFunction);
public abstract boolean isMeasureDefined();
public abstract void setData(Object data);
public abstract Object getData();
public abstract void print();
public abstract void setStyleInputs(float[] styleInputs, int size);
}

View File

@@ -0,0 +1,766 @@
/**
* 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.
*/
package com.facebook.yoga;
import com.facebook.proguard.annotations.DoNotStrip;
import com.facebook.soloader.SoLoader;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Nullable;
@DoNotStrip
public class YogaNodeJNI extends YogaNode {
static {
SoLoader.loadLibrary("yoga");
}
/**
* Get native instance count. Useful for testing only.
*/
static native int jni_YGNodeGetInstanceCount();
private YogaNodeJNI mOwner;
@Nullable private List<YogaNodeJNI> mChildren;
private YogaMeasureFunction mMeasureFunction;
private YogaBaselineFunction mBaselineFunction;
private long mNativePointer;
private Object mData;
/* Those flags needs be in sync with YGJNI.cpp */
private static final int MARGIN = 1;
private static final int PADDING = 2;
private static final int BORDER = 4;
private final boolean mAvoidGlobalJNIRefs;
@DoNotStrip
private float mWidth = YogaConstants.UNDEFINED;
@DoNotStrip
private float mHeight = YogaConstants.UNDEFINED;
@DoNotStrip
private float mTop = YogaConstants.UNDEFINED;
@DoNotStrip
private float mLeft = YogaConstants.UNDEFINED;
@DoNotStrip
private float mMarginLeft = 0;
@DoNotStrip
private float mMarginTop = 0;
@DoNotStrip
private float mMarginRight = 0;
@DoNotStrip
private float mMarginBottom = 0;
@DoNotStrip
private float mPaddingLeft = 0;
@DoNotStrip
private float mPaddingTop = 0;
@DoNotStrip
private float mPaddingRight = 0;
@DoNotStrip
private float mPaddingBottom = 0;
@DoNotStrip
private float mBorderLeft = 0;
@DoNotStrip
private float mBorderTop = 0;
@DoNotStrip
private float mBorderRight = 0;
@DoNotStrip
private float mBorderBottom = 0;
@DoNotStrip
private int mLayoutDirection = 0;
@DoNotStrip
private boolean mHasNewLayout = true;
@DoNotStrip private boolean mDoesLegacyStretchFlagAffectsLayout = false;
private native long jni_YGNodeNew();
public YogaNodeJNI() {
mAvoidGlobalJNIRefs = false;
mNativePointer = jni_YGNodeNew();
if (mNativePointer == 0) {
throw new IllegalStateException("Failed to allocate native memory");
}
}
private native long jni_YGNodeNewWithConfig(long configPointer, boolean avoidGlobalJNIRefs);
public YogaNodeJNI(YogaConfig config) {
mAvoidGlobalJNIRefs = config.avoidGlobalJNIRefs;
mNativePointer = jni_YGNodeNewWithConfig(config.mNativePointer, mAvoidGlobalJNIRefs);
if (mNativePointer == 0) {
throw new IllegalStateException("Failed to allocate native memory");
}
}
@Override
protected void finalize() throws Throwable {
try {
freeNatives();
} finally {
super.finalize();
}
}
private static native void jni_YGNodeFree(long nativePointer);
/* frees the native underlying YGNode. Useful for testing. */
public void freeNatives() {
if (mNativePointer > 0) {
long nativePointer = mNativePointer;
mNativePointer = 0;
jni_YGNodeFree(nativePointer);
}
}
private static native void jni_YGNodeReset(long nativePointer);
public void reset() {
mHasNewLayout = true;
mWidth = YogaConstants.UNDEFINED;
mHeight = YogaConstants.UNDEFINED;
mTop = YogaConstants.UNDEFINED;
mLeft = YogaConstants.UNDEFINED;
mMarginLeft = 0;
mMarginTop = 0;
mMarginRight = 0;
mMarginBottom = 0;
mPaddingLeft = 0;
mPaddingTop = 0;
mPaddingRight = 0;
mPaddingBottom = 0;
mBorderLeft = 0;
mBorderTop = 0;
mBorderRight = 0;
mBorderBottom = 0;
mLayoutDirection = 0;
mMeasureFunction = null;
mBaselineFunction = null;
mData = null;
mDoesLegacyStretchFlagAffectsLayout = false;
jni_YGNodeReset(mNativePointer);
}
public int getChildCount() {
return mChildren == null ? 0 : mChildren.size();
}
public YogaNodeJNI getChildAt(int i) {
if (mChildren == null) {
throw new IllegalStateException("YogaNode does not have children");
}
return mChildren.get(i);
}
private static native void jni_YGNodeInsertChild(long nativePointer, long childPointer, int index);
public void addChildAt(YogaNode c, int i) {
YogaNodeJNI child = (YogaNodeJNI) c;
if (child.mOwner != null) {
throw new IllegalStateException("Child already has a parent, it must be removed first.");
}
if (mChildren == null) {
mChildren = new ArrayList<>(4);
}
mChildren.add(i, child);
child.mOwner = this;
jni_YGNodeInsertChild(mNativePointer, child.mNativePointer, i);
}
private static native void jni_YGNodeSetIsReferenceBaseline(long nativePointer, boolean isReferenceBaseline);
public void setIsReferenceBaseline(boolean isReferenceBaseline) {
jni_YGNodeSetIsReferenceBaseline(mNativePointer, isReferenceBaseline);
}
private static native boolean jni_YGNodeIsReferenceBaseline(long nativePointer);
public boolean isReferenceBaseline() {
return jni_YGNodeIsReferenceBaseline(mNativePointer);
}
private static native void jni_YGNodeClearChildren(long nativePointer);
private void clearChildren() {
mChildren = null;
jni_YGNodeClearChildren(mNativePointer);
}
private static native void jni_YGNodeRemoveChild(long nativePointer, long childPointer);
public YogaNodeJNI removeChildAt(int i) {
if (mChildren == null) {
throw new IllegalStateException(
"Trying to remove a child of a YogaNode that does not have children");
}
final YogaNodeJNI child = mChildren.remove(i);
child.mOwner = null;
jni_YGNodeRemoveChild(mNativePointer, child.mNativePointer);
return child;
}
/**
* @returns the {@link YogaNode} that owns this {@link YogaNode}.
* The owner is used to identify the YogaTree that a {@link YogaNode} belongs
* to.
* This method will return the parent of the {@link YogaNode} when the
* {@link YogaNode} only belongs to one YogaTree or null when the
* {@link YogaNode} is shared between two or more YogaTrees.
*/
@Nullable
public YogaNodeJNI getOwner() {
return mOwner;
}
/** @deprecated Use #getOwner() instead. This will be removed in the next version. */
@Deprecated
@Nullable
public YogaNodeJNI getParent() {
return getOwner();
}
public int indexOf(YogaNode child) {
return mChildren == null ? -1 : mChildren.indexOf(child);
}
private static native void jni_YGNodeCalculateLayout(long nativePointer, float width, float height, long[] nativePointers, YogaNodeJNI[] nodes);
public void calculateLayout(float width, float height) {
long[] nativePointers = null;
YogaNodeJNI[] nodes = null;
if (mAvoidGlobalJNIRefs) {
ArrayList<YogaNodeJNI> n = new ArrayList<>();
n.add(this);
for (int i = 0; i < n.size(); ++i) {
List<YogaNodeJNI> children = n.get(i).mChildren;
if (children != null) {
n.addAll(children);
}
}
nodes = n.toArray(new YogaNodeJNI[n.size()]);
nativePointers = new long[nodes.length];
for (int i = 0; i < nodes.length; ++i) {
nativePointers[i] = nodes[i].mNativePointer;
}
}
jni_YGNodeCalculateLayout(mNativePointer, width, height, nativePointers, nodes);
}
public boolean hasNewLayout() {
return mHasNewLayout;
}
private static native void jni_YGNodeMarkDirty(long nativePointer);
public void dirty() {
jni_YGNodeMarkDirty(mNativePointer);
}
private static native void jni_YGNodeMarkDirtyAndPropogateToDescendants(long nativePointer);
public void dirtyAllDescendants() {
jni_YGNodeMarkDirtyAndPropogateToDescendants(mNativePointer);
}
private static native boolean jni_YGNodeIsDirty(long nativePointer);
public boolean isDirty() {
return jni_YGNodeIsDirty(mNativePointer);
}
private static native void jni_YGNodeCopyStyle(long dstNativePointer, long srcNativePointer);
@Override
public void copyStyle(YogaNode srcNode) {
jni_YGNodeCopyStyle(mNativePointer, ((YogaNodeJNI) srcNode).mNativePointer);
}
public void markLayoutSeen() {
mHasNewLayout = false;
}
private static native int jni_YGNodeStyleGetDirection(long nativePointer);
public YogaDirection getStyleDirection() {
return YogaDirection.fromInt(jni_YGNodeStyleGetDirection(mNativePointer));
}
private static native void jni_YGNodeStyleSetDirection(long nativePointer, int direction);
public void setDirection(YogaDirection direction) {
jni_YGNodeStyleSetDirection(mNativePointer, direction.intValue());
}
private static native int jni_YGNodeStyleGetFlexDirection(long nativePointer);
public YogaFlexDirection getFlexDirection() {
return YogaFlexDirection.fromInt(jni_YGNodeStyleGetFlexDirection(mNativePointer));
}
private static native void jni_YGNodeStyleSetFlexDirection(long nativePointer, int flexDirection);
public void setFlexDirection(YogaFlexDirection flexDirection) {
jni_YGNodeStyleSetFlexDirection(mNativePointer, flexDirection.intValue());
}
private static native int jni_YGNodeStyleGetJustifyContent(long nativePointer);
public YogaJustify getJustifyContent() {
return YogaJustify.fromInt(jni_YGNodeStyleGetJustifyContent(mNativePointer));
}
private static native void jni_YGNodeStyleSetJustifyContent(long nativePointer, int justifyContent);
public void setJustifyContent(YogaJustify justifyContent) {
jni_YGNodeStyleSetJustifyContent(mNativePointer, justifyContent.intValue());
}
private static native int jni_YGNodeStyleGetAlignItems(long nativePointer);
public YogaAlign getAlignItems() {
return YogaAlign.fromInt(jni_YGNodeStyleGetAlignItems(mNativePointer));
}
private static native void jni_YGNodeStyleSetAlignItems(long nativePointer, int alignItems);
public void setAlignItems(YogaAlign alignItems) {
jni_YGNodeStyleSetAlignItems(mNativePointer, alignItems.intValue());
}
private static native int jni_YGNodeStyleGetAlignSelf(long nativePointer);
public YogaAlign getAlignSelf() {
return YogaAlign.fromInt(jni_YGNodeStyleGetAlignSelf(mNativePointer));
}
private static native void jni_YGNodeStyleSetAlignSelf(long nativePointer, int alignSelf);
public void setAlignSelf(YogaAlign alignSelf) {
jni_YGNodeStyleSetAlignSelf(mNativePointer, alignSelf.intValue());
}
private static native int jni_YGNodeStyleGetAlignContent(long nativePointer);
public YogaAlign getAlignContent() {
return YogaAlign.fromInt(jni_YGNodeStyleGetAlignContent(mNativePointer));
}
private static native void jni_YGNodeStyleSetAlignContent(long nativePointer, int alignContent);
public void setAlignContent(YogaAlign alignContent) {
jni_YGNodeStyleSetAlignContent(mNativePointer, alignContent.intValue());
}
private static native int jni_YGNodeStyleGetPositionType(long nativePointer);
public YogaPositionType getPositionType() {
return YogaPositionType.fromInt(jni_YGNodeStyleGetPositionType(mNativePointer));
}
private static native void jni_YGNodeStyleSetPositionType(long nativePointer, int positionType);
public void setPositionType(YogaPositionType positionType) {
jni_YGNodeStyleSetPositionType(mNativePointer, positionType.intValue());
}
private static native int jni_YGNodeStyleGetFlexWrap(long nativePointer);
public YogaWrap getWrap() {
return YogaWrap.fromInt(jni_YGNodeStyleGetFlexWrap(mNativePointer));
}
private static native void jni_YGNodeStyleSetFlexWrap(long nativePointer, int wrapType);
public void setWrap(YogaWrap flexWrap) {
jni_YGNodeStyleSetFlexWrap(mNativePointer, flexWrap.intValue());
}
private static native int jni_YGNodeStyleGetOverflow(long nativePointer);
public YogaOverflow getOverflow() {
return YogaOverflow.fromInt(jni_YGNodeStyleGetOverflow(mNativePointer));
}
private static native void jni_YGNodeStyleSetOverflow(long nativePointer, int overflow);
public void setOverflow(YogaOverflow overflow) {
jni_YGNodeStyleSetOverflow(mNativePointer, overflow.intValue());
}
private static native int jni_YGNodeStyleGetDisplay(long nativePointer);
public YogaDisplay getDisplay() {
return YogaDisplay.fromInt(jni_YGNodeStyleGetDisplay(mNativePointer));
}
private static native void jni_YGNodeStyleSetDisplay(long nativePointer, int display);
public void setDisplay(YogaDisplay display) {
jni_YGNodeStyleSetDisplay(mNativePointer, display.intValue());
}
private static native float jni_YGNodeStyleGetFlex(long nativePointer);
public float getFlex() {
return jni_YGNodeStyleGetFlex(mNativePointer);
}
private static native void jni_YGNodeStyleSetFlex(long nativePointer, float flex);
public void setFlex(float flex) {
jni_YGNodeStyleSetFlex(mNativePointer, flex);
}
private static native float jni_YGNodeStyleGetFlexGrow(long nativePointer);
public float getFlexGrow() {
return jni_YGNodeStyleGetFlexGrow(mNativePointer);
}
private static native void jni_YGNodeStyleSetFlexGrow(long nativePointer, float flexGrow);
public void setFlexGrow(float flexGrow) {
jni_YGNodeStyleSetFlexGrow(mNativePointer, flexGrow);
}
private static native float jni_YGNodeStyleGetFlexShrink(long nativePointer);
public float getFlexShrink() {
return jni_YGNodeStyleGetFlexShrink(mNativePointer);
}
private static native void jni_YGNodeStyleSetFlexShrink(long nativePointer, float flexShrink);
public void setFlexShrink(float flexShrink) {
jni_YGNodeStyleSetFlexShrink(mNativePointer, flexShrink);
}
private static native Object jni_YGNodeStyleGetFlexBasis(long nativePointer);
public YogaValue getFlexBasis() {
return (YogaValue) jni_YGNodeStyleGetFlexBasis(mNativePointer);
}
private static native void jni_YGNodeStyleSetFlexBasis(long nativePointer, float flexBasis);
public void setFlexBasis(float flexBasis) {
jni_YGNodeStyleSetFlexBasis(mNativePointer, flexBasis);
}
private static native void jni_YGNodeStyleSetFlexBasisPercent(long nativePointer, float percent);
public void setFlexBasisPercent(float percent) {
jni_YGNodeStyleSetFlexBasisPercent(mNativePointer, percent);
}
private static native void jni_YGNodeStyleSetFlexBasisAuto(long nativePointer);
public void setFlexBasisAuto() {
jni_YGNodeStyleSetFlexBasisAuto(mNativePointer);
}
private static native Object jni_YGNodeStyleGetMargin(long nativePointer, int edge);
public YogaValue getMargin(YogaEdge edge) {
return (YogaValue) jni_YGNodeStyleGetMargin(mNativePointer, edge.intValue());
}
private static native void jni_YGNodeStyleSetMargin(long nativePointer, int edge, float margin);
public void setMargin(YogaEdge edge, float margin) {
jni_YGNodeStyleSetMargin(mNativePointer, edge.intValue(), margin);
}
private static native void jni_YGNodeStyleSetMarginPercent(long nativePointer, int edge, float percent);
public void setMarginPercent(YogaEdge edge, float percent) {
jni_YGNodeStyleSetMarginPercent(mNativePointer, edge.intValue(), percent);
}
private static native void jni_YGNodeStyleSetMarginAuto(long nativePointer, int edge);
public void setMarginAuto(YogaEdge edge) {
jni_YGNodeStyleSetMarginAuto(mNativePointer, edge.intValue());
}
private static native Object jni_YGNodeStyleGetPadding(long nativePointer, int edge);
public YogaValue getPadding(YogaEdge edge) {
return (YogaValue) jni_YGNodeStyleGetPadding(mNativePointer, edge.intValue());
}
private static native void jni_YGNodeStyleSetPadding(long nativePointer, int edge, float padding);
public void setPadding(YogaEdge edge, float padding) {
jni_YGNodeStyleSetPadding(mNativePointer, edge.intValue(), padding);
}
private static native void jni_YGNodeStyleSetPaddingPercent(long nativePointer, int edge, float percent);
public void setPaddingPercent(YogaEdge edge, float percent) {
jni_YGNodeStyleSetPaddingPercent(mNativePointer, edge.intValue(), percent);
}
private static native float jni_YGNodeStyleGetBorder(long nativePointer, int edge);
public float getBorder(YogaEdge edge) {
return jni_YGNodeStyleGetBorder(mNativePointer, edge.intValue());
}
private static native void jni_YGNodeStyleSetBorder(long nativePointer, int edge, float border);
public void setBorder(YogaEdge edge, float border) {
jni_YGNodeStyleSetBorder(mNativePointer, edge.intValue(), border);
}
private static native Object jni_YGNodeStyleGetPosition(long nativePointer, int edge);
public YogaValue getPosition(YogaEdge edge) {
return (YogaValue) jni_YGNodeStyleGetPosition(mNativePointer, edge.intValue());
}
private static native void jni_YGNodeStyleSetPosition(long nativePointer, int edge, float position);
public void setPosition(YogaEdge edge, float position) {
jni_YGNodeStyleSetPosition(mNativePointer, edge.intValue(), position);
}
private static native void jni_YGNodeStyleSetPositionPercent(long nativePointer, int edge, float percent);
public void setPositionPercent(YogaEdge edge, float percent) {
jni_YGNodeStyleSetPositionPercent(mNativePointer, edge.intValue(), percent);
}
private static native Object jni_YGNodeStyleGetWidth(long nativePointer);
public YogaValue getWidth() {
return (YogaValue) jni_YGNodeStyleGetWidth(mNativePointer);
}
private static native void jni_YGNodeStyleSetWidth(long nativePointer, float width);
public void setWidth(float width) {
jni_YGNodeStyleSetWidth(mNativePointer, width);
}
private static native void jni_YGNodeStyleSetWidthPercent(long nativePointer, float percent);
public void setWidthPercent(float percent) {
jni_YGNodeStyleSetWidthPercent(mNativePointer, percent);
}
private static native void jni_YGNodeStyleSetWidthAuto(long nativePointer);
public void setWidthAuto() {
jni_YGNodeStyleSetWidthAuto(mNativePointer);
}
private static native Object jni_YGNodeStyleGetHeight(long nativePointer);
public YogaValue getHeight() {
return (YogaValue) jni_YGNodeStyleGetHeight(mNativePointer);
}
private static native void jni_YGNodeStyleSetHeight(long nativePointer, float height);
public void setHeight(float height) {
jni_YGNodeStyleSetHeight(mNativePointer, height);
}
private static native void jni_YGNodeStyleSetHeightPercent(long nativePointer, float percent);
public void setHeightPercent(float percent) {
jni_YGNodeStyleSetHeightPercent(mNativePointer, percent);
}
private static native void jni_YGNodeStyleSetHeightAuto(long nativePointer);
public void setHeightAuto() {
jni_YGNodeStyleSetHeightAuto(mNativePointer);
}
private static native Object jni_YGNodeStyleGetMinWidth(long nativePointer);
public YogaValue getMinWidth() {
return (YogaValue) jni_YGNodeStyleGetMinWidth(mNativePointer);
}
private static native void jni_YGNodeStyleSetMinWidth(long nativePointer, float minWidth);
public void setMinWidth(float minWidth) {
jni_YGNodeStyleSetMinWidth(mNativePointer, minWidth);
}
private static native void jni_YGNodeStyleSetMinWidthPercent(long nativePointer, float percent);
public void setMinWidthPercent(float percent) {
jni_YGNodeStyleSetMinWidthPercent(mNativePointer, percent);
}
private static native Object jni_YGNodeStyleGetMinHeight(long nativePointer);
public YogaValue getMinHeight() {
return (YogaValue) jni_YGNodeStyleGetMinHeight(mNativePointer);
}
private static native void jni_YGNodeStyleSetMinHeight(long nativePointer, float minHeight);
public void setMinHeight(float minHeight) {
jni_YGNodeStyleSetMinHeight(mNativePointer, minHeight);
}
private static native void jni_YGNodeStyleSetMinHeightPercent(long nativePointer, float percent);
public void setMinHeightPercent(float percent) {
jni_YGNodeStyleSetMinHeightPercent(mNativePointer, percent);
}
private static native Object jni_YGNodeStyleGetMaxWidth(long nativePointer);
public YogaValue getMaxWidth() {
return (YogaValue) jni_YGNodeStyleGetMaxWidth(mNativePointer);
}
private static native void jni_YGNodeStyleSetMaxWidth(long nativePointer, float maxWidth);
public void setMaxWidth(float maxWidth) {
jni_YGNodeStyleSetMaxWidth(mNativePointer, maxWidth);
}
private static native void jni_YGNodeStyleSetMaxWidthPercent(long nativePointer, float percent);
public void setMaxWidthPercent(float percent) {
jni_YGNodeStyleSetMaxWidthPercent(mNativePointer, percent);
}
private static native Object jni_YGNodeStyleGetMaxHeight(long nativePointer);
public YogaValue getMaxHeight() {
return (YogaValue) jni_YGNodeStyleGetMaxHeight(mNativePointer);
}
private static native void jni_YGNodeStyleSetMaxHeight(long nativePointer, float maxheight);
public void setMaxHeight(float maxheight) {
jni_YGNodeStyleSetMaxHeight(mNativePointer, maxheight);
}
private static native void jni_YGNodeStyleSetMaxHeightPercent(long nativePointer, float percent);
public void setMaxHeightPercent(float percent) {
jni_YGNodeStyleSetMaxHeightPercent(mNativePointer, percent);
}
private static native float jni_YGNodeStyleGetAspectRatio(long nativePointer);
public float getAspectRatio() {
return jni_YGNodeStyleGetAspectRatio(mNativePointer);
}
private static native void jni_YGNodeStyleSetAspectRatio(long nativePointer, float aspectRatio);
public void setAspectRatio(float aspectRatio) {
jni_YGNodeStyleSetAspectRatio(mNativePointer, aspectRatio);
}
public float getLayoutX() {
return mLeft;
}
public float getLayoutY() {
return mTop;
}
public float getLayoutWidth() {
return mWidth;
}
public float getLayoutHeight() {
return mHeight;
}
public boolean getDoesLegacyStretchFlagAffectsLayout() {
return mDoesLegacyStretchFlagAffectsLayout;
}
public float getLayoutMargin(YogaEdge edge) {
switch (edge) {
case LEFT:
return mMarginLeft;
case TOP:
return mMarginTop;
case RIGHT:
return mMarginRight;
case BOTTOM:
return mMarginBottom;
case START:
return getLayoutDirection() == YogaDirection.RTL ? mMarginRight : mMarginLeft;
case END:
return getLayoutDirection() == YogaDirection.RTL ? mMarginLeft : mMarginRight;
default:
throw new IllegalArgumentException("Cannot get layout margins of multi-edge shorthands");
}
}
public float getLayoutPadding(YogaEdge edge) {
switch (edge) {
case LEFT:
return mPaddingLeft;
case TOP:
return mPaddingTop;
case RIGHT:
return mPaddingRight;
case BOTTOM:
return mPaddingBottom;
case START:
return getLayoutDirection() == YogaDirection.RTL ? mPaddingRight : mPaddingLeft;
case END:
return getLayoutDirection() == YogaDirection.RTL ? mPaddingLeft : mPaddingRight;
default:
throw new IllegalArgumentException("Cannot get layout paddings of multi-edge shorthands");
}
}
public float getLayoutBorder(YogaEdge edge) {
switch (edge) {
case LEFT:
return mBorderLeft;
case TOP:
return mBorderTop;
case RIGHT:
return mBorderRight;
case BOTTOM:
return mBorderBottom;
case START:
return getLayoutDirection() == YogaDirection.RTL ? mBorderRight : mBorderLeft;
case END:
return getLayoutDirection() == YogaDirection.RTL ? mBorderLeft : mBorderRight;
default:
throw new IllegalArgumentException("Cannot get layout border of multi-edge shorthands");
}
}
public YogaDirection getLayoutDirection() {
return YogaDirection.fromInt(mLayoutDirection);
}
private static native void jni_YGNodeSetHasMeasureFunc(long nativePointer, boolean hasMeasureFunc);
public void setMeasureFunction(YogaMeasureFunction measureFunction) {
mMeasureFunction = measureFunction;
jni_YGNodeSetHasMeasureFunc(mNativePointer, measureFunction != null);
}
// Implementation Note: Why this method needs to stay final
//
// We cache the jmethodid for this method in Yoga code. This means that even if a subclass
// were to override measure, we'd still call this implementation from layout code since the
// overriding method will have a different jmethodid. This is final to prevent that mistake.
@DoNotStrip
public final long measure(float width, int widthMode, float height, int heightMode) {
if (!isMeasureDefined()) {
throw new RuntimeException("Measure function isn't defined!");
}
return mMeasureFunction.measure(
this,
width,
YogaMeasureMode.fromInt(widthMode),
height,
YogaMeasureMode.fromInt(heightMode));
}
private static native void jni_YGNodeSetHasBaselineFunc(long nativePointer, boolean hasMeasureFunc);
public void setBaselineFunction(YogaBaselineFunction baselineFunction) {
mBaselineFunction = baselineFunction;
jni_YGNodeSetHasBaselineFunc(mNativePointer, baselineFunction != null);
}
@DoNotStrip
public final float baseline(float width, float height) {
return mBaselineFunction.baseline(this, width, height);
}
public boolean isMeasureDefined() {
return mMeasureFunction != null;
}
public void setData(Object data) {
mData = data;
}
public Object getData() {
return mData;
}
private static native void jni_YGNodePrint(long nativePointer);
/**
* Use the set logger (defaults to adb log) to print out the styles, children, and computed
* layout of the tree rooted at this node.
*/
public void print() {
jni_YGNodePrint(mNativePointer);
}
private static native void jni_YGNodeSetStyleInputs(long nativePointer, float[] styleInputsArray, int size);
public void setStyleInputs(float[] styleInputsArray, int size) {
jni_YGNodeSetStyleInputs(mNativePointer, styleInputsArray, size);
}
/**
* This method replaces the child at childIndex position with the newNode received by parameter.
* This is different than calling removeChildAt and addChildAt because this method ONLY replaces
* the child in the mChildren datastructure. @DoNotStrip: called from JNI
*
* @return the nativePointer of the newNode {@linl YogaNode}
*/
@DoNotStrip
private final long replaceChild(YogaNodeJNI newNode, int childIndex) {
if (mChildren == null) {
throw new IllegalStateException("Cannot replace child. YogaNode does not have children");
}
mChildren.remove(childIndex);
mChildren.add(childIndex, newNode);
newNode.mOwner = this;
return newNode.mNativePointer;
}
}

View File

@@ -0,0 +1,53 @@
/**
* 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.
*/
package com.facebook.yoga;
import com.facebook.proguard.annotations.DoNotStrip;
@DoNotStrip
public class YogaStyleInputs {
public static final short LAYOUT_DIRECTION = 0;
public static final short FLEX_DIRECTION = 1;
public static final short FLEX = 2;
public static final short FLEX_GROW = 3;
public static final short FLEX_SHRINK = 4;
public static final short FLEX_BASIS = 5;
public static final short FLEX_BASIS_PERCENT = 6;
public static final short FLEX_BASIS_AUTO = 7;
public static final short FLEX_WRAP = 8;
public static final short WIDTH = 9;
public static final short WIDTH_PERCENT = 10;
public static final short WIDTH_AUTO = 11;
public static final short MIN_WIDTH = 12;
public static final short MIN_WIDTH_PERCENT = 13;
public static final short MAX_WIDTH = 14;
public static final short MAX_WIDTH_PERCENT = 15;
public static final short HEIGHT = 16;
public static final short HEIGHT_PERCENT = 17;
public static final short HEIGHT_AUTO = 18;
public static final short MIN_HEIGHT = 19;
public static final short MIN_HEIGHT_PERCENT = 20;
public static final short MAX_HEIGHT = 21;
public static final short MAX_HEIGHT_PERCENT = 22;
public static final short JUSTIFY_CONTENT = 23;
public static final short ALIGN_ITEMS = 24;
public static final short ALIGN_SELF = 25;
public static final short ALIGN_CONTENT = 26;
public static final short POSITION_TYPE = 27;
public static final short ASPECT_RATIO = 28;
public static final short OVERFLOW = 29;
public static final short DISPLAY = 30;
public static final short MARGIN = 31;
public static final short MARGIN_PERCENT = 32;
public static final short MARGIN_AUTO = 33;
public static final short PADDING = 34;
public static final short PADDING_PERCENT = 35;
public static final short BORDER = 36;
public static final short POSITION = 37;
public static final short POSITION_PERCENT = 38;
public static final short IS_REFERENCE_BASELINE = 39;
}

File diff suppressed because it is too large Load Diff

44
java/jni/YGJTypes.cpp Normal file
View File

@@ -0,0 +1,44 @@
/**
* 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 "YGJTypes.h"
using facebook::jni::alias_ref;
using facebook::jni::local_ref;
jfloat JYogaNode::baseline(jfloat width, jfloat height) {
static auto javaMethod =
javaClassLocal()->getMethod<jfloat(jfloat, jfloat)>("baseline");
return javaMethod(self(), width, height);
}
jlong JYogaNode::measure(
jfloat width,
jint widthMode,
jfloat height,
jint heightMode) {
static auto javaMethod =
javaClassLocal()->getMethod<jlong(jfloat, jint, jfloat, jint)>("measure");
return javaMethod(self(), width, widthMode, height, heightMode);
}
facebook::jni::local_ref<JYogaLogLevel> JYogaLogLevel::fromInt(jint logLevel) {
static auto javaMethod =
javaClassStatic()->getStaticMethod<alias_ref<JYogaLogLevel>(jint)>(
"fromInt");
return javaMethod(javaClassStatic(), logLevel);
}
void JYogaLogger::log(
facebook::jni::alias_ref<JYogaNode> node,
facebook::jni::alias_ref<JYogaLogLevel> logLevel,
jstring message) {
static auto javaMethod =
javaClassLocal()
->getMethod<void(
alias_ref<JYogaNode>, alias_ref<JYogaLogLevel>, jstring)>("log");
javaMethod(self(), node, logLevel, message);
}

42
java/jni/YGJTypes.h Normal file
View File

@@ -0,0 +1,42 @@
/**
* 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 <fb/fbjni.h>
#include <yoga/YGValue.h>
struct JYogaNode : public facebook::jni::JavaClass<JYogaNode> {
static constexpr auto kJavaDescriptor = "Lcom/facebook/yoga/YogaNodeJNI;";
jfloat baseline(jfloat width, jfloat height);
jlong measure(jfloat width, jint widthMode, jfloat height, jint heightMode);
};
struct JYogaConfig : public facebook::jni::JavaClass<JYogaConfig> {
static constexpr auto kJavaDescriptor = "Lcom/facebook/yoga/YogaConfig;";
};
struct JYogaLogLevel : public facebook::jni::JavaClass<JYogaLogLevel> {
static constexpr auto kJavaDescriptor = "Lcom/facebook/yoga/YogaLogLevel;";
static facebook::jni::local_ref<JYogaLogLevel> fromInt(jint);
};
struct JYogaLogger : public facebook::jni::JavaClass<JYogaLogger> {
static constexpr auto kJavaDescriptor = "Lcom/facebook/yoga/YogaLogger";
void log(
facebook::jni::alias_ref<JYogaNode>,
facebook::jni::alias_ref<JYogaLogLevel>,
jstring);
};
struct JYogaValue : public facebook::jni::JavaClass<JYogaValue> {
constexpr static auto kJavaDescriptor = "Lcom/facebook/yoga/YogaValue;";
static facebook::jni::local_ref<javaobject> create(YGValue value) {
return newInstance(value.value, static_cast<int>(value.unit));
}
};

View File

@@ -0,0 +1,500 @@
/**
* 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.
*/
package com.facebook.yoga;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import static org.junit.Assert.assertEquals;
@RunWith(Parameterized.class)
public class BatchingAPITests {
@Parameterized.Parameters(name = "{0}")
public static Iterable<TestParametrization.NodeFactory> nodeFactories() {
return TestParametrization.nodeFactories();
}
@Parameterized.Parameter public TestParametrization.NodeFactory mNodeFactory;
@Test
public void testStyleInputLayoutDirection() {
YogaConfig config = new YogaConfig();
final YogaNode root = createNode(config);
float[] arr = new float[]{YogaStyleInputs.LAYOUT_DIRECTION, YogaDirection.LTR.intValue()};
root.setStyleInputs(arr, 2);
root.calculateLayout(YogaConstants.UNDEFINED, YogaConstants.UNDEFINED);
assertEquals(root.getLayoutDirection(), YogaDirection.LTR);
}
@Test
public void testStyleInputFlexDirection() {
YogaConfig config = new YogaConfig();
final YogaNode root = createNode(config);
float[] arr = new float[]{YogaStyleInputs.FLEX_DIRECTION, YogaFlexDirection.ROW.intValue()};
root.setStyleInputs(arr, 2);
assertEquals(root.getFlexDirection(), YogaFlexDirection.ROW);
}
@Test
public void testStyleInputFlex() {
YogaConfig config = new YogaConfig();
final YogaNode root = createNode(config);
float[] arr = new float[]{YogaStyleInputs.FLEX, 5f};
root.setStyleInputs(arr, 2);
assertEquals(root.getFlex(), 5f, 0.0f);
}
@Test
public void testStyleInputFlexGrow() {
YogaConfig config = new YogaConfig();
final YogaNode root = createNode(config);
float[] arr = new float[]{YogaStyleInputs.FLEX_GROW, 5f};
root.setStyleInputs(arr, 2);
assertEquals(root.getFlexGrow(), 5f, 0.0f);
}
@Test
public void testStyleInputFlexShrink() {
YogaConfig config = new YogaConfig();
final YogaNode root = createNode(config);
float[] arr = new float[]{YogaStyleInputs.FLEX_SHRINK, 5f};
root.setStyleInputs(arr, 2);
assertEquals(root.getFlexShrink(), 5f, 0.0f);
}
@Test
public void testStyleInputFlexBasis() {
YogaConfig config = new YogaConfig();
final YogaNode root = createNode(config);
float[] arr = new float[]{YogaStyleInputs.FLEX_BASIS, 5f};
root.setStyleInputs(arr, 2);
assertEquals(root.getFlexBasis(), new YogaValue(5f, YogaUnit.POINT));
}
@Test
public void testStyleInputFlexBasisPercent() {
YogaConfig config = new YogaConfig();
final YogaNode root = createNode(config);
float[] arr = new float[]{YogaStyleInputs.FLEX_BASIS_PERCENT, 5f};
root.setStyleInputs(arr, 2);
assertEquals(root.getFlexBasis(), new YogaValue(5f, YogaUnit.PERCENT));
}
@Test
public void testStyleInputFlexBasisAuto() {
YogaConfig config = new YogaConfig();
final YogaNode root = createNode(config);
float[] arr = new float[]{YogaStyleInputs.FLEX_BASIS_AUTO};
root.setStyleInputs(arr, 1);
assertEquals(root.getFlexBasis(), YogaValue.AUTO);
}
@Test
public void testStyleInputFlexWrap() {
YogaConfig config = new YogaConfig();
final YogaNode root = createNode(config);
float[] arr = new float[]{YogaStyleInputs.FLEX_WRAP, YogaWrap.WRAP.intValue()};
root.setStyleInputs(arr, 2);
assertEquals(root.getWrap(), YogaWrap.WRAP);
}
@Test
public void testStyleInputWidth() {
YogaConfig config = new YogaConfig();
final YogaNode root = createNode(config);
float[] arr = new float[]{YogaStyleInputs.WIDTH, 50f};
root.setStyleInputs(arr, 2);
assertEquals(root.getWidth(), new YogaValue(50f, YogaUnit.POINT));
}
@Test
public void testStyleInputWidthPercent() {
YogaConfig config = new YogaConfig();
final YogaNode root = createNode(config);
float[] arr = new float[]{YogaStyleInputs.WIDTH_PERCENT, 5f};
root.setStyleInputs(arr, 2);
assertEquals(root.getWidth(), new YogaValue(5f, YogaUnit.PERCENT));
}
@Test
public void testStyleInputWidthAuto() {
YogaConfig config = new YogaConfig();
final YogaNode root = createNode(config);
float[] arr = new float[]{YogaStyleInputs.WIDTH_AUTO};
root.setStyleInputs(arr, 1);
assertEquals(root.getWidth(), YogaValue.AUTO);
}
@Test
public void testStyleInputMinWidth() {
YogaConfig config = new YogaConfig();
final YogaNode root = createNode(config);
float[] arr = new float[]{YogaStyleInputs.MIN_WIDTH, 50f};
root.setStyleInputs(arr, 2);
assertEquals(root.getMinWidth(), new YogaValue(50f, YogaUnit.POINT));
}
@Test
public void testStyleInputMinWidthPercent() {
YogaConfig config = new YogaConfig();
final YogaNode root = createNode(config);
float[] arr = new float[]{YogaStyleInputs.MIN_WIDTH_PERCENT, 5f};
root.setStyleInputs(arr, 2);
assertEquals(root.getMinWidth(), new YogaValue(5f, YogaUnit.PERCENT));
}
@Test
public void testStyleInputMaxWidth() {
YogaConfig config = new YogaConfig();
final YogaNode root = createNode(config);
float[] arr = new float[]{YogaStyleInputs.MAX_WIDTH, 50f};
root.setStyleInputs(arr, 2);
assertEquals(root.getMaxWidth(), new YogaValue(50f, YogaUnit.POINT));
}
@Test
public void testStyleInputMaxWidthPercent() {
YogaConfig config = new YogaConfig();
final YogaNode root = createNode(config);
float[] arr = new float[]{YogaStyleInputs.MAX_WIDTH_PERCENT, 5f};
root.setStyleInputs(arr, 2);
assertEquals(root.getMaxWidth(), new YogaValue(5f, YogaUnit.PERCENT));
}
@Test
public void testStyleInputHeight() {
YogaConfig config = new YogaConfig();
final YogaNode root = createNode(config);
float[] arr = new float[]{YogaStyleInputs.HEIGHT, 50f};
root.setStyleInputs(arr, 2);
assertEquals(root.getHeight(), new YogaValue(50f, YogaUnit.POINT));
}
@Test
public void testStyleInputHeightPercent() {
YogaConfig config = new YogaConfig();
final YogaNode root = createNode(config);
float[] arr = new float[]{YogaStyleInputs.HEIGHT_PERCENT, 5f};
root.setStyleInputs(arr, 2);
assertEquals(root.getHeight(), new YogaValue(5f, YogaUnit.PERCENT));
}
@Test
public void testStyleInputHeightAuto() {
YogaConfig config = new YogaConfig();
final YogaNode root = createNode(config);
float[] arr = new float[]{YogaStyleInputs.HEIGHT_AUTO};
root.setStyleInputs(arr, 1);
assertEquals(root.getHeight(), YogaValue.AUTO);
}
@Test
public void testStyleInputMinHeight() {
YogaConfig config = new YogaConfig();
final YogaNode root = createNode(config);
float[] arr = new float[]{YogaStyleInputs.MIN_HEIGHT, 50f};
root.setStyleInputs(arr, 2);
assertEquals(root.getMinHeight(), new YogaValue(50f, YogaUnit.POINT));
}
@Test
public void testStyleInputMinHeightPercent() {
YogaConfig config = new YogaConfig();
final YogaNode root = createNode(config);
float[] arr = new float[]{YogaStyleInputs.MIN_HEIGHT_PERCENT, 5f};
root.setStyleInputs(arr, 2);
assertEquals(root.getMinHeight(), new YogaValue(5f, YogaUnit.PERCENT));
}
@Test
public void testStyleInputMaxHeight() {
YogaConfig config = new YogaConfig();
final YogaNode root = createNode(config);
float[] arr = new float[]{YogaStyleInputs.MAX_HEIGHT, 50f};
root.setStyleInputs(arr, 2);
assertEquals(root.getMaxHeight(), new YogaValue(50f, YogaUnit.POINT));
}
@Test
public void testStyleInputMaxHeightPercent() {
YogaConfig config = new YogaConfig();
final YogaNode root = createNode(config);
float[] arr = new float[]{YogaStyleInputs.MAX_HEIGHT_PERCENT, 5f};
root.setStyleInputs(arr, 2);
assertEquals(root.getMaxHeight(), new YogaValue(5f, YogaUnit.PERCENT));
}
@Test
public void testStyleInputJustiyContent() {
YogaConfig config = new YogaConfig();
final YogaNode root = createNode(config);
float[] arr = new float[]{YogaStyleInputs.JUSTIFY_CONTENT, YogaJustify.CENTER.intValue()};
root.setStyleInputs(arr, 2);
assertEquals(root.getJustifyContent(), YogaJustify.CENTER);
}
@Test
public void testStyleInputAlignItems() {
YogaConfig config = new YogaConfig();
final YogaNode root = createNode(config);
float[] arr = new float[]{YogaStyleInputs.ALIGN_ITEMS, YogaAlign.BASELINE.intValue()};
root.setStyleInputs(arr, 2);
assertEquals(root.getAlignItems(), YogaAlign.BASELINE);
}
@Test
public void testStyleInputAlignSelf() {
YogaConfig config = new YogaConfig();
final YogaNode root = createNode(config);
float[] arr = new float[]{YogaStyleInputs.ALIGN_SELF, YogaAlign.BASELINE.intValue()};
root.setStyleInputs(arr, 2);
assertEquals(root.getAlignSelf(), YogaAlign.BASELINE);
}
@Test
public void testStyleInputAlignContent() {
YogaConfig config = new YogaConfig();
final YogaNode root = createNode(config);
float[] arr = new float[]{YogaStyleInputs.ALIGN_CONTENT, YogaAlign.BASELINE.intValue()};
root.setStyleInputs(arr, 2);
assertEquals(root.getAlignContent(), YogaAlign.BASELINE);
}
@Test
public void testStyleInputPositionType() {
YogaConfig config = new YogaConfig();
final YogaNode root = createNode(config);
float[] arr = new float[]{YogaStyleInputs.POSITION_TYPE, YogaPositionType.ABSOLUTE.intValue()};
root.setStyleInputs(arr, 2);
assertEquals(root.getPositionType(), YogaPositionType.ABSOLUTE);
}
@Test
public void testStyleInputAspectRatio() {
YogaConfig config = new YogaConfig();
final YogaNode root = createNode(config);
float[] arr = new float[]{YogaStyleInputs.ASPECT_RATIO, 2f};
root.setStyleInputs(arr, 2);
assertEquals(root.getAspectRatio(), 2f, 0.0f);
}
@Test
public void testStyleInputOverflow() {
YogaConfig config = new YogaConfig();
final YogaNode root = createNode(config);
float[] arr = new float[]{YogaStyleInputs.OVERFLOW, YogaOverflow.HIDDEN.intValue()};
root.setStyleInputs(arr, 2);
assertEquals(root.getOverflow(), YogaOverflow.HIDDEN);
}
@Test
public void testStyleInputDisplay() {
YogaConfig config = new YogaConfig();
final YogaNode root = createNode(config);
float[] arr = new float[]{YogaStyleInputs.DISPLAY, YogaDisplay.NONE.intValue()};
root.setStyleInputs(arr, 2);
assertEquals(root.getDisplay(), YogaDisplay.NONE);
}
@Test
public void testStyleInputMargin() {
YogaConfig config = new YogaConfig();
final YogaNode root = createNode(config);
float[] arr = new float[]{YogaStyleInputs.MARGIN, YogaEdge.LEFT.intValue(), 12f};
root.setStyleInputs(arr, 3);
assertEquals(root.getMargin(YogaEdge.LEFT), new YogaValue(12f, YogaUnit.POINT));
}
@Test
public void testStyleInputMarginPercent() {
YogaConfig config = new YogaConfig();
final YogaNode root = createNode(config);
float[] arr = new float[]{YogaStyleInputs.MARGIN_PERCENT, YogaEdge.LEFT.intValue(), 12f};
root.setStyleInputs(arr, 3);
assertEquals(root.getMargin(YogaEdge.LEFT), new YogaValue(12f, YogaUnit.PERCENT));
}
@Test
public void testStyleInputMarginAuto() {
YogaConfig config = new YogaConfig();
final YogaNode root = createNode(config);
float[] arr = new float[]{YogaStyleInputs.MARGIN_AUTO, YogaEdge.LEFT.intValue()};
root.setStyleInputs(arr, 2);
assertEquals(root.getMargin(YogaEdge.LEFT), YogaValue.AUTO);
}
@Test
public void testStyleInputPadding() {
YogaConfig config = new YogaConfig();
final YogaNode root = createNode(config);
float[] arr = new float[]{YogaStyleInputs.PADDING, YogaEdge.LEFT.intValue(), 12f};
root.setStyleInputs(arr, 3);
assertEquals(root.getPadding(YogaEdge.LEFT), new YogaValue(12f, YogaUnit.POINT));
}
@Test
public void testStyleInputPaddingPercent() {
YogaConfig config = new YogaConfig();
final YogaNode root = createNode(config);
float[] arr = new float[]{YogaStyleInputs.PADDING_PERCENT, YogaEdge.LEFT.intValue(), 12f};
root.setStyleInputs(arr, 3);
assertEquals(root.getPadding(YogaEdge.LEFT), new YogaValue(12f, YogaUnit.PERCENT));
}
@Test
public void testStyleInputBorder() {
YogaConfig config = new YogaConfig();
final YogaNode root = createNode(config);
float[] arr = new float[]{YogaStyleInputs.BORDER, YogaEdge.LEFT.intValue(), 12f};
root.setStyleInputs(arr, 3);
assertEquals(root.getBorder(YogaEdge.LEFT), 12f, 0.0f);
}
@Test
public void testStyleInputPosition() {
YogaConfig config = new YogaConfig();
final YogaNode root = createNode(config);
float[] arr = new float[]{YogaStyleInputs.POSITION, YogaEdge.LEFT.intValue(), 12f};
root.setStyleInputs(arr, 3);
assertEquals(root.getPosition(YogaEdge.LEFT), new YogaValue(12f, YogaUnit.POINT));
}
@Test
public void testStyleInputPositionPercent() {
YogaConfig config = new YogaConfig();
final YogaNode root = createNode(config);
float[] arr = new float[]{YogaStyleInputs.POSITION_PERCENT, YogaEdge.LEFT.intValue(), 12f};
root.setStyleInputs(arr, 3);
assertEquals(root.getPosition(YogaEdge.LEFT), new YogaValue(12f, YogaUnit.PERCENT));
}
@Test
public void testStyleInputIsReferenceBaseline() {
YogaConfig config = new YogaConfig();
final YogaNode root = createNode(config);
float[] arr = new float[]{YogaStyleInputs.IS_REFERENCE_BASELINE, 1f};
root.setStyleInputs(arr, 2);
assertEquals(root.isReferenceBaseline(), true);
}
@Test
public void testStyleInputs() {
YogaConfig config = new YogaConfig();
final YogaNode root = createNode(config);
int count = 0;
float[] arr = new float[100];
arr[count++] = YogaStyleInputs.FLEX_DIRECTION;
arr[count++] = YogaFlexDirection.ROW.intValue();
arr[count++] = YogaStyleInputs.FLEX;
arr[count++] = 5f;
arr[count++] = YogaStyleInputs.FLEX_GROW;
arr[count++] = 5f;
arr[count++] = YogaStyleInputs.FLEX_SHRINK;
arr[count++] = 5f;
arr[count++] = YogaStyleInputs.FLEX_BASIS_AUTO;
arr[count++] = YogaStyleInputs.FLEX_WRAP;
arr[count++] = YogaWrap.WRAP.intValue();
arr[count++] = YogaStyleInputs.WIDTH;
arr[count++] = 50f;
arr[count++] = YogaStyleInputs.MIN_WIDTH;
arr[count++] = 50f;
arr[count++] = YogaStyleInputs.MAX_WIDTH;
arr[count++] = 50f;
arr[count++] = YogaStyleInputs.HEIGHT;
arr[count++] = 5f;
arr[count++] = YogaStyleInputs.MIN_HEIGHT;
arr[count++] = 50f;
arr[count++] = YogaStyleInputs.MAX_HEIGHT;
arr[count++] = 50f;
arr[count++] = YogaStyleInputs.JUSTIFY_CONTENT;
arr[count++] = YogaJustify.CENTER.intValue();
arr[count++] = YogaStyleInputs.ALIGN_ITEMS;
arr[count++] = YogaAlign.BASELINE.intValue();
arr[count++] = YogaStyleInputs.ALIGN_SELF;
arr[count++] = YogaAlign.BASELINE.intValue();
arr[count++] = YogaStyleInputs.ALIGN_CONTENT;
arr[count++] = YogaAlign.BASELINE.intValue();
arr[count++] = YogaStyleInputs.POSITION_TYPE;
arr[count++] = YogaPositionType.ABSOLUTE.intValue();
arr[count++] = YogaStyleInputs.ASPECT_RATIO;
arr[count++] = 2f;
arr[count++] = YogaStyleInputs.OVERFLOW;
arr[count++] = YogaOverflow.HIDDEN.intValue();
arr[count++] = YogaStyleInputs.DISPLAY;
arr[count++] = YogaDisplay.NONE.intValue();
arr[count++] = YogaStyleInputs.MARGIN_AUTO;
arr[count++] = YogaEdge.LEFT.intValue();
arr[count++] = YogaStyleInputs.PADDING;
arr[count++] = YogaEdge.LEFT.intValue();
arr[count++] = 12f;
arr[count++] = YogaStyleInputs.BORDER;
arr[count++] = YogaEdge.LEFT.intValue();
arr[count++] = 12f;
arr[count++] = YogaStyleInputs.POSITION_PERCENT;
arr[count++] = YogaEdge.LEFT.intValue();
arr[count++] = 12f;
arr[count++] = YogaStyleInputs.IS_REFERENCE_BASELINE;
arr[count++] = 1f;
root.setStyleInputs(arr, count);
assertEquals(root.getFlexDirection(), YogaFlexDirection.ROW);
assertEquals(root.getFlex(), 5f, 0.0f);
assertEquals(root.getFlexGrow(), 5f, 0.0f);
assertEquals(root.getFlexShrink(), 5f, 0.0f);
assertEquals(root.getFlexBasis(), YogaValue.AUTO);
assertEquals(root.getWrap(), YogaWrap.WRAP);
assertEquals(root.getWidth(), new YogaValue(50f, YogaUnit.POINT));
assertEquals(root.getMinWidth(), new YogaValue(50f, YogaUnit.POINT));
assertEquals(root.getMaxWidth(), new YogaValue(50f, YogaUnit.POINT));
assertEquals(root.getHeight(), new YogaValue(5f, YogaUnit.POINT));
assertEquals(root.getMinHeight(), new YogaValue(50f, YogaUnit.POINT));
assertEquals(root.getMaxHeight(), new YogaValue(50f, YogaUnit.POINT));
assertEquals(root.getJustifyContent(), YogaJustify.CENTER);
assertEquals(root.getAlignItems(), YogaAlign.BASELINE);
assertEquals(root.getAlignSelf(), YogaAlign.BASELINE);
assertEquals(root.getAlignContent(), YogaAlign.BASELINE);
assertEquals(root.getPositionType(), YogaPositionType.ABSOLUTE);
assertEquals(root.getAspectRatio(), 2f, 0.0f);
assertEquals(root.getOverflow(), YogaOverflow.HIDDEN);
assertEquals(root.getDisplay(), YogaDisplay.NONE);
assertEquals(root.getMargin(YogaEdge.LEFT), YogaValue.AUTO);
assertEquals(root.getPadding(YogaEdge.LEFT), new YogaValue(12f, YogaUnit.POINT));
assertEquals(root.getBorder(YogaEdge.LEFT), 12f, 0.0f);
assertEquals(root.getPosition(YogaEdge.LEFT), new YogaValue(12f, YogaUnit.PERCENT));
assertEquals(root.isReferenceBaseline(), true);
}
private YogaNode createNode(YogaConfig config) {
return mNodeFactory.create(config);
}
}

View File

@@ -15,12 +15,12 @@ public class TestParametrization {
NodeFactory nodeFactory = new NodeFactory() {
@Override
public YogaNode create() {
return new YogaNode();
return YogaNode.create();
}
@Override
public YogaNode create(YogaConfig config) {
return new YogaNode(config);
return YogaNode.create(config);
}
@Override

View File

@@ -30,9 +30,9 @@ public class YogaNodeTest {
@Test
public void testInit() {
final int refCount = YogaNode.jni_YGNodeGetInstanceCount();
final int refCount = YogaNodeJNI.jni_YGNodeGetInstanceCount();
final YogaNode node = createNode();
assertEquals(refCount + 1, YogaNode.jni_YGNodeGetInstanceCount());
assertEquals(refCount + 1, YogaNodeJNI.jni_YGNodeGetInstanceCount());
}
@Test
@@ -233,112 +233,6 @@ public class YogaNodeTest {
}
}
@Test
public void testCloneNode() throws Exception {
YogaConfig config = new YogaConfig();
YogaNode root = createNode(config);
YogaNode child = createNode(config);
YogaNode grandChild = createNode(config);
root.addChildAt(child, 0);
child.addChildAt(grandChild, 0);
child.setFlexDirection(YogaFlexDirection.ROW);
YogaNode clonedChild = child.clone();
assertNotSame(clonedChild, child);
assertEquals(YogaFlexDirection.ROW, child.getFlexDirection());
assertEquals(child.getFlexDirection(), clonedChild.getFlexDirection());
// Verify the cloning is shallow on the List of children
assertEquals(1, child.getChildCount());
assertEquals(child.getChildCount(), clonedChild.getChildCount());
assertEquals(child.getChildAt(0), clonedChild.getChildAt(0));
child.removeChildAt(0);
assertEquals(0, child.getChildCount());
assertEquals(1, clonedChild.getChildCount());
}
@Test
public void testCloneWithNewChildren() throws Exception {
YogaConfig config = new YogaConfig();
YogaNode root = createNode(config);
YogaNode child = createNode(config);
YogaNode grandChild = createNode(config);
root.addChildAt(child, 0);
child.addChildAt(grandChild, 0);
child.setFlexDirection(YogaFlexDirection.ROW);
YogaNode clonedChild = child.cloneWithNewChildren();
assertNotSame(clonedChild, child);
assertEquals(YogaFlexDirection.ROW, clonedChild.getFlexDirection());
assertEquals(child.getFlexDirection(), clonedChild.getFlexDirection());
assertEquals(0, clonedChild.getChildCount());
assertEquals(1, child.getChildCount());
}
@Test
public void testCloneNodeListener() throws Exception {
final AtomicBoolean onNodeClonedExecuted = new AtomicBoolean(false);
YogaConfig config = new YogaConfig();
config.setOnCloneNode(
new YogaNodeCloneFunction() {
@Override
public YogaNode cloneNode(YogaNode oldNode, YogaNode owner, int childIndex) {
onNodeClonedExecuted.set(true);
return oldNode.clone();
}
});
YogaNode root = createNode(config);
root.setWidth(100f);
root.setHeight(100f);
YogaNode child0 = createNode(config);
root.addChildAt(child0, 0);
child0.setWidth(50f);
root.calculateLayout(YogaConstants.UNDEFINED, YogaConstants.UNDEFINED);
// Force a clone to happen.
final YogaNode root2 = root.clone();
root2.setWidth(200f);
root2.calculateLayout(YogaConstants.UNDEFINED, YogaConstants.UNDEFINED);
assertTrue(onNodeClonedExecuted.get());
assertEquals(1, root2.getChildCount());
YogaNode clonedNode = root2.getChildAt(0);
assertNotSame(child0, clonedNode);
assertEquals(child0.getWidth(), clonedNode.getWidth());
assertEquals(50f, clonedNode.getWidth().value, 0.01f);
}
@Test
public void testOnNodeClonedLeak() throws Exception {
YogaConfig config = new YogaConfig();
config.setOnCloneNode(
new YogaNodeCloneFunction() {
@Override
public YogaNode cloneNode(YogaNode oldNode, YogaNode owner, int childIndex) {
return oldNode.clone();
}
});
config.setOnCloneNode(null);
WeakReference<Object> ref = new WeakReference<Object>(config);
// noinspection UnusedAssignment
config = null;
// try and free for the next 5 seconds, usually it works after the
// first GC attempt.
for (int i = 0; i < 50; i++) {
System.gc();
if (ref.get() == null) {
// free successfully
return;
}
Thread.sleep(100);
}
fail("YogaConfig leaked");
}
@Test
public void testFlagShouldDiffLayoutWithoutLegacyStretchBehaviour() throws Exception {
YogaConfig config = new YogaConfig();
@@ -359,7 +253,7 @@ public class YogaNodeTest {
root_child0_child0_child0.setFlexShrink(1);
root_child0_child0.addChildAt(root_child0_child0_child0, 0);
root.calculateLayout(YogaConstants.UNDEFINED, YogaConstants.UNDEFINED);
assertFalse(root.getDoesLegacyStretchFlagAffectsLayout());
assertFalse(((YogaNodeJNI) root).getDoesLegacyStretchFlagAffectsLayout());
}
@Test

View File

@@ -234,9 +234,9 @@ class FBEXPORT JClass : public JavaClass<JClass, JObject, jclass> {
/// makeNativeMethod("nativeMethodWithExplicitDescriptor",
/// "(Lcom/facebook/example/MyClass;)V",
/// methodWithExplicitDescriptor),
/// makeCriticalNativeMethod("criticalNativeMethodWithAutomaticDescriptor",
/// makeCriticalNativeMethod_DO_NOT_USE_OR_YOU_WILL_BE_FIRED("criticalNativeMethodWithAutomaticDescriptor",
/// criticalNativeMethodWithAutomaticDescriptor),
/// makeCriticalNativeMethod("criticalNativeMethodWithExplicitDescriptor",
/// makeCriticalNativeMethod_DO_NOT_USE_OR_YOU_WILL_BE_FIRED("criticalNativeMethodWithExplicitDescriptor",
/// "(IIF)Z",
/// criticalNativeMethodWithExplicitDescriptor),
/// });

View File

@@ -96,6 +96,55 @@ struct CriticalMethod<R (*)(Args...)> {
// signature with an exclamation mark.
// Android v8+ supports fast calls by annotating methods:
// https://source.android.com/devices/tech/dalvik/improvements#faster-native-methods
//
// YOU ALMOST CERTAINLY DO NOT NEED THIS AND IT IS DANGEROUS.
// YOU ALMOST CERTAINLY DO NOT NEED THIS AND IT IS DANGEROUS.
// YOU ALMOST CERTAINLY DO NOT NEED THIS AND IT IS DANGEROUS.
// YOU ALMOST CERTAINLY DO NOT NEED THIS AND IT IS DANGEROUS.
// YOU ALMOST CERTAINLY DO NOT NEED THIS AND IT IS DANGEROUS.
//
// "Fast" calls are only on the order of a few dozen NANO-seconds faster than
// regular JNI calls. If your method does almost aaanything of consequence - if
// you loop, if you write to a log, if you call another method, if you even
// simply allocate or deallocate - then the method body will significantly
// outweigh the method overhead.
//
// The difference between a regular JNI method and a "FastJNI" method (as
// they're called inside the runtime) is that a FastJNI method doesn't mark the
// thread as executing native code, and by skipping that avoids the locking and
// thread state check overhead of interacting with the Garbage Collector.
//
// To understand why this is dangerous, you need to understand a bit about the
// GC. In order to perform its work the GC needs to have at least one (usually
// two in modern implementations) "stop the world" checkpoints where it can
// guarantee that all managed-code execution is paused. The VM performs these
// checkpoints at allocations, method boundaries, and each backward branch (ie
// anytime you loop). When the GC wants to run, it will signal to all managed
// threads that they should pause at the next checkpoint, and then it will wait
// for every thread in the system to transition from the "runnable" state into a
// "waiting" state. Once every thread has stopped, the GC thread can perform the
// work it needs to and then it will trigger the execution threads to resume.
//
// JNI methods fit neatly into the above paradigm: They're still methods, so
// they perform GC checkpoints at method entry and method exit. JNI methods also
// perform checkpoints at any JNI boundary crossing - ie, any time you call
// GetObjectField etc. Because access to managed objects from native code is
// tightly controlled, the VM is able to mark threads executing native methods
// into a special "native" state which the GC is able to ignore: It knows they
// can't touch managed objects (without hitting a checkpoint) so it doesn't care
// about them.
//
// JNI critical methods don't perform that "runnable" -> "native" thread state
// transition. Skipping that transition allows them to shave about 20ns off
// their total execution time, but it means that the GC has to wait for them to
// complete before it can move forward. If a critical method begins blocking,
// say on a long loop, or an I/O operation, or on perhaps a mutex, then the GC
// will also block, and because the GC is blocking the entire rest of the VM
// (which is waiting on the GC) will block. If the critical method is blocking
// on a mutex that's already held by the GC - for example, the VM's internal
// weak_globals_lock_ which guards modifications to the weak global reference
// table (and is required in order to create or free a weak_ref<>) - then you
// have a system-wide deadlock.
// prefixes a JNI method signature as android "fast call".
#if defined(__ANDROID__) && defined(FBJNI_WITH_FAST_CALLS)
@@ -106,20 +155,25 @@ struct CriticalMethod<R (*)(Args...)> {
#define makeCriticalNativeMethod3(name, desc, func) \
makeNativeMethod3( \
name, \
FBJNI_PREFIX_FAST_CALL(desc), \
::facebook::jni::detail::CriticalMethod<decltype(&func)>::call<&func>)
name, \
FBJNI_PREFIX_FAST_CALL(desc), \
::facebook::jni::detail::CriticalMethod<decltype(&func)>::call<&func>)
#define makeCriticalNativeMethod2(name, func) \
makeCriticalNativeMethod3( \
name, \
::facebook::jni::detail::CriticalMethod<decltype(&func)>::desc<&func>(), \
func)
#define makeCriticalNativeMethod2(name, func) \
makeCriticalNativeMethod3( \
name, \
::facebook::jni::detail::CriticalMethod<decltype(&func)>::desc<&func>(), \
func)
#define makeCriticalNativeMethodN(a, b, c, count, ...) \
makeCriticalNativeMethod##count
#define makeCriticalNativeMethod(...) \
makeCriticalNativeMethodN(__VA_ARGS__, 3, 2)(__VA_ARGS__)
#define makeCriticalNativeMethodN(a, b, c, count, ...) makeCriticalNativeMethod ## count
// YOU ALMOST CERTAINLY DO NOT NEED THIS AND IT IS DANGEROUS.
// YOU ALMOST CERTAINLY DO NOT NEED THIS AND IT IS DANGEROUS.
// YOU ALMOST CERTAINLY DO NOT NEED THIS AND IT IS DANGEROUS.
// YOU ALMOST CERTAINLY DO NOT NEED THIS AND IT IS DANGEROUS.
// YOU ALMOST CERTAINLY DO NOT NEED THIS AND IT IS DANGEROUS.
// See above for an explanation.
#define makeCriticalNativeMethod_DO_NOT_USE_OR_YOU_WILL_BE_FIRED(...) makeCriticalNativeMethodN(__VA_ARGS__, 3, 2)(__VA_ARGS__)
}}

View File

@@ -55,5 +55,5 @@ function installAndroidSDK {
echo > $ANDROID_HOME/licenses/android-sdk-license
echo -n d56f5187479451eabf01fb78af6dfcb131a6481e > $ANDROID_HOME/licenses/android-sdk-license
installsdk 'build-tools;25.0.3' 'build-tools;26.0.2' 'platforms;android-25' 'platforms;android-26' 'ndk-bundle' 'extras;android;m2repository'
installsdk 'build-tools;25.0.3' 'build-tools;26.0.2' 'platforms;android-25' 'platforms;android-26' 'ndk-bundle' 'extras;android;m2repository' 'cmake;3.6.4111459'
}

View File

@@ -200,7 +200,7 @@ TEST(YogaTest, align_baseline_parent_using_child_in_column_as_reference) {
const YGNodeRef root_child1_child1 =
createYGNode(config, YGFlexDirectionColumn, 500, 400, false);
root_child1_child1->setBaseLineFunc(_baselineFunc);
root_child1_child1->setBaselineFunc(_baselineFunc);
YGNodeSetIsReferenceBaseline(root_child1_child1, true);
YGNodeInsertChild(root_child1, root_child1_child1, 1);
@@ -245,7 +245,7 @@ TEST(
const YGNodeRef root_child1_child1 =
createYGNode(config, YGFlexDirectionColumn, 500, 400, false);
root_child1_child1->setBaseLineFunc(_baselineFunc);
root_child1_child1->setBaselineFunc(_baselineFunc);
YGNodeSetIsReferenceBaseline(root_child1_child1, true);
YGNodeStyleSetPadding(root_child1_child1, YGEdgeLeft, 100);
YGNodeStyleSetPadding(root_child1_child1, YGEdgeRight, 100);
@@ -298,7 +298,7 @@ TEST(
const YGNodeRef root_child1_child1 =
createYGNode(config, YGFlexDirectionColumn, 500, 400, false);
root_child1_child1->setBaseLineFunc(_baselineFunc);
root_child1_child1->setBaselineFunc(_baselineFunc);
YGNodeSetIsReferenceBaseline(root_child1_child1, true);
YGNodeInsertChild(root_child1, root_child1_child1, 1);
@@ -347,7 +347,7 @@ TEST(
const YGNodeRef root_child1_child1 =
createYGNode(config, YGFlexDirectionColumn, 500, 400, false);
root_child1_child1->setBaseLineFunc(_baselineFunc);
root_child1_child1->setBaselineFunc(_baselineFunc);
YGNodeSetIsReferenceBaseline(root_child1_child1, true);
YGNodeInsertChild(root_child1, root_child1_child1, 1);
@@ -392,7 +392,7 @@ TEST(
const YGNodeRef root_child1_child1 =
createYGNode(config, YGFlexDirectionColumn, 500, 400, false);
root_child1_child1->setBaseLineFunc(_baselineFunc);
root_child1_child1->setBaselineFunc(_baselineFunc);
YGNodeSetIsReferenceBaseline(root_child1_child1, true);
YGNodeStyleSetMargin(root_child1_child1, YGEdgeLeft, 100);
YGNodeStyleSetMargin(root_child1_child1, YGEdgeRight, 100);
@@ -439,7 +439,7 @@ TEST(YogaTest, align_baseline_parent_using_child_in_row_as_reference) {
const YGNodeRef root_child1_child1 =
createYGNode(config, YGFlexDirectionColumn, 500, 400, false);
root_child1_child1->setBaseLineFunc(_baselineFunc);
root_child1_child1->setBaselineFunc(_baselineFunc);
YGNodeSetIsReferenceBaseline(root_child1_child1, true);
YGNodeInsertChild(root_child1, root_child1_child1, 1);
@@ -484,7 +484,7 @@ TEST(
const YGNodeRef root_child1_child1 =
createYGNode(config, YGFlexDirectionColumn, 500, 400, false);
root_child1_child1->setBaseLineFunc(_baselineFunc);
root_child1_child1->setBaselineFunc(_baselineFunc);
YGNodeSetIsReferenceBaseline(root_child1_child1, true);
YGNodeStyleSetPadding(root_child1_child1, YGEdgeLeft, 100);
YGNodeStyleSetPadding(root_child1_child1, YGEdgeRight, 100);
@@ -533,7 +533,7 @@ TEST(
const YGNodeRef root_child1_child1 =
createYGNode(config, YGFlexDirectionColumn, 500, 400, false);
root_child1_child1->setBaseLineFunc(_baselineFunc);
root_child1_child1->setBaselineFunc(_baselineFunc);
YGNodeSetIsReferenceBaseline(root_child1_child1, true);
YGNodeStyleSetMargin(root_child1_child1, YGEdgeLeft, 100);
YGNodeStyleSetMargin(root_child1_child1, YGEdgeRight, 100);
@@ -673,7 +673,7 @@ TEST(
const YGNodeRef root_child1_child1 =
createYGNode(config, YGFlexDirectionColumn, 500, 400, false);
root_child1_child1->setBaseLineFunc(_baselineFunc);
root_child1_child1->setBaselineFunc(_baselineFunc);
YGNodeSetIsReferenceBaseline(root_child1_child1, true);
YGNodeInsertChild(root_child1, root_child1_child1, 1);
@@ -724,7 +724,7 @@ TEST(
const YGNodeRef root_child1_child1 =
createYGNode(config, YGFlexDirectionColumn, 500, 400, false);
root_child1_child1->setBaseLineFunc(_baselineFunc);
root_child1_child1->setBaselineFunc(_baselineFunc);
YGNodeSetIsReferenceBaseline(root_child1_child1, true);
YGNodeInsertChild(root_child1, root_child1_child1, 1);

View File

@@ -34,7 +34,7 @@ TEST(YogaTest, align_baseline_customer_func) {
const YGNodeRef root_child1_child0 = YGNodeNew();
root_child1_child0->setContext(&baselineValue);
YGNodeStyleSetWidth(root_child1_child0, 50);
root_child1_child0->setBaseLineFunc(_baseline);
root_child1_child0->setBaselineFunc(_baseline);
YGNodeStyleSetHeight(root_child1_child0, 20);
YGNodeInsertChild(root_child1, root_child1_child0, 0);
YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);

69
tests/YGConfigTest.cpp Normal file
View File

@@ -0,0 +1,69 @@
/**
* 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.
*/
// @Generated by gentest/gentest.rb from gentest/fixtures/YGDisplayTest.html
#include <gtest/gtest.h>
#include <yoga/Yoga.h>
#include <yoga/YGConfig.h>
#include <yoga/YGNode.h>
#include <functional>
#include <memory>
struct ConfigCloningTest : public ::testing::Test {
std::unique_ptr<YGConfig, std::function<void(YGConfig*)>> config;
void SetUp() override;
void TearDown() override;
static YGNode clonedNode;
static YGNodeRef cloneNode(YGNodeRef, YGNodeRef, int) {
return &clonedNode;
}
static YGNodeRef doNotClone(YGNodeRef, YGNodeRef, int) {
return nullptr;
}
};
TEST_F(ConfigCloningTest, uses_values_provided_by_cloning_callback) {
config->setCloneNodeCallback(cloneNode);
YGNode node{}, owner{};
auto clone = config->cloneNode(&node, &owner, 0, nullptr);
ASSERT_EQ(clone, &clonedNode);
}
TEST_F(
ConfigCloningTest,
falls_back_to_regular_cloning_if_callback_returns_null) {
config->setCloneNodeCallback(doNotClone);
YGNode node{}, owner{};
auto clone = config->cloneNode(&node, &owner, 0, nullptr);
ASSERT_NE(clone, nullptr);
YGNodeFree(clone);
}
TEST_F(ConfigCloningTest, can_clone_with_context) {
config->setCloneNodeCallback([](YGNodeRef, YGNodeRef, int, void* context) {
return (YGNodeRef) context;
});
YGNode node{}, owner{}, clone{};
ASSERT_EQ(config->cloneNode(&node, &owner, 0, &clone), &clone);
}
void ConfigCloningTest::SetUp() {
config = {YGConfigNew(), YGConfigFree};
}
void ConfigCloningTest::TearDown() {
config.reset();
}
YGNode ConfigCloningTest::clonedNode = {};

View File

@@ -8,45 +8,51 @@
#include <yoga/YGNode.h>
#include <yoga/Yoga.h>
static YGSize _measure(YGNodeRef node,
float width,
YGMeasureMode widthMode,
float height,
YGMeasureMode heightMode) {
int* measureCount = (int*)node->getContext();
static YGSize _measure(
YGNodeRef node,
float width,
YGMeasureMode widthMode,
float height,
YGMeasureMode heightMode) {
int* measureCount = (int*) node->getContext();
if (measureCount) {
(*measureCount)++;
}
return YGSize{
.width = 10, .height = 10,
.width = 10,
.height = 10,
};
}
static YGSize _simulate_wrapping_text(YGNodeRef node,
float width,
YGMeasureMode widthMode,
float height,
YGMeasureMode heightMode) {
static YGSize _simulate_wrapping_text(
YGNodeRef node,
float width,
YGMeasureMode widthMode,
float height,
YGMeasureMode heightMode) {
if (widthMode == YGMeasureModeUndefined || width >= 68) {
return YGSize{.width = 68, .height = 16};
}
return YGSize{
.width = 50, .height = 32,
.width = 50,
.height = 32,
};
}
static YGSize _measure_assert_negative(YGNodeRef node,
float width,
YGMeasureMode widthMode,
float height,
YGMeasureMode heightMode) {
static YGSize _measure_assert_negative(
YGNodeRef node,
float width,
YGMeasureMode widthMode,
float height,
YGMeasureMode heightMode) {
EXPECT_GE(width, 0);
EXPECT_GE(height, 0);
return YGSize{
.width = 0, .height = 0,
.width = 0,
.height = 0,
};
}
@@ -148,7 +154,6 @@ TEST(YogaTest, dont_measure_when_min_equals_max_percentages) {
YGNodeFreeRecursive(root);
}
TEST(YogaTest, measure_nodes_with_margin_auto_and_stretch) {
const YGNodeRef root = YGNodeNew();
YGNodeStyleSetWidth(root, 500);
@@ -593,7 +598,7 @@ TEST(YogaTest, can_nullify_measure_func_on_any_node) {
const YGNodeRef root = YGNodeNew();
YGNodeInsertChild(root, YGNodeNew(), 0);
root->setMeasureFunc(nullptr);
ASSERT_TRUE(root->getMeasure() == NULL);
ASSERT_TRUE(!root->hasMeasureFunc());
YGNodeFreeRecursive(root);
}
@@ -635,14 +640,16 @@ TEST(YogaTest, cant_call_negative_measure_horizontal) {
YGConfigFree(config);
}
static YGSize _measure_90_10(YGNodeRef node,
float width,
YGMeasureMode widthMode,
float height,
YGMeasureMode heightMode) {
static YGSize _measure_90_10(
YGNodeRef node,
float width,
YGMeasureMode widthMode,
float height,
YGMeasureMode heightMode) {
return YGSize{
.width = 90, .height = 10,
.width = 90,
.height = 10,
};
}

View File

@@ -0,0 +1,146 @@
/**
* 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 <gtest/gtest.h>
#include <yoga/YGNode.h>
#include <ostream>
inline bool operator==(const YGSize& lhs, const YGSize& rhs) {
return lhs.width == rhs.width && lhs.height == rhs.height;
}
void PrintTo(const YGSize&, std::ostream*);
TEST(YGNode, hasMeasureFunc_initial) {
auto n = YGNode{};
ASSERT_FALSE(n.hasMeasureFunc());
}
TEST(YGNode, hasMeasureFunc_with_measure_fn) {
auto n = YGNode{};
n.setMeasureFunc([](YGNode*, float, YGMeasureMode, float, YGMeasureMode) {
return YGSize{};
});
ASSERT_TRUE(n.hasMeasureFunc());
}
TEST(YGNode, measure_with_measure_fn) {
auto n = YGNode{};
n.setMeasureFunc(
[](YGNode*, float w, YGMeasureMode wm, float h, YGMeasureMode hm) {
return YGSize{w * wm, h / hm};
});
ASSERT_EQ(
n.measure(23, YGMeasureModeExactly, 24, YGMeasureModeAtMost, nullptr),
(YGSize{23, 12}));
}
TEST(YGNode, measure_with_context_measure_fn) {
auto n = YGNode{};
n.setMeasureFunc(
[](YGNode*, float, YGMeasureMode, float, YGMeasureMode, void* ctx) {
return *(YGSize*) ctx;
});
auto result = YGSize{123.4, -56.7};
ASSERT_EQ(
n.measure(0, YGMeasureModeUndefined, 0, YGMeasureModeUndefined, &result),
result);
}
TEST(YGNode, switching_measure_fn_types) {
auto n = YGNode{};
n.setMeasureFunc(
[](YGNode*, float, YGMeasureMode, float, YGMeasureMode, void*) {
return YGSize{};
});
n.setMeasureFunc(
[](YGNode*, float w, YGMeasureMode wm, float h, YGMeasureMode hm) {
return YGSize{w * wm, h / hm};
});
ASSERT_EQ(
n.measure(23, YGMeasureModeExactly, 24, YGMeasureModeAtMost, nullptr),
(YGSize{23, 12}));
}
TEST(YGNode, hasMeasureFunc_after_unset) {
auto n = YGNode{};
n.setMeasureFunc([](YGNode*, float, YGMeasureMode, float, YGMeasureMode) {
return YGSize{};
});
n.setMeasureFunc(nullptr);
ASSERT_FALSE(n.hasMeasureFunc());
}
TEST(YGNode, hasMeasureFunc_after_unset_context) {
auto n = YGNode{};
n.setMeasureFunc(
[](YGNode*, float, YGMeasureMode, float, YGMeasureMode, void*) {
return YGSize{};
});
n.setMeasureFunc(nullptr);
ASSERT_FALSE(n.hasMeasureFunc());
}
TEST(YGNode, hasBaselineFunc_initial) {
auto n = YGNode{};
ASSERT_FALSE(n.hasBaselineFunc());
}
TEST(YGNode, hasBaselineFunc_with_baseline_fn) {
auto n = YGNode{};
n.setBaselineFunc([](YGNode*, float, float) { return 0.0f; });
ASSERT_TRUE(n.hasBaselineFunc());
}
TEST(YGNode, baseline_with_baseline_fn) {
auto n = YGNode{};
n.setBaselineFunc([](YGNode*, float w, float h) { return w + h; });
ASSERT_EQ(n.baseline(1.25f, 2.5f, nullptr), 3.75f);
}
TEST(YGNode, baseline_with_context_baseline_fn) {
auto n = YGNode{};
n.setBaselineFunc([](YGNode*, 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);
}
TEST(YGNode, hasBaselineFunc_after_unset) {
auto n = YGNode{};
n.setBaselineFunc([](YGNode*, float, float) { return 0.0f; });
n.setBaselineFunc(nullptr);
ASSERT_FALSE(n.hasBaselineFunc());
}
TEST(YGNode, hasBaselineFunc_after_unset_context) {
auto n = YGNode{};
n.setBaselineFunc([](YGNode*, float, float, void*) { return 0.0f; });
n.setMeasureFunc(nullptr);
ASSERT_FALSE(n.hasMeasureFunc());
}
TEST(YGNode, switching_baseline_fn_types) {
auto n = YGNode{};
n.setBaselineFunc([](YGNode*, float, float, void*) { return 0.0f; });
n.setBaselineFunc([](YGNode*, float, float) { return 1.0f; });
ASSERT_EQ(n.baseline(1, 2, nullptr), 1.0f);
}
void PrintTo(const YGSize& size, std::ostream* os) {
*os << "YGSize{" << size.width << ", " << size.height << "}";
}

View File

@@ -6,6 +6,7 @@
*/
#include <gtest/gtest.h>
#include <yoga/Yoga.h>
#include <yoga/YGNode.h>
TEST(YogaTest, cloning_shared_root) {
const YGConfigRef config = YGConfigNew();
@@ -104,7 +105,7 @@ TEST(YogaTest, cloning_shared_root) {
YGConfigFree(config);
}
TEST(YogaTest, mutating_children_of_a_clone_clones) {
TEST(YogaTest, mutating_children_of_a_clone_clones_only_after_layout) {
const YGConfigRef config = YGConfigNew();
const YGNodeRef root = YGNodeNewWithConfig(config);
@@ -129,7 +130,7 @@ TEST(YogaTest, mutating_children_of_a_clone_clones) {
ASSERT_EQ(1, YGNodeGetChildCount(root2));
ASSERT_EQ(2, YGNodeGetChildCount(root3));
ASSERT_EQ(root3_child1, YGNodeGetChild(root3, 1));
ASSERT_NE(YGNodeGetChild(root2, 0), YGNodeGetChild(root3, 0));
ASSERT_EQ(YGNodeGetChild(root2, 0), YGNodeGetChild(root3, 0));
const YGNodeRef root4 = YGNodeClone(root3);
ASSERT_EQ(root3_child1, YGNodeGetChild(root4, 1));
@@ -137,7 +138,12 @@ TEST(YogaTest, mutating_children_of_a_clone_clones) {
YGNodeRemoveChild(root4, root3_child1);
ASSERT_EQ(2, YGNodeGetChildCount(root3));
ASSERT_EQ(1, YGNodeGetChildCount(root4));
ASSERT_EQ(YGNodeGetChild(root3, 0), YGNodeGetChild(root4, 0));
YGNodeCalculateLayout(root4, YGUndefined, YGUndefined, YGDirectionLTR);
ASSERT_NE(YGNodeGetChild(root3, 0), YGNodeGetChild(root4, 0));
YGNodeCalculateLayout(root3, YGUndefined, YGUndefined, YGDirectionLTR);
ASSERT_NE(YGNodeGetChild(root2, 0), YGNodeGetChild(root3, 0));
YGNodeFreeRecursive(root4);
YGNodeFreeRecursive(root3);
@@ -245,3 +251,34 @@ TEST(YogaTest, cloning_and_freeing) {
ASSERT_EQ(initialInstanceCount, YGNodeGetInstanceCount());
}
TEST(YogaTest, mixed_shared_and_owned_children) {
// Don't try this at home!
YGNodeRef root0 = YGNodeNew();
YGNodeRef root1 = YGNodeNew();
YGNodeRef root0_child0 = YGNodeNew();
YGNodeRef root0_child0_0 = YGNodeNew();
YGNodeInsertChild(root0, root0_child0, 0);
YGNodeInsertChild(root0_child0, root0_child0_0, 0);
YGNodeRef root1_child0 = YGNodeNew();
YGNodeRef root1_child2 = YGNodeNew();
YGNodeInsertChild(root1, root1_child0, 0);
YGNodeInsertChild(root1, root1_child2, 1);
auto children = root1->getChildren();
children.insert(children.begin() + 1, root0_child0);
root1->setChildren(children);
auto secondChild = YGNodeGetChild(root1, 1);
ASSERT_EQ(secondChild, YGNodeGetChild(root0, 0));
ASSERT_EQ(YGNodeGetChild(secondChild, 0), YGNodeGetChild(root0_child0, 0));
YGNodeCalculateLayout(root1, YGUndefined, YGUndefined, YGDirectionLTR);
secondChild = YGNodeGetChild(root1, 1);
ASSERT_NE(secondChild, YGNodeGetChild(root0, 0));
ASSERT_EQ(YGNodeGetOwner(secondChild), root1);
ASSERT_NE(YGNodeGetChild(secondChild, 0), YGNodeGetChild(root0_child0, 0));
ASSERT_EQ(YGNodeGetOwner(YGNodeGetChild(secondChild, 0)), secondChild);
}

View File

@@ -19,7 +19,7 @@ dependencies {
### Javascript
The JavaScript bindings for Yoga can be used from node.js and within the browser.
When using Yoga from node.js the native library is used, in browesers a pure JS
When using Yoga from node.js the native library is used, in browsers a pure JS
version is used (cross-compiled using [emscripten](http://kripken.github.io/emscripten-site/)).
```

View File

@@ -6,4 +6,38 @@
*/
#include "YGConfig.h"
YGConfig::YGConfig(YGLogger logger) : logger(logger) {}
YGConfig::YGConfig(YGLogger logger) : cloneNodeCallback_{nullptr} {
logger_.noContext = logger;
loggerUsesContext_ = false;
}
void YGConfig::log(
YGConfig* config,
YGNode* node,
YGLogLevel logLevel,
void* logContext,
const char* format,
va_list args) {
if (loggerUsesContext_) {
logger_.withContext(config, node, logLevel, logContext, format, args);
} else {
logger_.noContext(config, node, logLevel, format, args);
}
}
YGNodeRef YGConfig::cloneNode(
YGNodeRef node,
YGNodeRef owner,
int childIndex,
void* cloneContext) {
YGNodeRef clone = nullptr;
if (cloneNodeCallback_.noContext != nullptr) {
clone = cloneNodeUsesContext_
? cloneNodeCallback_.withContext(node, owner, childIndex, cloneContext)
: cloneNodeCallback_.noContext(node, owner, childIndex);
}
if (clone == nullptr) {
clone = YGNodeClone(node);
}
return clone;
}

View File

@@ -10,17 +10,70 @@
#include "Yoga.h"
struct YGConfig {
std::array<bool, facebook::yoga::enums::count<YGExperimentalFeature>()>
experimentalFeatures = {};
using LogWithContextFn = int (*)(
YGConfigRef config,
YGNodeRef node,
YGLogLevel level,
void* context,
const char* format,
va_list args);
using CloneWithContextFn = YGNodeRef (*)(
YGNodeRef node,
YGNodeRef owner,
int childIndex,
void* cloneContext);
private:
union {
CloneWithContextFn withContext;
YGCloneNodeFunc noContext;
} cloneNodeCallback_;
union {
LogWithContextFn withContext;
YGLogger noContext;
} logger_;
bool cloneNodeUsesContext_;
bool loggerUsesContext_;
public:
bool useWebDefaults = false;
bool useLegacyStretchBehaviour = false;
bool shouldDiffLayoutWithoutLegacyStretchBehaviour = false;
bool printTree = false;
float pointScaleFactor = 1.0f;
YGLogger logger;
YGCloneNodeFunc cloneNodeCallback = nullptr;
std::array<bool, facebook::yoga::enums::count<YGExperimentalFeature>()>
experimentalFeatures = {};
void* context = nullptr;
YGMarkerCallbacks markerCallbacks = {nullptr, nullptr};
YGConfig(YGLogger logger);
void log(YGConfig*, YGNode*, YGLogLevel, void*, const char*, va_list);
void setLogger(YGLogger logger) {
logger_.noContext = logger;
loggerUsesContext_ = false;
}
void setLogger(LogWithContextFn logger) {
logger_.withContext = logger;
loggerUsesContext_ = true;
}
void setLogger(std::nullptr_t) {
setLogger(YGLogger{nullptr});
}
YGNodeRef cloneNode(
YGNodeRef node,
YGNodeRef owner,
int childIndex,
void* cloneContext);
void setCloneNodeCallback(YGCloneNodeFunc cloneNode) {
cloneNodeCallback_.noContext = cloneNode;
cloneNodeUsesContext_ = false;
}
void setCloneNodeCallback(CloneWithContextFn cloneNode) {
cloneNodeCallback_.withContext = cloneNode;
cloneNodeUsesContext_ = true;
}
void setCloneNodeCallback(std::nullptr_t) {
setCloneNodeCallback(YGCloneNodeFunc{nullptr});
}
};

View File

@@ -5,6 +5,7 @@
* file in the root directory of this source tree.
*/
#include "YGNode.h"
#include <algorithm>
#include <iostream>
#include "CompactValue.h"
#include "Utils.h"
@@ -12,6 +13,41 @@
using namespace facebook;
using facebook::yoga::detail::CompactValue;
YGNode::YGNode(YGNode&& node) {
context_ = node.context_;
hasNewLayout_ = node.hasNewLayout_;
isReferenceBaseline_ = node.isReferenceBaseline_;
isDirty_ = node.isDirty_;
nodeType_ = node.nodeType_;
measureUsesContext_ = node.measureUsesContext_;
baselineUsesContext_ = node.baselineUsesContext_;
printUsesContext_ = node.printUsesContext_;
measure_ = node.measure_;
baseline_ = node.baseline_;
print_ = node.print_;
dirtied_ = node.dirtied_;
style_ = node.style_;
layout_ = node.layout_;
lineIndex_ = node.lineIndex_;
owner_ = node.owner_;
children_ = std::move(node.children_);
config_ = node.config_;
resolvedDimensions_ = node.resolvedDimensions_;
for (auto c : children_) {
c->setOwner(c);
}
}
void YGNode::print(void* printContext) {
if (print_.noContext != nullptr) {
if (printUsesContext_) {
print_.withContext(this, printContext);
} else {
print_.noContext(this);
}
}
}
YGFloatOptional YGNode::getLeadingPosition(
const YGFlexDirection axis,
const float axisSize) const {
@@ -101,11 +137,29 @@ YGFloatOptional YGNode::getMarginForAxis(
return getLeadingMargin(axis, widthSize) + getTrailingMargin(axis, widthSize);
}
YGSize YGNode::measure(
float width,
YGMeasureMode widthMode,
float height,
YGMeasureMode heightMode,
void* layoutContext) {
return measureUsesContext_
? measure_.withContext(
this, width, widthMode, height, heightMode, layoutContext)
: measure_.noContext(this, width, widthMode, height, heightMode);
}
float YGNode::baseline(float width, float height, void* layoutContext) {
return baselineUsesContext_
? baseline_.withContext(this, width, height, layoutContext)
: baseline_.noContext(this, width, height);
}
// Setters
void YGNode::setMeasureFunc(YGMeasureFunc measureFunc) {
if (measureFunc == nullptr) {
measure_ = nullptr;
void YGNode::setMeasureFunc(decltype(YGNode::measure_) measureFunc) {
if (measureFunc.noContext == nullptr) {
// TODO: t18095186 Move nodeType to opt-in function and mark appropriate
// places in Litho
nodeType_ = YGNodeTypeDefault;
@@ -115,7 +169,6 @@ void YGNode::setMeasureFunc(YGMeasureFunc measureFunc) {
children_.size() == 0,
"Cannot set measure function: Nodes with measure functions cannot have "
"children.");
measure_ = measureFunc;
// TODO: t18095186 Move nodeType to opt-in function and mark appropriate
// places in Litho
setNodeType(YGNodeTypeText);
@@ -124,6 +177,20 @@ void YGNode::setMeasureFunc(YGMeasureFunc measureFunc) {
measure_ = measureFunc;
}
void YGNode::setMeasureFunc(YGMeasureFunc measureFunc) {
measureUsesContext_ = false;
decltype(YGNode::measure_) m;
m.noContext = measureFunc;
setMeasureFunc(m);
}
void YGNode::setMeasureFunc(MeasureWithContextFn measureFunc) {
measureUsesContext_ = true;
decltype(YGNode::measure_) m;
m.withContext = measureFunc;
setMeasureFunc(m);
}
void YGNode::replaceChild(YGNodeRef child, uint32_t index) {
children_[index] = child;
}
@@ -257,34 +324,6 @@ void YGNode::setPosition(
trailing[crossAxis]);
}
YGNode& YGNode::operator=(const YGNode& node) {
if (&node == this) {
return *this;
}
for (auto child : children_) {
delete child;
}
context_ = node.getContext();
print_ = node.getPrintFunc();
hasNewLayout_ = node.getHasNewLayout();
nodeType_ = node.getNodeType();
measure_ = node.getMeasure();
baseline_ = node.getBaseline();
dirtied_ = node.getDirtied();
style_ = node.style_;
layout_ = node.layout_;
lineIndex_ = node.getLineIndex();
owner_ = node.getOwner();
children_ = node.getChildren();
config_ = node.getConfig();
isDirty_ = node.isDirty();
resolvedDimensions_ = node.getResolvedDimensions();
return *this;
}
YGValue YGNode::marginLeadingValue(const YGFlexDirection axis) const {
if (YGFlexDirectionIsRow(axis) && !style_.margin[YGEdgeStart].isUndefined()) {
return style_.margin[YGEdgeStart];
@@ -341,38 +380,8 @@ void YGNode::clearChildren() {
// Other Methods
void YGNode::cloneChildrenIfNeeded() {
// YGNodeRemoveChild in yoga.cpp has a forked variant of this algorithm
// optimized for deletions.
const uint32_t childCount = static_cast<uint32_t>(children_.size());
if (childCount == 0) {
// This is an empty set. Nothing to clone.
return;
}
const YGNodeRef firstChild = children_.front();
if (firstChild->getOwner() == this) {
// If the first child has this node as its owner, we assume that it is
// already unique. We can do this because if we have it has a child, that
// means that its owner was at some point cloned which made that subtree
// immutable. We also assume that all its sibling are cloned as well.
return;
}
const YGCloneNodeFunc cloneNodeCallback = config_->cloneNodeCallback;
for (uint32_t i = 0; i < childCount; ++i) {
const YGNodeRef oldChild = children_[i];
YGNodeRef newChild = nullptr;
if (cloneNodeCallback) {
newChild = cloneNodeCallback(oldChild, this, i);
}
if (newChild == nullptr) {
newChild = YGNodeClone(oldChild);
}
replaceChild(newChild, i);
newChild->setOwner(this);
}
void YGNode::cloneChildrenIfNeeded(void* cloneContext) {
iterChildrenAfterCloningIfNeeded([](YGNodeRef, void*) {}, cloneContext);
}
void YGNode::markDirtyAndPropogate() {
@@ -557,3 +566,22 @@ bool YGNode::isLayoutTreeEqualToNode(const YGNode& node) const {
}
return isLayoutTreeEqual;
}
void YGNode::reset() {
YGAssertWithNode(
this,
children_.size() == 0,
"Cannot reset a node which still has children attached");
YGAssertWithNode(
this, owner_ == nullptr, "Cannot reset a node still attached to a owner");
clearChildren();
auto config = getConfig();
*this = YGNode{};
if (config->useWebDefaults) {
setStyleFlexDirection(YGFlexDirectionRow);
setStyleAlignContent(YGAlignStretch);
}
setConfig(config);
}

View File

@@ -12,15 +12,32 @@
#include "Yoga-internal.h"
struct YGNode {
using MeasureWithContextFn =
YGSize (*)(YGNode*, float, YGMeasureMode, float, YGMeasureMode, void*);
using BaselineWithContextFn = float (*)(YGNode*, float, float, void*);
using PrintWithContextFn = void (*)(YGNode*, void*);
private:
void* context_ = nullptr;
YGPrintFunc print_ = nullptr;
bool hasNewLayout_ : 1;
bool isReferenceBaseline_ : 1;
bool isDirty_ : 1;
YGNodeType nodeType_ : 1;
YGMeasureFunc measure_ = nullptr;
YGBaselineFunc baseline_ = nullptr;
bool measureUsesContext_ : 1;
bool baselineUsesContext_ : 1;
bool printUsesContext_ : 1;
union {
YGMeasureFunc noContext;
MeasureWithContextFn withContext;
} measure_ = {nullptr};
union {
YGBaselineFunc noContext;
BaselineWithContextFn withContext;
} baseline_ = {nullptr};
union {
YGPrintFunc noContext;
PrintWithContextFn withContext;
} print_ = {nullptr};
YGDirtiedFunc dirtied_ = nullptr;
YGStyle style_ = {};
YGLayout layout_ = {};
@@ -35,25 +52,44 @@ private:
const YGFlexDirection axis,
const float axisSize) const;
void setMeasureFunc(decltype(measure_));
void setBaselineFunc(decltype(baseline_));
// DANGER DANGER DANGER!
// If the the node assigned to has children, we'd either have to deallocate
// them (potentially incorrect) or ignore them (danger of leaks). Only ever
// use this after checking that there are no children.
// DO NOT CHANGE THE VISIBILITY OF THIS METHOD!
YGNode& operator=(YGNode&&) = default;
public:
YGNode()
: hasNewLayout_(true),
isReferenceBaseline_(false),
isDirty_(false),
nodeType_(YGNodeTypeDefault) {}
: hasNewLayout_{true},
isReferenceBaseline_{false},
isDirty_{false},
nodeType_{YGNodeTypeDefault},
measureUsesContext_{false},
baselineUsesContext_{false},
printUsesContext_{false} {}
~YGNode() = default; // cleanup of owner/children relationships in YGNodeFree
explicit YGNode(const YGConfigRef newConfig) : config_(newConfig){};
YGNode(YGNode&&);
// Does not expose true value semantics, as children are not cloned eagerly.
// Should we remove this?
YGNode(const YGNode& node) = default;
YGNode& operator=(const YGNode& node);
// assignment means potential leaks of existing children, or alternatively
// freeing unowned memory, double free, or freeing stack memory.
YGNode& operator=(const YGNode&) = delete;
// Getters
void* getContext() const {
return context_;
}
YGPrintFunc getPrintFunc() const {
return print_;
}
void print(void*);
bool getHasNewLayout() const {
return hasNewLayout_;
@@ -63,14 +99,18 @@ public:
return nodeType_;
}
YGMeasureFunc getMeasure() const {
return measure_;
bool hasMeasureFunc() const noexcept {
return measure_.noContext != nullptr;
}
YGBaselineFunc getBaseline() const {
return baseline_;
YGSize measure(float, YGMeasureMode, float, YGMeasureMode, void*);
bool hasBaselineFunc() const noexcept {
return baseline_.noContext != nullptr;
}
float baseline(float width, float height, void* layoutContext);
YGDirtiedFunc getDirtied() const {
return dirtied_;
}
@@ -118,6 +158,22 @@ public:
return children_;
}
// Applies a callback to all children, after cloning them if they are not
// owned.
template <typename T>
void iterChildrenAfterCloningIfNeeded(T callback, void* cloneContext) {
int i = 0;
for (YGNodeRef& child : children_) {
if (child->getOwner() != this) {
child = config_->cloneNode(child, this, i, cloneContext);
child->setOwner(this);
}
i += 1;
callback(child, cloneContext);
}
}
YGNodeRef getChild(uint32_t index) const {
return children_.at(index);
}
@@ -177,7 +233,15 @@ public:
}
void setPrintFunc(YGPrintFunc printFunc) {
print_ = printFunc;
print_.noContext = printFunc;
printUsesContext_ = false;
}
void setPrintFunc(PrintWithContextFn printFunc) {
print_.withContext = printFunc;
printUsesContext_ = true;
}
void setPrintFunc(std::nullptr_t) {
setPrintFunc(YGPrintFunc{nullptr});
}
void setHasNewLayout(bool hasNewLayout) {
@@ -197,9 +261,21 @@ public:
}
void setMeasureFunc(YGMeasureFunc measureFunc);
void setMeasureFunc(MeasureWithContextFn);
void setMeasureFunc(std::nullptr_t) {
return setMeasureFunc(YGMeasureFunc{nullptr});
}
void setBaseLineFunc(YGBaselineFunc baseLineFunc) {
baseline_ = baseLineFunc;
void setBaselineFunc(YGBaselineFunc baseLineFunc) {
baselineUsesContext_ = false;
baseline_.noContext = baseLineFunc;
}
void setBaselineFunc(BaselineWithContextFn baseLineFunc) {
baselineUsesContext_ = true;
baseline_.withContext = baseLineFunc;
}
void setBaselineFunc(std::nullptr_t) {
return setBaselineFunc(YGBaselineFunc{nullptr});
}
void setDirtiedFunc(YGDirtiedFunc dirtiedFunc) {
@@ -274,11 +350,12 @@ public:
bool removeChild(YGNodeRef child);
void removeChild(uint32_t index);
void cloneChildrenIfNeeded();
void cloneChildrenIfNeeded(void*);
void markDirtyAndPropogate();
float resolveFlexGrow();
float resolveFlexShrink();
bool isNodeFlexible();
bool didUseLegacyFlag();
bool isLayoutTreeEqualToNode(const YGNode& node) const;
void reset();
};

View File

@@ -114,9 +114,6 @@ void YGNodeToString(
uint32_t level) {
indent(str, level);
appendFormatedString(str, "<div ");
if (node->getPrintFunc() != nullptr) {
node->getPrintFunc()(node);
}
if (options & YGPrintOptionsLayout) {
appendFormatedString(str, "layout=\"");
@@ -169,7 +166,7 @@ void YGNodeToString(
if (node->getStyle().flexWrap != YGNode().getStyle().flexWrap) {
appendFormatedString(
str, "flexWrap: %s; ", YGWrapToString(node->getStyle().flexWrap));
str, "flex-wrap: %s; ", YGWrapToString(node->getStyle().flexWrap));
}
if (node->getStyle().overflow != YGNode().getStyle().overflow) {
@@ -214,7 +211,7 @@ void YGNodeToString(
str, "bottom", node->getStyle().position, YGEdgeBottom);
appendFormatedString(str, "\" ");
if (node->getMeasure() != nullptr) {
if (node->hasMeasureFunc()) {
appendFormatedString(str, "has-custom-measure=\"true\"");
}
}

View File

@@ -22,6 +22,13 @@ WIN_EXPORT float YGRoundValueToPixelGrid(
const bool forceCeil,
const bool forceFloor);
void YGNodeCalculateLayoutWithContext(
YGNodeRef node,
float availableWidth,
float availableHeight,
YGDirection ownerDirection,
void* layoutContext);
YG_EXTERN_C_END
namespace facebook {

View File

@@ -5,6 +5,7 @@
* file in the root directory of this source tree.
*/
#include "Yoga.h"
#include "log.h"
#include <float.h>
#include <string.h>
#include <algorithm>
@@ -25,6 +26,16 @@ __forceinline const float fmaxf(const float a, const float b) {
#endif
using namespace facebook::yoga;
using detail::Log;
namespace {
size_t usedMeasureCacheEntries = YG_MAX_CACHED_RESULT_COUNT;
}
void YGSetUsedCachedEntries(size_t n) {
usedMeasureCacheEntries =
n == 0 || n > YG_MAX_CACHED_RESULT_COUNT ? YG_MAX_CACHED_RESULT_COUNT : n;
}
#ifdef ANDROID
static int YGAndroidLog(
@@ -143,20 +154,20 @@ void YGNodeSetContext(YGNodeRef node, void* context) {
return node->setContext(context);
}
YGMeasureFunc YGNodeGetMeasureFunc(YGNodeRef node) {
return node->getMeasure();
bool YGNodeHasMeasureFunc(YGNodeRef node) {
return node->hasMeasureFunc();
}
void YGNodeSetMeasureFunc(YGNodeRef node, YGMeasureFunc measureFunc) {
node->setMeasureFunc(measureFunc);
}
YGBaselineFunc YGNodeGetBaselineFunc(YGNodeRef node) {
return node->getBaseline();
bool YGNodeHasBaselineFunc(YGNodeRef node) {
return node->hasBaselineFunc();
}
void YGNodeSetBaselineFunc(YGNodeRef node, YGBaselineFunc baselineFunc) {
node->setBaseLineFunc(baselineFunc);
node->setBaselineFunc(baselineFunc);
}
YGDirtiedFunc YGNodeGetDirtiedFunc(YGNodeRef node) {
@@ -167,10 +178,6 @@ void YGNodeSetDirtiedFunc(YGNodeRef node, YGDirtiedFunc dirtiedFunc) {
node->setDirtiedFunc(dirtiedFunc);
}
YGPrintFunc YGNodeGetPrintFunc(YGNodeRef node) {
return node->getPrintFunc();
}
void YGNodeSetPrintFunc(YGNodeRef node, YGPrintFunc printFunc) {
node->setPrintFunc(printFunc);
}
@@ -304,14 +311,16 @@ static void YGConfigFreeRecursive(const YGNodeRef root) {
void YGNodeFreeRecursiveWithCleanupFunc(
const YGNodeRef root,
YGNodeCleanupFunc cleanup) {
while (YGNodeGetChildCount(root) > 0) {
const YGNodeRef child = YGNodeGetChild(root, 0);
uint32_t skipped = 0;
while (YGNodeGetChildCount(root) > skipped) {
const YGNodeRef child = YGNodeGetChild(root, skipped);
if (child->getOwner() != root) {
// Don't free shared nodes that we don't own.
break;
skipped += 1;
} else {
YGNodeRemoveChild(root, child);
YGNodeFreeRecursive(child);
}
YGNodeRemoveChild(root, child);
YGNodeFreeRecursive(child);
}
if (cleanup != nullptr) {
cleanup(root);
@@ -323,25 +332,8 @@ void YGNodeFreeRecursive(const YGNodeRef root) {
return YGNodeFreeRecursiveWithCleanupFunc(root, nullptr);
}
void YGNodeReset(const YGNodeRef node) {
YGAssertWithNode(
node,
YGNodeGetChildCount(node) == 0,
"Cannot reset a node which still has children attached");
YGAssertWithNode(
node,
node->getOwner() == nullptr,
"Cannot reset a node still attached to a owner");
node->clearChildren();
const YGConfigRef config = node->getConfig();
*node = YGNode();
if (config->useWebDefaults) {
node->setStyleFlexDirection(YGFlexDirectionRow);
node->setStyleAlignContent(YGAlignStretch);
}
node->setConfig(config);
void YGNodeReset(YGNodeRef node) {
node->reset();
}
int32_t YGNodeGetInstanceCount(void) {
@@ -383,77 +375,40 @@ bool YGNodeIsReferenceBaseline(YGNodeRef node) {
}
void YGNodeInsertChild(
const YGNodeRef node,
const YGNodeRef owner,
const YGNodeRef child,
const uint32_t index) {
YGAssertWithNode(
node,
owner,
child->getOwner() == nullptr,
"Child already has a owner, it must be removed first.");
YGAssertWithNode(
node,
node->getMeasure() == nullptr,
owner,
!owner->hasMeasureFunc(),
"Cannot add child: Nodes with measure functions cannot have children.");
node->cloneChildrenIfNeeded();
node->insertChild(child, index);
YGNodeRef owner = child->getOwner() ? nullptr : node;
owner->insertChild(child, index);
child->setOwner(owner);
node->markDirtyAndPropogate();
owner->markDirtyAndPropogate();
}
void YGNodeRemoveChild(const YGNodeRef owner, const YGNodeRef excludedChild) {
// This algorithm is a forked variant from cloneChildrenIfNeeded in YGNode
// that excludes a child.
const uint32_t childCount = YGNodeGetChildCount(owner);
if (childCount == 0) {
if (YGNodeGetChildCount(owner) == 0) {
// This is an empty set. Nothing to remove.
return;
}
const YGNodeRef firstChild = YGNodeGetChild(owner, 0);
if (firstChild->getOwner() == owner) {
// If the first child has this node as its owner, we assume that it is
// already unique. We can now try to delete a child in this list.
if (owner->removeChild(excludedChild)) {
excludedChild->setLayout(
YGNode().getLayout()); // layout is no longer valid
excludedChild->setOwner(nullptr);
owner->markDirtyAndPropogate();
}
return;
}
// Otherwise we have to clone the node list except for the child we're trying
// to delete. We don't want to simply clone all children, because then the
// host will need to free the clone of the child that was just deleted.
const YGCloneNodeFunc cloneNodeCallback =
owner->getConfig()->cloneNodeCallback;
uint32_t nextInsertIndex = 0;
for (uint32_t i = 0; i < childCount; i++) {
const YGNodeRef oldChild = owner->getChild(i);
if (excludedChild == oldChild) {
// Ignore the deleted child. Don't reset its layout or owner since it is
// still valid in the other owner. However, since this owner has now
// changed, we need to mark it as dirty.
owner->markDirtyAndPropogate();
continue;
}
YGNodeRef newChild = nullptr;
if (cloneNodeCallback) {
newChild = cloneNodeCallback(oldChild, owner, nextInsertIndex);
}
if (newChild == nullptr) {
newChild = YGNodeClone(oldChild);
}
owner->replaceChild(newChild, nextInsertIndex);
newChild->setOwner(owner);
nextInsertIndex++;
}
while (nextInsertIndex < childCount) {
owner->removeChild(nextInsertIndex);
nextInsertIndex++;
// Children may be shared between parents, which is indicated by not having an
// owner. We only want to reset the child completely if it is owned
// exclusively by one node.
auto childOwner = excludedChild->getOwner();
if (owner->removeChild(excludedChild)) {
if (owner == childOwner) {
excludedChild->setLayout({}); // layout is no longer valid
excludedChild->setOwner(nullptr);
}
owner->markDirtyAndPropogate();
}
}
@@ -553,7 +508,7 @@ YGNodeRef YGNodeGetParent(const YGNodeRef node) {
void YGNodeMarkDirty(const YGNodeRef node) {
YGAssertWithNode(
node,
node->getMeasure() != nullptr,
node->hasMeasureFunc(),
"Only leaf nodes with custom measure functions"
"should manually mark themselves as dirty");
@@ -1021,14 +976,15 @@ bool YGLayoutNodeInternal(
const bool performLayout,
const char* reason,
const YGConfigRef config,
YGMarkerLayoutData& layoutMarkerData);
YGMarkerLayoutData& layoutMarkerData,
void* const layoutContext);
static void YGNodePrintInternal(
const YGNodeRef node,
const YGPrintOptions options) {
std::string str;
facebook::yoga::YGNodeToString(str, node, options, 0);
YGLog(node, YGLogLevelDebug, str.c_str());
Log::log(node, YGLogLevelDebug, nullptr, str.c_str());
}
void YGNodePrint(const YGNodeRef node, const YGPrintOptions options) {
@@ -1072,14 +1028,14 @@ static inline YGAlign YGNodeAlignItem(
return align;
}
static float YGBaseline(const YGNodeRef node) {
if (node->getBaseline() != nullptr) {
static float YGBaseline(const YGNodeRef node, void* layoutContext) {
if (node->hasBaselineFunc()) {
const float baseline = marker::MarkerSection<YGMarkerBaselineFn>::wrap(
node,
node->getBaseline(),
node,
&YGNode::baseline,
node->getLayout().measuredDimensions[YGDimensionWidth],
node->getLayout().measuredDimensions[YGDimensionHeight]);
node->getLayout().measuredDimensions[YGDimensionHeight],
layoutContext);
YGAssertWithNode(
node,
!YGFloatIsUndefined(baseline),
@@ -1112,7 +1068,7 @@ static float YGBaseline(const YGNodeRef node) {
return node->getLayout().measuredDimensions[YGDimensionHeight];
}
const float baseline = YGBaseline(baselineChild);
const float baseline = YGBaseline(baselineChild, layoutContext);
return baseline + baselineChild->getLayout().position[YGEdgeTop];
}
@@ -1263,7 +1219,8 @@ static void YGNodeComputeFlexBasisForChild(
const YGMeasureMode heightMode,
const YGDirection direction,
const YGConfigRef config,
YGMarkerLayoutData& layoutMarkerData) {
YGMarkerLayoutData& layoutMarkerData,
void* const layoutContext) {
const YGFlexDirection mainAxis =
YGResolveFlexDirection(node->getStyle().flexDirection, direction);
const bool isMainAxisRow = YGFlexDirectionIsRow(mainAxis);
@@ -1437,7 +1394,8 @@ static void YGNodeComputeFlexBasisForChild(
false,
"measure",
config,
layoutMarkerData);
layoutMarkerData,
layoutContext);
child->setLayoutComputedFlexBasis(YGFloatOptional(YGFloatMax(
child->getLayout().measuredDimensions[dim[mainAxis]],
@@ -1454,7 +1412,8 @@ static void YGNodeAbsoluteLayoutChild(
const float height,
const YGDirection direction,
const YGConfigRef config,
YGMarkerLayoutData& layoutMarkerData) {
YGMarkerLayoutData& layoutMarkerData,
void* const layoutContext) {
const YGFlexDirection mainAxis =
YGResolveFlexDirection(node->getStyle().flexDirection, direction);
const YGFlexDirection crossAxis = YGFlexDirectionCross(mainAxis, direction);
@@ -1559,7 +1518,8 @@ static void YGNodeAbsoluteLayoutChild(
false,
"abs-measure",
config,
layoutMarkerData);
layoutMarkerData,
layoutContext);
childWidth = child->getLayout().measuredDimensions[YGDimensionWidth] +
child->getMarginForAxis(YGFlexDirectionRow, width).unwrap();
childHeight = child->getLayout().measuredDimensions[YGDimensionHeight] +
@@ -1578,7 +1538,8 @@ static void YGNodeAbsoluteLayoutChild(
true,
"abs-layout",
config,
layoutMarkerData);
layoutMarkerData,
layoutContext);
if (child->isTrailingPosDefined(mainAxis) &&
!child->isLeadingPositionDefined(mainAxis)) {
@@ -1645,10 +1606,11 @@ static void YGNodeWithMeasureFuncSetMeasuredDimensions(
const YGMeasureMode widthMeasureMode,
const YGMeasureMode heightMeasureMode,
const float ownerWidth,
const float ownerHeight) {
const float ownerHeight,
void* const layoutContext) {
YGAssertWithNode(
node,
node->getMeasure() != nullptr,
node->hasMeasureFunc(),
"Expected node to have custom measure function");
const float paddingAndBorderAxisRow =
@@ -1692,12 +1654,12 @@ static void YGNodeWithMeasureFuncSetMeasuredDimensions(
// Measure the text under the current constraints.
const YGSize measuredSize = marker::MarkerSection<YGMarkerMeasure>::wrap(
node,
node->getMeasure(),
node,
&YGNode::measure,
innerWidth,
widthMeasureMode,
innerHeight,
heightMeasureMode);
heightMeasureMode,
layoutContext);
node->setLayoutMeasuredDimension(
YGNodeBoundAxis(
@@ -1819,17 +1781,16 @@ static bool YGNodeFixedSizeSetMeasuredDimensions(
return false;
}
static void YGZeroOutLayoutRecursivly(const YGNodeRef node) {
static void YGZeroOutLayoutRecursivly(
const YGNodeRef node,
void* layoutContext) {
node->getLayout() = {};
node->setLayoutDimension(0, 0);
node->setLayoutDimension(0, 1);
node->setHasNewLayout(true);
node->cloneChildrenIfNeeded();
const uint32_t childCount = YGNodeGetChildCount(node);
for (uint32_t i = 0; i < childCount; i++) {
const YGNodeRef child = node->getChild(i);
YGZeroOutLayoutRecursivly(child);
}
node->iterChildrenAfterCloningIfNeeded(
YGZeroOutLayoutRecursivly, layoutContext);
}
static float YGNodeCalculateAvailableInnerDim(
@@ -1881,7 +1842,8 @@ static float YGNodeComputeFlexBasisForChildren(
YGFlexDirection mainAxis,
const YGConfigRef config,
bool performLayout,
YGMarkerLayoutData& layoutMarkerData) {
YGMarkerLayoutData& layoutMarkerData,
void* const layoutContext) {
float totalOuterFlexBasis = 0.0f;
YGNodeRef singleFlexChild = nullptr;
YGVector children = node->getChildren();
@@ -1910,7 +1872,7 @@ static float YGNodeComputeFlexBasisForChildren(
for (auto child : children) {
child->resolveDimension();
if (child->getStyle().display == YGDisplayNone) {
YGZeroOutLayoutRecursivly(child);
YGZeroOutLayoutRecursivly(child, layoutContext);
child->setHasNewLayout(true);
child->setDirty(false);
continue;
@@ -1946,7 +1908,8 @@ static float YGNodeComputeFlexBasisForChildren(
heightMeasureMode,
direction,
config,
layoutMarkerData);
layoutMarkerData,
layoutContext);
}
totalOuterFlexBasis +=
@@ -2059,7 +2022,8 @@ static float YGDistributeFreeSpaceSecondPass(
const YGMeasureMode measureModeCrossDim,
const bool performLayout,
const YGConfigRef config,
YGMarkerLayoutData& layoutMarkerData) {
YGMarkerLayoutData& layoutMarkerData,
void* const layoutContext) {
float childFlexBasis = 0;
float flexShrinkScaledFactor = 0;
float flexGrowFactor = 0;
@@ -2224,7 +2188,8 @@ static float YGDistributeFreeSpaceSecondPass(
performLayout && !requiresStretchLayout,
"flex",
config,
layoutMarkerData);
layoutMarkerData,
layoutContext);
node->setLayoutHadOverflow(
node->getLayout().hadOverflow |
currentRelativeChild->getLayout().hadOverflow);
@@ -2354,7 +2319,8 @@ static void YGResolveFlexibleLength(
const YGMeasureMode measureModeCrossDim,
const bool performLayout,
const YGConfigRef config,
YGMarkerLayoutData& layoutMarkerData) {
YGMarkerLayoutData& layoutMarkerData,
void* const layoutContext) {
const float originalFreeSpace = collectedFlexItemsValues.remainingFreeSpace;
// First pass: detect the flex items whose min/max constraints trigger
YGDistributeFreeSpaceFirstPass(
@@ -2379,7 +2345,8 @@ static void YGResolveFlexibleLength(
measureModeCrossDim,
performLayout,
config,
layoutMarkerData);
layoutMarkerData,
layoutContext);
collectedFlexItemsValues.remainingFreeSpace =
originalFreeSpace - distributedFreeSpace;
@@ -2398,7 +2365,8 @@ static void YGJustifyMainAxis(
const float availableInnerMainDim,
const float availableInnerCrossDim,
const float availableInnerWidth,
const bool performLayout) {
const bool performLayout,
void* const layoutContext) {
const YGStyle& style = node->getStyle();
const float leadingPaddingAndBorderMain =
node->getLeadingPaddingAndBorder(mainAxis, ownerWidth).unwrap();
@@ -2558,7 +2526,7 @@ static void YGJustifyMainAxis(
if (isNodeBaselineLayout) {
// If the child is baseline aligned then the cross dimension is
// calculated by adding maxAscent and maxDescent from the baseline.
const float ascent = YGBaseline(child) +
const float ascent = YGBaseline(child, layoutContext) +
child
->getLeadingMargin(
YGFlexDirectionColumn, availableInnerWidth)
@@ -2676,7 +2644,8 @@ static void YGNodelayoutImpl(
const float ownerHeight,
const bool performLayout,
const YGConfigRef config,
YGMarkerLayoutData& layoutMarkerData) {
YGMarkerLayoutData& layoutMarkerData,
void* const layoutContext) {
YGAssertWithNode(
node,
YGFloatIsUndefined(availableWidth)
@@ -2735,7 +2704,7 @@ static void YGNodelayoutImpl(
node->getTrailingPadding(flexColumnDirection, ownerWidth).unwrap(),
YGEdgeBottom);
if (node->getMeasure() != nullptr) {
if (node->hasMeasureFunc()) {
YGNodeWithMeasureFuncSetMeasuredDimensions(
node,
availableWidth,
@@ -2743,7 +2712,8 @@ static void YGNodelayoutImpl(
widthMeasureMode,
heightMeasureMode,
ownerWidth,
ownerHeight);
ownerHeight,
layoutContext);
return;
}
@@ -2776,7 +2746,7 @@ static void YGNodelayoutImpl(
// At this point we know we're going to perform work. Ensure that each child
// has a mutable copy.
node->cloneChildrenIfNeeded();
node->cloneChildrenIfNeeded(layoutContext);
// Reset layout flags, as they could have changed.
node->setLayoutHadOverflow(false);
@@ -2860,7 +2830,8 @@ static void YGNodelayoutImpl(
mainAxis,
config,
performLayout,
layoutMarkerData);
layoutMarkerData,
layoutContext);
const bool flexBasisOverflows = measureModeMainDim == YGMeasureModeUndefined
? false
@@ -2967,7 +2938,8 @@ static void YGNodelayoutImpl(
measureModeCrossDim,
performLayout,
config,
layoutMarkerData);
layoutMarkerData,
layoutContext);
}
node->setLayoutHadOverflow(
@@ -2994,7 +2966,8 @@ static void YGNodelayoutImpl(
availableInnerMainDim,
availableInnerCrossDim,
availableInnerWidth,
performLayout);
performLayout,
layoutContext);
float containerCrossAxis = availableInnerCrossDim;
if (measureModeCrossDim == YGMeasureModeUndefined ||
@@ -3133,7 +3106,8 @@ static void YGNodelayoutImpl(
true,
"stretch",
config,
layoutMarkerData);
layoutMarkerData,
layoutContext);
}
} else {
const float remainingCrossDim = containerCrossAxis -
@@ -3237,7 +3211,7 @@ static void YGNodelayoutImpl(
.unwrap());
}
if (YGNodeAlignItem(node, child) == YGAlignBaseline) {
const float ascent = YGBaseline(child) +
const float ascent = YGBaseline(child, layoutContext) +
child
->getLeadingMargin(
YGFlexDirectionColumn, availableInnerWidth)
@@ -3340,14 +3314,16 @@ static void YGNodelayoutImpl(
true,
"multiline-stretch",
config,
layoutMarkerData);
layoutMarkerData,
layoutContext);
}
}
break;
}
case YGAlignBaseline: {
child->setLayoutPosition(
currentLead + maxAscentForCurrentLine - YGBaseline(child) +
currentLead + maxAscentForCurrentLine -
YGBaseline(child, layoutContext) +
child
->getLeadingPosition(
YGFlexDirectionColumn, availableInnerCrossDim)
@@ -3478,7 +3454,8 @@ static void YGNodelayoutImpl(
availableInnerHeight,
direction,
config,
layoutMarkerData);
layoutMarkerData,
layoutContext);
}
// STEP 11: SETTING TRAILING POSITIONS FOR CHILDREN
@@ -3711,7 +3688,8 @@ bool YGLayoutNodeInternal(
const bool performLayout,
const char* reason,
const YGConfigRef config,
YGMarkerLayoutData& layoutMarkerData) {
YGMarkerLayoutData& layoutMarkerData,
void* const layoutContext) {
YGLayout* layout = &node->getLayout();
gDepth++;
@@ -3739,7 +3717,7 @@ bool YGLayoutNodeInternal(
// dimensions. We handle nodes with measure functions specially here because
// they are the most expensive to measure, so it's worth avoiding redundant
// measurements if at all possible.
if (node->getMeasure() != nullptr) {
if (node->hasMeasureFunc()) {
const float marginAxisRow =
node->getMarginForAxis(YGFlexDirectionRow, ownerWidth).unwrap();
const float marginAxisColumn =
@@ -3814,18 +3792,18 @@ bool YGLayoutNodeInternal(
: layoutMarkerData.cachedMeasures) += 1;
if (gPrintChanges && gPrintSkips) {
YGLog(
Log::log(
node,
YGLogLevelVerbose,
nullptr,
"%s%d.{[skipped] ",
YGSpacer(gDepth),
gDepth);
if (node->getPrintFunc() != nullptr) {
node->getPrintFunc()(node);
}
YGLog(
node->print(layoutContext);
Log::log(
node,
YGLogLevelVerbose,
nullptr,
"wm: %s, hm: %s, aw: %f ah: %f => d: (%f, %f) %s\n",
YGMeasureModeName(widthMeasureMode, performLayout),
YGMeasureModeName(heightMeasureMode, performLayout),
@@ -3837,19 +3815,19 @@ bool YGLayoutNodeInternal(
}
} else {
if (gPrintChanges) {
YGLog(
Log::log(
node,
YGLogLevelVerbose,
nullptr,
"%s%d.{%s",
YGSpacer(gDepth),
gDepth,
needToVisitNode ? "*" : "");
if (node->getPrintFunc() != nullptr) {
node->getPrintFunc()(node);
}
YGLog(
node->print(layoutContext);
Log::log(
node,
YGLogLevelVerbose,
nullptr,
"wm: %s, hm: %s, aw: %f ah: %f %s\n",
YGMeasureModeName(widthMeasureMode, performLayout),
YGMeasureModeName(heightMeasureMode, performLayout),
@@ -3869,22 +3847,23 @@ bool YGLayoutNodeInternal(
ownerHeight,
performLayout,
config,
layoutMarkerData);
layoutMarkerData,
layoutContext);
if (gPrintChanges) {
YGLog(
Log::log(
node,
YGLogLevelVerbose,
nullptr,
"%s%d.}%s",
YGSpacer(gDepth),
gDepth,
needToVisitNode ? "*" : "");
if (node->getPrintFunc() != nullptr) {
node->getPrintFunc()(node);
}
YGLog(
node->print(layoutContext);
Log::log(
node,
YGLogLevelVerbose,
nullptr,
"wm: %s, hm: %s, d: (%f, %f) %s\n",
YGMeasureModeName(widthMeasureMode, performLayout),
YGMeasureModeName(heightMeasureMode, performLayout),
@@ -3901,9 +3880,9 @@ bool YGLayoutNodeInternal(
layoutMarkerData.maxMeasureCache =
layout->nextCachedMeasurementsIndex + 1;
}
if (layout->nextCachedMeasurementsIndex == YG_MAX_CACHED_RESULT_COUNT) {
if (layout->nextCachedMeasurementsIndex == usedMeasureCacheEntries) {
if (gPrintChanges) {
YGLog(node, YGLogLevelVerbose, "Out of cache entries!\n");
Log::log(node, YGLogLevelVerbose, nullptr, "Out of cache entries!\n");
}
layout->nextCachedMeasurementsIndex = 0;
}
@@ -4037,11 +4016,12 @@ static void YGRoundToPixelGrid(
}
}
void YGNodeCalculateLayout(
void YGNodeCalculateLayoutWithContext(
const YGNodeRef node,
const float ownerWidth,
const float ownerHeight,
const YGDirection ownerDirection) {
const YGDirection ownerDirection,
void* layoutContext) {
marker::MarkerSection<YGMarkerLayout> marker{node};
// Increment the generation count. This will force the recursive routine to
@@ -4105,7 +4085,8 @@ void YGNodeCalculateLayout(
true,
"initial",
node->getConfig(),
marker.data)) {
marker.data,
layoutContext)) {
node->setPosition(
node->getLayout().direction, ownerWidth, ownerHeight, ownerWidth);
YGRoundToPixelGrid(node, node->getConfig()->pointScaleFactor, 0.0f, 0.0f);
@@ -4148,7 +4129,8 @@ void YGNodeCalculateLayout(
true,
"initial",
originalNode->getConfig(),
layoutMarkerData)) {
layoutMarkerData,
layoutContext)) {
originalNode->setPosition(
originalNode->getLayout().direction,
ownerWidth,
@@ -4178,14 +4160,23 @@ void YGNodeCalculateLayout(
}
}
void YGNodeCalculateLayout(
const YGNodeRef node,
const float ownerWidth,
const float ownerHeight,
const YGDirection ownerDirection) {
YGNodeCalculateLayoutWithContext(
node, ownerWidth, ownerHeight, ownerDirection, nullptr);
}
void YGConfigSetLogger(const YGConfigRef config, YGLogger logger) {
if (logger != nullptr) {
config->logger = logger;
config->setLogger(logger);
} else {
#ifdef ANDROID
config->logger = &YGAndroidLog;
config->setLogger(&YGAndroidLog);
#else
config->logger = &YGDefaultLog;
config->setLogger(&YGDefaultLog);
#endif
}
}
@@ -4196,43 +4187,9 @@ void YGConfigSetShouldDiffLayoutWithoutLegacyStretchBehaviour(
config->shouldDiffLayoutWithoutLegacyStretchBehaviour = shouldDiffLayout;
}
static void YGVLog(
const YGConfigRef config,
const YGNodeRef node,
YGLogLevel level,
const char* format,
va_list args) {
const YGConfigRef logConfig =
config != nullptr ? config : YGConfigGetDefault();
logConfig->logger(logConfig, node, level, format, args);
if (level == YGLogLevelFatal) {
abort();
}
}
void YGLogWithConfig(
const YGConfigRef config,
YGLogLevel level,
const char* format,
...) {
va_list args;
va_start(args, format);
YGVLog(config, nullptr, level, format, args);
va_end(args);
}
void YGLog(const YGNodeRef node, YGLogLevel level, const char* format, ...) {
va_list args;
va_start(args, format);
YGVLog(
node == nullptr ? nullptr : node->getConfig(), node, level, format, args);
va_end(args);
}
void YGAssert(const bool condition, const char* message) {
if (!condition) {
YGLog(nullptr, YGLogLevelFatal, "%s\n", message);
Log::log(YGNodeRef{nullptr}, YGLogLevelFatal, nullptr, "%s\n", message);
}
}
@@ -4241,7 +4198,7 @@ void YGAssertWithNode(
const bool condition,
const char* message) {
if (!condition) {
YGLog(node, YGLogLevelFatal, "%s\n", message);
Log::log(node, YGLogLevelFatal, nullptr, "%s\n", message);
}
}
@@ -4250,7 +4207,7 @@ void YGAssertWithConfig(
const bool condition,
const char* message) {
if (!condition) {
YGLogWithConfig(config, YGLogLevelFatal, "%s\n", message);
Log::log(config, YGLogLevelFatal, nullptr, "%s\n", message);
}
}
@@ -4292,7 +4249,7 @@ void* YGConfigGetContext(const YGConfigRef config) {
void YGConfigSetCloneNodeFunc(
const YGConfigRef config,
const YGCloneNodeFunc callback) {
config->cloneNodeCallback = callback;
config->setCloneNodeCallback(callback);
}
static void YGTraverseChildrenPreOrder(

View File

@@ -132,13 +132,12 @@ WIN_EXPORT void YGNodeCopyStyle(
WIN_EXPORT void* YGNodeGetContext(YGNodeRef node);
WIN_EXPORT void YGNodeSetContext(YGNodeRef node, void* context);
void YGConfigSetPrintTreeFlag(YGConfigRef config, bool enabled);
YGMeasureFunc YGNodeGetMeasureFunc(YGNodeRef node);
bool YGNodeHasMeasureFunc(YGNodeRef node);
WIN_EXPORT void YGNodeSetMeasureFunc(YGNodeRef node, YGMeasureFunc measureFunc);
YGBaselineFunc YGNodeGetBaselineFunc(YGNodeRef node);
bool YGNodeHasBaselineFunc(YGNodeRef node);
void YGNodeSetBaselineFunc(YGNodeRef node, YGBaselineFunc baselineFunc);
YGDirtiedFunc YGNodeGetDirtiedFunc(YGNodeRef node);
void YGNodeSetDirtiedFunc(YGNodeRef node, YGDirtiedFunc dirtiedFunc);
YGPrintFunc YGNodeGetPrintFunc(YGNodeRef node);
void YGNodeSetPrintFunc(YGNodeRef node, YGPrintFunc printFunc);
WIN_EXPORT bool YGNodeGetHasNewLayout(YGNodeRef node);
WIN_EXPORT void YGNodeSetHasNewLayout(YGNodeRef node, bool hasNewLayout);
@@ -348,16 +347,6 @@ WIN_EXPORT float YGNodeLayoutGetPadding(
const YGEdge edge);
WIN_EXPORT void YGConfigSetLogger(const YGConfigRef config, YGLogger logger);
WIN_EXPORT void YGLog(
const YGNodeRef node,
YGLogLevel level,
const char* message,
...);
WIN_EXPORT void YGLogWithConfig(
const YGConfigRef config,
YGLogLevel level,
const char* format,
...);
WIN_EXPORT void YGAssert(const bool condition, const char* message);
WIN_EXPORT void YGAssertWithNode(
const YGNodeRef node,
@@ -422,6 +411,8 @@ WIN_EXPORT float YGRoundValueToPixelGrid(
const bool forceCeil,
const bool forceFloor);
void YGSetUsedCachedEntries(size_t);
YG_EXTERN_C_END
#ifdef __cplusplus

View File

@@ -28,9 +28,12 @@ public:
typename Data::type data = {};
template <typename Ret, typename... Args>
static Ret wrap(YGNodeRef node, Ret (*fn)(Args...), Args... args) {
static Ret wrap(
YGNodeRef node,
Ret (YGNode::*method)(Args...),
Args... args) {
MarkerSection<MarkerType> section{node};
return fn(std::forward<Args>(args)...);
return (node->*method)(std::forward<Args>(args)...);
}
private:

68
yoga/log.cpp Normal file
View File

@@ -0,0 +1,68 @@
/**
* 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 "log.h"
#include "Yoga.h"
#include "YGConfig.h"
#include "YGNode.h"
namespace facebook {
namespace yoga {
namespace detail {
namespace {
void vlog(
YGConfig* config,
YGNode* node,
YGLogLevel level,
void* context,
const char* format,
va_list args) {
YGConfig* logConfig = config != nullptr ? config : YGConfigGetDefault();
logConfig->log(logConfig, node, level, context, format, args);
if (level == YGLogLevelFatal) {
abort();
}
}
} // namespace
void Log::log(
YGNode* node,
YGLogLevel level,
void* context,
const char* format,
...) noexcept {
va_list args;
va_start(args, format);
vlog(
node == nullptr ? nullptr : node->getConfig(),
node,
level,
context,
format,
args);
va_end(args);
}
void Log::log(
YGConfig* config,
YGLogLevel level,
void* context,
const char* format,
...) noexcept {
va_list args;
va_start(args, format);
vlog(config, nullptr, level, context, format, args);
va_end(args);
}
} // namespace detail
} // namespace yoga
} // namespace facebook

37
yoga/log.h Normal file
View File

@@ -0,0 +1,37 @@
/**
* 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.
*/
#pragma once
#include "YGEnums.h"
struct YGNode;
struct YGConfig;
namespace facebook {
namespace yoga {
namespace detail {
struct Log {
static void log(
YGNode* node,
YGLogLevel level,
void*,
const char* message,
...) noexcept;
static void log(
YGConfig* config,
YGLogLevel level,
void*,
const char* format,
...) noexcept;
};
} // namespace detail
} // namespace yoga
} // namespace facebook