Add YGConfigGetInstanceCount #497

Closed
splhack wants to merge 2 commits from config-instance-count into master
5 changed files with 109 additions and 37 deletions

View File

@@ -15,6 +15,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Native.cs" /> <Compile Include="$(MSBuildThisFileDirectory)Native.cs" />
<Compile Include="$(MSBuildThisFileDirectory)YogaAlign.cs" /> <Compile Include="$(MSBuildThisFileDirectory)YogaAlign.cs" />
<Compile Include="$(MSBuildThisFileDirectory)YogaBaselineFunc.cs" /> <Compile Include="$(MSBuildThisFileDirectory)YogaBaselineFunc.cs" />
<Compile Include="$(MSBuildThisFileDirectory)YogaConfig.cs" />
<Compile Include="$(MSBuildThisFileDirectory)YogaConstants.cs" /> <Compile Include="$(MSBuildThisFileDirectory)YogaConstants.cs" />
<Compile Include="$(MSBuildThisFileDirectory)YogaDimension.cs" /> <Compile Include="$(MSBuildThisFileDirectory)YogaDimension.cs" />
<Compile Include="$(MSBuildThisFileDirectory)YogaDirection.cs" /> <Compile Include="$(MSBuildThisFileDirectory)YogaDirection.cs" />

View File

@@ -138,6 +138,9 @@ namespace Facebook.Yoga
[DllImport(DllName, ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)] [DllImport(DllName, ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
public static extern int YGNodeGetInstanceCount(); public static extern int YGNodeGetInstanceCount();
[DllImport(DllName, ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
public static extern int YGConfigGetInstanceCount();
[DllImport(DllName, ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)] [DllImport(DllName, ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
public static extern void YGConfigSetExperimentalFeatureEnabled( public static extern void YGConfigSetExperimentalFeatureEnabled(
YGConfigHandle config, YGConfigHandle config,
@@ -149,6 +152,19 @@ namespace Facebook.Yoga
YGConfigHandle config, YGConfigHandle config,
YogaExperimentalFeature feature); YogaExperimentalFeature feature);
[DllImport(DllName, ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
public static extern void YGConfigSetUseWebDefaults(
YGConfigHandle config,
bool useWebDefaults);
[DllImport(DllName, ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
public static extern bool YGConfigGetUseWebDefaults(YGConfigHandle config);
[DllImport(DllName, ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
public static extern void YGConfigSetPointScaleFactor(
YGConfigHandle config,
float pixelsInPoint);
[DllImport(DllName, ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)] [DllImport(DllName, ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
public static extern void YGNodeInsertChild( public static extern void YGNodeInsertChild(
YGNodeHandle node, YGNodeHandle node,

View File

@@ -0,0 +1,72 @@
/**
* Copyright (c) 2014-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
using System;
namespace Facebook.Yoga
{
public class YogaConfig
{
private Native.YGConfigHandle _ygConfig;
public YogaConfig()
{
_ygConfig = Native.YGConfigNew();
if (_ygConfig.IsInvalid)
{
throw new InvalidOperationException("Failed to allocate native memory");
}
}
internal Native.YGConfigHandle Handle
{
get {
return _ygConfig;
}
}
public void SetExperimentalFeatureEnabled(
YogaExperimentalFeature feature,
bool enabled)
{
Native.YGConfigSetExperimentalFeatureEnabled(_ygConfig, feature, enabled);
}
public bool IsExperimentalFeatureEnabled(YogaExperimentalFeature feature)
{
return Native.YGConfigIsExperimentalFeatureEnabled(_ygConfig, feature);
}
public bool UseWebDefaults
{
get
{
return Native.YGConfigGetUseWebDefaults(_ygConfig);
}
set
{
Native.YGConfigSetUseWebDefaults(_ygConfig, value);
}
}
public float PointScaleFactor
{
set
{
Native.YGConfigSetPointScaleFactor(_ygConfig, value);
}
}
public static int GetInstanceCount()
{
return Native.YGConfigGetInstanceCount();
}
}
}

View File

@@ -24,43 +24,10 @@ using AOT;
namespace Facebook.Yoga namespace Facebook.Yoga
{ {
public class YogaConfig
{
private Native.YGConfigHandle _ygConfig;
public YogaConfig()
{
_ygConfig = Native.YGConfigNew();
if (_ygConfig.IsInvalid)
{
throw new InvalidOperationException("Failed to allocate native memory");
}
}
internal Native.YGConfigHandle Handle {
get {
return _ygConfig;
}
}
public void SetExperimentalFeatureEnabled(
YogaExperimentalFeature feature,
bool enabled)
{
Native.YGConfigSetExperimentalFeatureEnabled(_ygConfig, feature, enabled);
}
public bool IsExperimentalFeatureEnabled(YogaExperimentalFeature feature)
{
return Native.YGConfigIsExperimentalFeatureEnabled(_ygConfig, feature);
}
}
public partial class YogaNode : IEnumerable<YogaNode> public partial class YogaNode : IEnumerable<YogaNode>
{ {
private Native.YGNodeHandle _ygNode; private Native.YGNodeHandle _ygNode;
private YogaConfig _config;
private WeakReference _parent; private WeakReference _parent;
private List<YogaNode> _children; private List<YogaNode> _children;
private MeasureFunction _measureFunction; private MeasureFunction _measureFunction;
@@ -89,7 +56,15 @@ namespace Facebook.Yoga
{ {
YogaLogger.Initialize(); YogaLogger.Initialize();
_ygNode = Native.YGNodeNewWithConfig(config.Handle); if (config != null)
{
_config = config;
_ygNode = Native.YGNodeNewWithConfig(_config.Handle);
}
else
{
_ygNode = Native.YGNodeNew();
}
if (_ygNode.IsInvalid) if (_ygNode.IsInvalid)
{ {
throw new InvalidOperationException("Failed to allocate native memory"); throw new InvalidOperationException("Failed to allocate native memory");
@@ -97,7 +72,7 @@ namespace Facebook.Yoga
} }
public YogaNode(YogaNode srcNode) public YogaNode(YogaNode srcNode)
: this() : this(srcNode._config)
{ {
CopyStyle(srcNode); CopyStyle(srcNode);
} }

View File

@@ -308,6 +308,7 @@ static inline float YGResolveValueMargin(const YGValue *const value, const float
} }
int32_t gNodeInstanceCount = 0; int32_t gNodeInstanceCount = 0;
int32_t gConfigInstanceCount = 0;
WIN_EXPORT YGNodeRef YGNodeNewWithConfig(const YGConfigRef config) { WIN_EXPORT YGNodeRef YGNodeNewWithConfig(const YGConfigRef config) {
const YGNodeRef node = gYGMalloc(sizeof(YGNode)); const YGNodeRef node = gYGMalloc(sizeof(YGNode));
@@ -373,15 +374,21 @@ int32_t YGNodeGetInstanceCount(void) {
return gNodeInstanceCount; return gNodeInstanceCount;
} }
int32_t YGConfigGetInstanceCount(void) {
return gConfigInstanceCount;
}
YGConfigRef YGConfigNew(void) { YGConfigRef YGConfigNew(void) {
const YGConfigRef config = gYGMalloc(sizeof(YGConfig)); const YGConfigRef config = gYGMalloc(sizeof(YGConfig));
YG_ASSERT(config, "Could not allocate memory for config"); YG_ASSERT(config, "Could not allocate memory for config");
gConfigInstanceCount++;
memcpy(config, &gYGConfigDefaults, sizeof(YGConfig)); memcpy(config, &gYGConfigDefaults, sizeof(YGConfig));
return config; return config;
} }
void YGConfigFree(const YGConfigRef config) { void YGConfigFree(const YGConfigRef config) {
gYGFree(config); gYGFree(config);
gConfigInstanceCount--;
} }
static void YGNodeMarkDirtyInternal(const YGNodeRef node) { static void YGNodeMarkDirtyInternal(const YGNodeRef node) {
@@ -3468,7 +3475,8 @@ bool YGConfigGetUseWebDefaults(const YGConfigRef config) {
} }
void YGSetMemoryFuncs(YGMalloc ygmalloc, YGCalloc yccalloc, YGRealloc ygrealloc, YGFree ygfree) { void YGSetMemoryFuncs(YGMalloc ygmalloc, YGCalloc yccalloc, YGRealloc ygrealloc, YGFree ygfree) {
YG_ASSERT(gNodeInstanceCount == 0, "Cannot set memory functions: all node must be freed first"); YG_ASSERT(gNodeInstanceCount == 0 && gConfigInstanceCount == 0,
"Cannot set memory functions: all node must be freed first");
YG_ASSERT((ygmalloc == NULL && yccalloc == NULL && ygrealloc == NULL && ygfree == NULL) || YG_ASSERT((ygmalloc == NULL && yccalloc == NULL && ygrealloc == NULL && ygfree == NULL) ||
(ygmalloc != NULL && yccalloc != NULL && ygrealloc != NULL && ygfree != NULL), (ygmalloc != NULL && yccalloc != NULL && ygrealloc != NULL && ygfree != NULL),
"Cannot set memory functions: functions must be all NULL or Non-NULL"); "Cannot set memory functions: functions must be all NULL or Non-NULL");