Remove BitUtils Usage in YGNode (#1250)
Summary: Pull Request resolved: https://github.com/facebook/yoga/pull/1250 X-link: https://github.com/facebook/react-native/pull/36988 BitUtils functions in Yoga are like bit fields, with more steps, and more error prone (you need to work with explicit offsets which can be tricky for anything variable length). Replace usage with a bitfield struct. Eventually I'd like to remove the BitUtils functions in general. Changelog: [Internal] Reviewed By: rshest Differential Revision: D45133645 fbshipit-source-id: aa1430df5e2fb71ed9d2a5f5b1a35429b71c7069
This commit is contained in:
committed by
Facebook GitHub Bot
parent
19e15a4455
commit
88f1f3cab9
@@ -8,7 +8,6 @@
|
|||||||
#include "YGNode.h"
|
#include "YGNode.h"
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include "CompactValue.h"
|
|
||||||
#include "Utils.h"
|
#include "Utils.h"
|
||||||
|
|
||||||
using namespace facebook;
|
using namespace facebook;
|
||||||
@@ -16,7 +15,7 @@ using facebook::yoga::detail::CompactValue;
|
|||||||
|
|
||||||
YGNode::YGNode(YGNode&& node) {
|
YGNode::YGNode(YGNode&& node) {
|
||||||
context_ = node.context_;
|
context_ = node.context_;
|
||||||
flags = node.flags;
|
flags_ = node.flags_;
|
||||||
measure_ = node.measure_;
|
measure_ = node.measure_;
|
||||||
baseline_ = node.baseline_;
|
baseline_ = node.baseline_;
|
||||||
print_ = node.print_;
|
print_ = node.print_;
|
||||||
@@ -42,7 +41,7 @@ YGNode::YGNode(const YGNode& node, YGConfigRef config) : YGNode{node} {
|
|||||||
|
|
||||||
void YGNode::print(void* printContext) {
|
void YGNode::print(void* printContext) {
|
||||||
if (print_.noContext != nullptr) {
|
if (print_.noContext != nullptr) {
|
||||||
if (facebook::yoga::detail::getBooleanData(flags, printUsesContext_)) {
|
if (flags_.printUsesContext) {
|
||||||
print_.withContext(this, printContext);
|
print_.withContext(this, printContext);
|
||||||
} else {
|
} else {
|
||||||
print_.noContext(this);
|
print_.noContext(this);
|
||||||
@@ -202,14 +201,14 @@ YGSize YGNode::measure(
|
|||||||
float height,
|
float height,
|
||||||
YGMeasureMode heightMode,
|
YGMeasureMode heightMode,
|
||||||
void* layoutContext) {
|
void* layoutContext) {
|
||||||
return facebook::yoga::detail::getBooleanData(flags, measureUsesContext_)
|
return flags_.measureUsesContext
|
||||||
? measure_.withContext(
|
? measure_.withContext(
|
||||||
this, width, widthMode, height, heightMode, layoutContext)
|
this, width, widthMode, height, heightMode, layoutContext)
|
||||||
: measure_.noContext(this, width, widthMode, height, heightMode);
|
: measure_.noContext(this, width, widthMode, height, heightMode);
|
||||||
}
|
}
|
||||||
|
|
||||||
float YGNode::baseline(float width, float height, void* layoutContext) {
|
float YGNode::baseline(float width, float height, void* layoutContext) {
|
||||||
return facebook::yoga::detail::getBooleanData(flags, baselineUsesContext_)
|
return flags_.baselineUsesContext
|
||||||
? baseline_.withContext(this, width, height, layoutContext)
|
? baseline_.withContext(this, width, height, layoutContext)
|
||||||
: baseline_.noContext(this, width, height);
|
: baseline_.noContext(this, width, height);
|
||||||
}
|
}
|
||||||
@@ -236,14 +235,14 @@ void YGNode::setMeasureFunc(decltype(YGNode::measure_) measureFunc) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void YGNode::setMeasureFunc(YGMeasureFunc measureFunc) {
|
void YGNode::setMeasureFunc(YGMeasureFunc measureFunc) {
|
||||||
facebook::yoga::detail::setBooleanData(flags, measureUsesContext_, false);
|
flags_.measureUsesContext = false;
|
||||||
decltype(YGNode::measure_) m;
|
decltype(YGNode::measure_) m;
|
||||||
m.noContext = measureFunc;
|
m.noContext = measureFunc;
|
||||||
setMeasureFunc(m);
|
setMeasureFunc(m);
|
||||||
}
|
}
|
||||||
|
|
||||||
YOGA_EXPORT void YGNode::setMeasureFunc(MeasureWithContextFn measureFunc) {
|
YOGA_EXPORT void YGNode::setMeasureFunc(MeasureWithContextFn measureFunc) {
|
||||||
facebook::yoga::detail::setBooleanData(flags, measureUsesContext_, true);
|
flags_.measureUsesContext = true;
|
||||||
decltype(YGNode::measure_) m;
|
decltype(YGNode::measure_) m;
|
||||||
m.withContext = measureFunc;
|
m.withContext = measureFunc;
|
||||||
setMeasureFunc(m);
|
setMeasureFunc(m);
|
||||||
@@ -262,10 +261,10 @@ void YGNode::insertChild(YGNodeRef child, uint32_t index) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void YGNode::setDirty(bool isDirty) {
|
void YGNode::setDirty(bool isDirty) {
|
||||||
if (isDirty == facebook::yoga::detail::getBooleanData(flags, isDirty_)) {
|
if (isDirty == flags_.isDirty) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
facebook::yoga::detail::setBooleanData(flags, isDirty_, isDirty);
|
flags_.isDirty = isDirty;
|
||||||
if (isDirty && dirtied_) {
|
if (isDirty && dirtied_) {
|
||||||
dirtied_(this);
|
dirtied_(this);
|
||||||
}
|
}
|
||||||
@@ -408,9 +407,7 @@ YGValue YGNode::resolveFlexBasisPtr() const {
|
|||||||
return flexBasis;
|
return flexBasis;
|
||||||
}
|
}
|
||||||
if (!style_.flex().isUndefined() && style_.flex().unwrap() > 0.0f) {
|
if (!style_.flex().isUndefined() && style_.flex().unwrap() > 0.0f) {
|
||||||
return facebook::yoga::detail::getBooleanData(flags, useWebDefaults_)
|
return flags_.useWebDefaults ? YGValueAuto : YGValueZero;
|
||||||
? YGValueAuto
|
|
||||||
: YGValueZero;
|
|
||||||
}
|
}
|
||||||
return YGValueAuto;
|
return YGValueAuto;
|
||||||
}
|
}
|
||||||
@@ -449,7 +446,7 @@ void YGNode::cloneChildrenIfNeeded(void* cloneContext) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void YGNode::markDirtyAndPropagate() {
|
void YGNode::markDirtyAndPropagate() {
|
||||||
if (!facebook::yoga::detail::getBooleanData(flags, isDirty_)) {
|
if (!flags_.isDirty) {
|
||||||
setDirty(true);
|
setDirty(true);
|
||||||
setLayoutComputedFlexBasis(YGFloatOptional());
|
setLayoutComputedFlexBasis(YGFloatOptional());
|
||||||
if (owner_) {
|
if (owner_) {
|
||||||
@@ -459,7 +456,7 @@ void YGNode::markDirtyAndPropagate() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void YGNode::markDirtyAndPropagateDownwards() {
|
void YGNode::markDirtyAndPropagateDownwards() {
|
||||||
facebook::yoga::detail::setBooleanData(flags, isDirty_, true);
|
flags_.isDirty = true;
|
||||||
for_each(children_.begin(), children_.end(), [](YGNodeRef childNode) {
|
for_each(children_.begin(), children_.end(), [](YGNodeRef childNode) {
|
||||||
childNode->markDirtyAndPropagateDownwards();
|
childNode->markDirtyAndPropagateDownwards();
|
||||||
});
|
});
|
||||||
@@ -486,13 +483,11 @@ float YGNode::resolveFlexShrink() const {
|
|||||||
if (!style_.flexShrink().isUndefined()) {
|
if (!style_.flexShrink().isUndefined()) {
|
||||||
return style_.flexShrink().unwrap();
|
return style_.flexShrink().unwrap();
|
||||||
}
|
}
|
||||||
if (!facebook::yoga::detail::getBooleanData(flags, useWebDefaults_) &&
|
if (!flags_.useWebDefaults && !style_.flex().isUndefined() &&
|
||||||
!style_.flex().isUndefined() && style_.flex().unwrap() < 0.0f) {
|
style_.flex().unwrap() < 0.0f) {
|
||||||
return -style_.flex().unwrap();
|
return -style_.flex().unwrap();
|
||||||
}
|
}
|
||||||
return facebook::yoga::detail::getBooleanData(flags, useWebDefaults_)
|
return flags_.useWebDefaults ? kWebDefaultFlexShrink : kDefaultFlexShrink;
|
||||||
? kWebDefaultFlexShrink
|
|
||||||
: kDefaultFlexShrink;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool YGNode::isNodeFlexible() {
|
bool YGNode::isNodeFlexible() {
|
||||||
@@ -570,8 +565,7 @@ void YGNode::reset() {
|
|||||||
|
|
||||||
clearChildren();
|
clearChildren();
|
||||||
|
|
||||||
auto webDefaults =
|
auto webDefaults = flags_.useWebDefaults;
|
||||||
facebook::yoga::detail::getBooleanData(flags, useWebDefaults_);
|
|
||||||
*this = YGNode{getConfig()};
|
*this = YGNode{getConfig()};
|
||||||
if (webDefaults) {
|
if (webDefaults) {
|
||||||
useWebDefaults();
|
useWebDefaults();
|
||||||
|
@@ -11,7 +11,6 @@
|
|||||||
|
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include "BitUtils.h"
|
|
||||||
#include "CompactValue.h"
|
#include "CompactValue.h"
|
||||||
#include "YGConfig.h"
|
#include "YGConfig.h"
|
||||||
#include "YGLayout.h"
|
#include "YGLayout.h"
|
||||||
@@ -20,6 +19,20 @@
|
|||||||
|
|
||||||
YGConfigRef YGConfigGetDefault();
|
YGConfigRef YGConfigGetDefault();
|
||||||
|
|
||||||
|
#pragma pack(push)
|
||||||
|
#pragma pack(1)
|
||||||
|
struct YGNodeFlags {
|
||||||
|
bool hasNewLayout : 1;
|
||||||
|
bool isReferenceBaseline : 1;
|
||||||
|
bool isDirty : 1;
|
||||||
|
uint8_t nodeType : 1;
|
||||||
|
bool measureUsesContext : 1;
|
||||||
|
bool baselineUsesContext : 1;
|
||||||
|
bool printUsesContext : 1;
|
||||||
|
bool useWebDefaults : 1;
|
||||||
|
};
|
||||||
|
#pragma pack(pop)
|
||||||
|
|
||||||
struct YOGA_EXPORT YGNode {
|
struct YOGA_EXPORT YGNode {
|
||||||
using MeasureWithContextFn =
|
using MeasureWithContextFn =
|
||||||
YGSize (*)(YGNode*, float, YGMeasureMode, float, YGMeasureMode, void*);
|
YGSize (*)(YGNode*, float, YGMeasureMode, float, YGMeasureMode, void*);
|
||||||
@@ -27,17 +40,8 @@ struct YOGA_EXPORT YGNode {
|
|||||||
using PrintWithContextFn = void (*)(YGNode*, void*);
|
using PrintWithContextFn = void (*)(YGNode*, void*);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static constexpr size_t hasNewLayout_ = 0;
|
|
||||||
static constexpr size_t isReferenceBaseline_ = 1;
|
|
||||||
static constexpr size_t isDirty_ = 2;
|
|
||||||
static constexpr size_t nodeType_ = 3;
|
|
||||||
static constexpr size_t measureUsesContext_ = 4;
|
|
||||||
static constexpr size_t baselineUsesContext_ = 5;
|
|
||||||
static constexpr size_t printUsesContext_ = 6;
|
|
||||||
static constexpr size_t useWebDefaults_ = 7;
|
|
||||||
|
|
||||||
void* context_ = nullptr;
|
void* context_ = nullptr;
|
||||||
uint8_t flags = 1;
|
YGNodeFlags flags_ = {};
|
||||||
uint8_t reserved_ = 0;
|
uint8_t reserved_ = 0;
|
||||||
union {
|
union {
|
||||||
YGMeasureFunc noContext;
|
YGMeasureFunc noContext;
|
||||||
@@ -69,7 +73,7 @@ private:
|
|||||||
void setBaselineFunc(decltype(baseline_));
|
void setBaselineFunc(decltype(baseline_));
|
||||||
|
|
||||||
void useWebDefaults() {
|
void useWebDefaults() {
|
||||||
facebook::yoga::detail::setBooleanData(flags, useWebDefaults_, true);
|
flags_.useWebDefaults = true;
|
||||||
style_.flexDirection() = YGFlexDirectionRow;
|
style_.flexDirection() = YGFlexDirectionRow;
|
||||||
style_.alignContent() = YGAlignStretch;
|
style_.alignContent() = YGAlignStretch;
|
||||||
}
|
}
|
||||||
@@ -86,6 +90,8 @@ private:
|
|||||||
public:
|
public:
|
||||||
YGNode() : YGNode{YGConfigGetDefault()} {}
|
YGNode() : YGNode{YGConfigGetDefault()} {}
|
||||||
explicit YGNode(const YGConfigRef config) : config_{config} {
|
explicit YGNode(const YGConfigRef config) : config_{config} {
|
||||||
|
flags_.hasNewLayout = true;
|
||||||
|
|
||||||
if (config->useWebDefaults) {
|
if (config->useWebDefaults) {
|
||||||
useWebDefaults();
|
useWebDefaults();
|
||||||
}
|
}
|
||||||
@@ -113,12 +119,10 @@ public:
|
|||||||
|
|
||||||
void print(void*);
|
void print(void*);
|
||||||
|
|
||||||
bool getHasNewLayout() const {
|
bool getHasNewLayout() const { return flags_.hasNewLayout; }
|
||||||
return facebook::yoga::detail::getBooleanData(flags, hasNewLayout_);
|
|
||||||
}
|
|
||||||
|
|
||||||
YGNodeType getNodeType() const {
|
YGNodeType getNodeType() const {
|
||||||
return facebook::yoga::detail::getEnumData<YGNodeType>(flags, nodeType_);
|
return static_cast<YGNodeType>(flags_.nodeType);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool hasMeasureFunc() const noexcept { return measure_.noContext != nullptr; }
|
bool hasMeasureFunc() const noexcept { return measure_.noContext != nullptr; }
|
||||||
@@ -145,9 +149,7 @@ public:
|
|||||||
|
|
||||||
uint32_t getLineIndex() const { return lineIndex_; }
|
uint32_t getLineIndex() const { return lineIndex_; }
|
||||||
|
|
||||||
bool isReferenceBaseline() {
|
bool isReferenceBaseline() { return flags_.isReferenceBaseline; }
|
||||||
return facebook::yoga::detail::getBooleanData(flags, isReferenceBaseline_);
|
|
||||||
}
|
|
||||||
|
|
||||||
// returns the YGNodeRef that owns this YGNode. An owner is used to identify
|
// returns the YGNodeRef that owns this YGNode. An owner is used to identify
|
||||||
// the YogaTree that a YGNode belongs to. This method will return the parent
|
// the YogaTree that a YGNode belongs to. This method will return the parent
|
||||||
@@ -180,9 +182,7 @@ public:
|
|||||||
|
|
||||||
YGConfigRef getConfig() const { return config_; }
|
YGConfigRef getConfig() const { return config_; }
|
||||||
|
|
||||||
bool isDirty() const {
|
bool isDirty() const { return flags_.isDirty; }
|
||||||
return facebook::yoga::detail::getBooleanData(flags, isDirty_);
|
|
||||||
}
|
|
||||||
|
|
||||||
std::array<YGValue, 2> getResolvedDimensions() const {
|
std::array<YGValue, 2> getResolvedDimensions() const {
|
||||||
return resolvedDimensions_;
|
return resolvedDimensions_;
|
||||||
@@ -252,21 +252,20 @@ public:
|
|||||||
|
|
||||||
void setPrintFunc(YGPrintFunc printFunc) {
|
void setPrintFunc(YGPrintFunc printFunc) {
|
||||||
print_.noContext = printFunc;
|
print_.noContext = printFunc;
|
||||||
facebook::yoga::detail::setBooleanData(flags, printUsesContext_, false);
|
flags_.printUsesContext = false;
|
||||||
}
|
}
|
||||||
void setPrintFunc(PrintWithContextFn printFunc) {
|
void setPrintFunc(PrintWithContextFn printFunc) {
|
||||||
print_.withContext = printFunc;
|
print_.withContext = printFunc;
|
||||||
facebook::yoga::detail::setBooleanData(flags, printUsesContext_, true);
|
flags_.printUsesContext = true;
|
||||||
}
|
}
|
||||||
void setPrintFunc(std::nullptr_t) { setPrintFunc(YGPrintFunc{nullptr}); }
|
void setPrintFunc(std::nullptr_t) { setPrintFunc(YGPrintFunc{nullptr}); }
|
||||||
|
|
||||||
void setHasNewLayout(bool hasNewLayout) {
|
void setHasNewLayout(bool hasNewLayout) {
|
||||||
facebook::yoga::detail::setBooleanData(flags, hasNewLayout_, hasNewLayout);
|
flags_.hasNewLayout = hasNewLayout;
|
||||||
}
|
}
|
||||||
|
|
||||||
void setNodeType(YGNodeType nodeType) {
|
void setNodeType(YGNodeType nodeType) {
|
||||||
return facebook::yoga::detail::setEnumData<YGNodeType>(
|
flags_.nodeType = static_cast<uint8_t>(nodeType);
|
||||||
flags, nodeType_, nodeType);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void setMeasureFunc(YGMeasureFunc measureFunc);
|
void setMeasureFunc(YGMeasureFunc measureFunc);
|
||||||
@@ -276,11 +275,11 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
void setBaselineFunc(YGBaselineFunc baseLineFunc) {
|
void setBaselineFunc(YGBaselineFunc baseLineFunc) {
|
||||||
facebook::yoga::detail::setBooleanData(flags, baselineUsesContext_, false);
|
flags_.baselineUsesContext = false;
|
||||||
baseline_.noContext = baseLineFunc;
|
baseline_.noContext = baseLineFunc;
|
||||||
}
|
}
|
||||||
void setBaselineFunc(BaselineWithContextFn baseLineFunc) {
|
void setBaselineFunc(BaselineWithContextFn baseLineFunc) {
|
||||||
facebook::yoga::detail::setBooleanData(flags, baselineUsesContext_, true);
|
flags_.baselineUsesContext = true;
|
||||||
baseline_.withContext = baseLineFunc;
|
baseline_.withContext = baseLineFunc;
|
||||||
}
|
}
|
||||||
void setBaselineFunc(std::nullptr_t) {
|
void setBaselineFunc(std::nullptr_t) {
|
||||||
@@ -296,8 +295,7 @@ public:
|
|||||||
void setLineIndex(uint32_t lineIndex) { lineIndex_ = lineIndex; }
|
void setLineIndex(uint32_t lineIndex) { lineIndex_ = lineIndex; }
|
||||||
|
|
||||||
void setIsReferenceBaseline(bool isReferenceBaseline) {
|
void setIsReferenceBaseline(bool isReferenceBaseline) {
|
||||||
facebook::yoga::detail::setBooleanData(
|
flags_.isReferenceBaseline = isReferenceBaseline;
|
||||||
flags, isReferenceBaseline_, isReferenceBaseline);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void setOwner(YGNodeRef owner) { owner_ = owner; }
|
void setOwner(YGNodeRef owner) { owner_ = owner; }
|
||||||
|
Reference in New Issue
Block a user