Skip to content

Commit

Permalink
Add YGErrata integration within C ABI (#37075)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #37075

X-link: facebook/yoga#1255

This diff wires up YGErrata to a public API, along with existing functions to set UseLegacyStretchBehaviour.

The `UseLegacyStretchBehaviour` functions will be removed after the world internally is transitioned to `YGConfigSetErrata`. This is intentionally breaking, since most users previously enabling `UseLegacyStretchBehaviour` will want to pick a new appropriate errata setting. Internally, users of the API will be moved to`YGErrataAll`.

The overall change looks like:
1. Clean up YGConfig to use accessors/setters
2. Change up YGconfig internal storage
    1. Fabric has a config per ShadowNode, so it makes sense to do some size optimization before adding more (free-form bools to bitfield, `std::array<bool,>` to `std::bitset` since not specialized)
3. Wire accessor/setter of UseLegacyStretchBehaviour to errata while both APIs exist
4. Add errata APIs to C ABI

After this we will need to expose the ABI to more language projections, and (more involved), add usages of the API to internal consumption of Yoga before adding more errata and removing `UseLegacyStretchBehaviour`.

Note that this API representation is similar, but distinct to `YGExperimentalFeature`. I think that API may also have made sense as an enum bitset, like we explicitly want for the new API, but it's not really worth changing the existing API to make that happen.

Reviewed By: rshest

Differential Revision: D45254097

fbshipit-source-id: 5c725ce5a77b25c1356f753d11c468587dbd8ded
  • Loading branch information
NickGerleman authored and facebook-github-bot committed Apr 27, 2023
1 parent 72fb75d commit 0fd0f56
Show file tree
Hide file tree
Showing 6 changed files with 222 additions and 97 deletions.
5 changes: 5 additions & 0 deletions packages/react-native/ReactCommon/yoga/yoga/BitUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#pragma once

#include <bitset>
#include <cstdio>
#include <cstdint>
#include "YGEnums.h"
Expand All @@ -16,6 +17,10 @@ namespace yoga {

namespace detail {

// std::bitset with one bit for each option defined in YG_ENUM_SEQ_DECL
template <typename Enum>
using EnumBitset = std::bitset<facebook::yoga::enums::count<Enum>()>;

constexpr size_t log2ceilFn(size_t n) {
return n < 1 ? 0 : (1 + log2ceilFn(n / 2));
}
Expand Down
90 changes: 85 additions & 5 deletions packages/react-native/ReactCommon/yoga/yoga/YGConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,75 @@

#include "YGConfig.h"

using namespace facebook::yoga::detail;

YGConfig::YGConfig(YGLogger logger) : cloneNodeCallback_{nullptr} {
setLogger(logger);
}

void YGConfig::setUseWebDefaults(bool useWebDefaults) {
flags_.useWebDefaults = useWebDefaults;
}

bool YGConfig::useWebDefaults() const {
return flags_.useWebDefaults;
}

void YGConfig::setShouldPrintTree(bool printTree) {
flags_.printTree = printTree;
}

bool YGConfig::shouldPrintTree() const {
return flags_.printTree;
}

void YGConfig::setExperimentalFeatureEnabled(
YGExperimentalFeature feature,
bool enabled) {
experimentalFeatures_.set(feature, enabled);
}

bool YGConfig::isExperimentalFeatureEnabled(
YGExperimentalFeature feature) const {
return experimentalFeatures_.test(feature);
}

void YGConfig::setErrata(YGErrata errata) {
errata_ = errata;
}

YGErrata YGConfig::getErrata() const {
return errata_;
}

void YGConfig::setPointScaleFactor(float pointScaleFactor) {
pointScaleFactor_ = pointScaleFactor;
}

float YGConfig::getPointScaleFactor() const {
return pointScaleFactor_;
}

void YGConfig::setContext(void* context) {
context_ = context;
}

void* YGConfig::getContext() const {
return context_;
}

void YGConfig::setLogger(YGLogger logger) {
logger_.noContext = logger;
loggerUsesContext_ = false;
flags_.loggerUsesContext = false;
}

void YGConfig::setLogger(LogWithContextFn logger) {
logger_.withContext = logger;
flags_.loggerUsesContext = true;
}

void YGConfig::setLogger(std::nullptr_t) {
setLogger(YGLogger{nullptr});
}

void YGConfig::log(
Expand All @@ -18,22 +84,36 @@ void YGConfig::log(
YGLogLevel logLevel,
void* logContext,
const char* format,
va_list args) {
if (loggerUsesContext_) {
va_list args) const {
if (flags_.loggerUsesContext) {
logger_.withContext(config, node, logLevel, logContext, format, args);
} else {
logger_.noContext(config, node, logLevel, format, args);
}
}

void YGConfig::setCloneNodeCallback(YGCloneNodeFunc cloneNode) {
cloneNodeCallback_.noContext = cloneNode;
flags_.cloneNodeUsesContext = false;
}

void YGConfig::setCloneNodeCallback(CloneWithContextFn cloneNode) {
cloneNodeCallback_.withContext = cloneNode;
flags_.cloneNodeUsesContext = true;
}

void YGConfig::setCloneNodeCallback(std::nullptr_t) {
setCloneNodeCallback(YGCloneNodeFunc{nullptr});
}

YGNodeRef YGConfig::cloneNode(
YGNodeRef node,
YGNodeRef owner,
int childIndex,
void* cloneContext) {
void* cloneContext) const {
YGNodeRef clone = nullptr;
if (cloneNodeCallback_.noContext != nullptr) {
clone = cloneNodeUsesContext_
clone = flags_.cloneNodeUsesContext
? cloneNodeCallback_.withContext(node, owner, childIndex, cloneContext)
: cloneNodeCallback_.noContext(node, owner, childIndex);
}
Expand Down
124 changes: 73 additions & 51 deletions packages/react-native/ReactCommon/yoga/yoga/YGConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,69 +9,91 @@

#include "Yoga-internal.h"
#include "Yoga.h"
#include "BitUtils.h"

namespace facebook {
namespace yoga {
namespace detail {

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);

#pragma pack(push)
#pragma pack(1)
struct YGConfigFlags {
bool useWebDefaults : 1;
bool printTree : 1;
bool cloneNodeUsesContext : 1;
bool loggerUsesContext : 1;
};
#pragma pack(pop)

} // namespace detail
} // namespace yoga
} // namespace facebook

struct YOGA_EXPORT YGConfig {
using LogWithContextFn = int (*)(
YGConfigRef config,
YGNodeRef node,
YGLogLevel level,
void* context,
const char* format,
va_list args);
using CloneWithContextFn = YGNodeRef (*)(
YGConfig(YGLogger logger);

void setUseWebDefaults(bool useWebDefaults);
bool useWebDefaults() const;

void setShouldPrintTree(bool printTree);
bool shouldPrintTree() const;

void setExperimentalFeatureEnabled(
YGExperimentalFeature feature,
bool enabled);
bool isExperimentalFeatureEnabled(YGExperimentalFeature feature) const;

void setErrata(YGErrata errata);
YGErrata getErrata() const;

void setPointScaleFactor(float pointScaleFactor);
float getPointScaleFactor() const;

void setContext(void* context);
void* getContext() const;

void setLogger(YGLogger logger);
void setLogger(facebook::yoga::detail::LogWithContextFn logger);
void setLogger(std::nullptr_t);
void log(YGConfig*, YGNode*, YGLogLevel, void*, const char*, va_list) const;

void setCloneNodeCallback(YGCloneNodeFunc cloneNode);
void setCloneNodeCallback(
facebook::yoga::detail::CloneWithContextFn cloneNode);
void setCloneNodeCallback(std::nullptr_t);
YGNodeRef cloneNode(
YGNodeRef node,
YGNodeRef owner,
int childIndex,
void* cloneContext);
void* cloneContext) const;

private:
union {
CloneWithContextFn withContext;
facebook::yoga::detail::CloneWithContextFn withContext;
YGCloneNodeFunc noContext;
} cloneNodeCallback_;
union {
LogWithContextFn withContext;
facebook::yoga::detail::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;
std::array<bool, facebook::yoga::enums::count<YGExperimentalFeature>()>
experimentalFeatures = {};
void* context = 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});
}
facebook::yoga::detail::YGConfigFlags flags_{};
facebook::yoga::detail::EnumBitset<YGExperimentalFeature>
experimentalFeatures_{};
YGErrata errata_ = YGErrataNone;
float pointScaleFactor_ = 1.0f;
void* context_ = nullptr;
};
10 changes: 5 additions & 5 deletions packages/react-native/ReactCommon/yoga/yoga/YGNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ YGNode::YGNode(const YGConfigRef config) : config_{config} {
config != nullptr, "Attempting to construct YGNode with null config");

flags_.hasNewLayout = true;
if (config->useWebDefaults) {
if (config->useWebDefaults()) {
useWebDefaults();
}
};
Expand Down Expand Up @@ -267,7 +267,7 @@ void YGNode::setConfig(YGConfigRef config) {
YGAssert(config != nullptr, "Attempting to set a null config on a YGNode");
YGAssertWithConfig(
config,
config->useWebDefaults == config_->useWebDefaults,
config->useWebDefaults() == config_->useWebDefaults(),
"UseWebDefaults may not be changed after constructing a YGNode");
config_ = config;
}
Expand Down Expand Up @@ -419,7 +419,7 @@ YGValue YGNode::resolveFlexBasisPtr() const {
return flexBasis;
}
if (!style_.flex().isUndefined() && style_.flex().unwrap() > 0.0f) {
return config_->useWebDefaults ? YGValueAuto : YGValueZero;
return config_->useWebDefaults() ? YGValueAuto : YGValueZero;
}
return YGValueAuto;
}
Expand Down Expand Up @@ -495,11 +495,11 @@ float YGNode::resolveFlexShrink() const {
if (!style_.flexShrink().isUndefined()) {
return style_.flexShrink().unwrap();
}
if (!config_->useWebDefaults && !style_.flex().isUndefined() &&
if (!config_->useWebDefaults() && !style_.flex().isUndefined() &&
style_.flex().unwrap() < 0.0f) {
return -style_.flex().unwrap();
}
return config_->useWebDefaults ? kWebDefaultFlexShrink : kDefaultFlexShrink;
return config_->useWebDefaults() ? kWebDefaultFlexShrink : kDefaultFlexShrink;
}

bool YGNode::isNodeFlexible() {
Expand Down
Loading

0 comments on commit 0fd0f56

Please sign in to comment.