Skip to content

Commit

Permalink
Use Bitfield in YGLayout
Browse files Browse the repository at this point in the history
Summary: Replaces the usage of C++ bitfields with our portable `Bitfield` class.

Reviewed By: SidharthGuglani

Differential Revision: D16656361

fbshipit-source-id: 05f679e2e994e109b2bd1090c879d6850fabdc40
  • Loading branch information
davidaurelio authored and facebook-github-bot committed Aug 7, 2019
1 parent 2c73dbd commit f94360a
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 27 deletions.
3 changes: 2 additions & 1 deletion ReactCommon/yoga/yoga/YGLayout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ bool YGLayout::operator==(YGLayout layout) const {
YGFloatArrayEqual(margin, layout.margin) &&
YGFloatArrayEqual(border, layout.border) &&
YGFloatArrayEqual(padding, layout.padding) &&
direction == layout.direction && hadOverflow == layout.hadOverflow &&
direction() == layout.direction() &&
hadOverflow() == layout.hadOverflow() &&
lastOwnerDirection == layout.lastOwnerDirection &&
nextCachedMeasurementsIndex == layout.nextCachedMeasurementsIndex &&
cachedLayout == layout.cachedLayout &&
Expand Down
41 changes: 32 additions & 9 deletions ReactCommon/yoga/yoga/YGLayout.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* file in the root directory of this source tree.
*/
#pragma once
#include "Bitfield.h"
#include "YGFloatOptional.h"
#include "Yoga-internal.h"

Expand All @@ -14,11 +15,16 @@ struct YGLayout {
std::array<float, 4> margin = {};
std::array<float, 4> border = {};
std::array<float, 4> padding = {};
YGDirection direction : 2;
bool didUseLegacyFlag : 1;
bool doesLegacyStretchFlagAffectsLayout : 1;
bool hadOverflow : 1;

private:
static constexpr size_t directionIdx = 0;
static constexpr size_t didUseLegacyFlagIdx = 1;
static constexpr size_t doesLegacyStretchFlagAffectsLayoutIdx = 2;
static constexpr size_t hadOverflowIdx = 3;
facebook::yoga::Bitfield<uint8_t, YGDirection, bool, bool, bool> flags_ =
{YGDirectionInherit, false, false, false};

public:
uint32_t computedFlexBasisGeneration = 0;
YGFloatOptional computedFlexBasis = {};

Expand All @@ -34,11 +40,28 @@ struct YGLayout {

YGCachedMeasurement cachedLayout = YGCachedMeasurement();

YGLayout()
: direction(YGDirectionInherit),
didUseLegacyFlag(false),
doesLegacyStretchFlagAffectsLayout(false),
hadOverflow(false) {}
YGDirection direction() const { return flags_.at<directionIdx>(); }
decltype(flags_)::Ref<directionIdx> direction() {
return flags_.at<directionIdx>();
}

bool didUseLegacyFlag() const { return flags_.at<didUseLegacyFlagIdx>(); }
decltype(flags_)::Ref<didUseLegacyFlagIdx> didUseLegacyFlag() {
return flags_.at<didUseLegacyFlagIdx>();
}

bool doesLegacyStretchFlagAffectsLayout() const {
return flags_.at<doesLegacyStretchFlagAffectsLayoutIdx>();
}
decltype(flags_)::Ref<doesLegacyStretchFlagAffectsLayoutIdx>
doesLegacyStretchFlagAffectsLayout() {
return flags_.at<doesLegacyStretchFlagAffectsLayoutIdx>();
}

bool hadOverflow() const { return flags_.at<hadOverflowIdx>(); }
decltype(flags_)::Ref<hadOverflowIdx> hadOverflow() {
return flags_.at<hadOverflowIdx>();
}

bool operator==(YGLayout layout) const;
bool operator!=(YGLayout layout) const { return !(*this == layout); }
Expand Down
12 changes: 6 additions & 6 deletions ReactCommon/yoga/yoga/YGNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ void YGNode::removeChild(uint32_t index) {
}

void YGNode::setLayoutDirection(YGDirection direction) {
layout_.direction = direction;
layout_.direction() = direction;
}

void YGNode::setLayoutMargin(float margin, int index) {
Expand Down Expand Up @@ -269,7 +269,7 @@ void YGNode::setLayoutMeasuredDimension(float measuredDimension, int index) {
}

void YGNode::setLayoutHadOverflow(bool hadOverflow) {
layout_.hadOverflow = hadOverflow;
layout_.hadOverflow() = hadOverflow;
}

void YGNode::setLayoutDimension(float dimension, int index) {
Expand Down Expand Up @@ -520,12 +520,12 @@ YGFloatOptional YGNode::getTrailingPaddingAndBorder(
}

bool YGNode::didUseLegacyFlag() {
bool didUseLegacyFlag = layout_.didUseLegacyFlag;
bool didUseLegacyFlag = layout_.didUseLegacyFlag();
if (didUseLegacyFlag) {
return true;
}
for (const auto& child : children_) {
if (child->layout_.didUseLegacyFlag) {
if (child->layout_.didUseLegacyFlag()) {
didUseLegacyFlag = true;
break;
}
Expand All @@ -535,11 +535,11 @@ bool YGNode::didUseLegacyFlag() {

void YGNode::setLayoutDoesLegacyFlagAffectsLayout(
bool doesLegacyFlagAffectsLayout) {
layout_.doesLegacyStretchFlagAffectsLayout = doesLegacyFlagAffectsLayout;
layout_.doesLegacyStretchFlagAffectsLayout() = doesLegacyFlagAffectsLayout;
}

void YGNode::setLayoutDidUseLegacyFlag(bool didUseLegacyFlag) {
layout_.didUseLegacyFlag = didUseLegacyFlag;
layout_.didUseLegacyFlag() = didUseLegacyFlag;
}

bool YGNode::isLayoutTreeEqualToNode(const YGNode& node) const {
Expand Down
22 changes: 11 additions & 11 deletions ReactCommon/yoga/yoga/Yoga.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -892,15 +892,15 @@ YGValue YGNodeStyleGetMaxHeight(const YGNodeConstRef node) {
"Cannot get layout properties of multi-edge shorthands"); \
\
if (edge == YGEdgeStart) { \
if (node->getLayout().direction == YGDirectionRTL) { \
if (node->getLayout().direction() == YGDirectionRTL) { \
return node->getLayout().instanceName[YGEdgeRight]; \
} else { \
return node->getLayout().instanceName[YGEdgeLeft]; \
} \
} \
\
if (edge == YGEdgeEnd) { \
if (node->getLayout().direction == YGDirectionRTL) { \
if (node->getLayout().direction() == YGDirectionRTL) { \
return node->getLayout().instanceName[YGEdgeLeft]; \
} else { \
return node->getLayout().instanceName[YGEdgeRight]; \
Expand All @@ -916,15 +916,15 @@ YG_NODE_LAYOUT_PROPERTY_IMPL(float, Right, position[YGEdgeRight]);
YG_NODE_LAYOUT_PROPERTY_IMPL(float, Bottom, position[YGEdgeBottom]);
YG_NODE_LAYOUT_PROPERTY_IMPL(float, Width, dimensions[YGDimensionWidth]);
YG_NODE_LAYOUT_PROPERTY_IMPL(float, Height, dimensions[YGDimensionHeight]);
YG_NODE_LAYOUT_PROPERTY_IMPL(YGDirection, Direction, direction);
YG_NODE_LAYOUT_PROPERTY_IMPL(bool, HadOverflow, hadOverflow);
YG_NODE_LAYOUT_PROPERTY_IMPL(YGDirection, Direction, direction());
YG_NODE_LAYOUT_PROPERTY_IMPL(bool, HadOverflow, hadOverflow());

YG_NODE_LAYOUT_RESOLVED_PROPERTY_IMPL(float, Margin, margin);
YG_NODE_LAYOUT_RESOLVED_PROPERTY_IMPL(float, Border, border);
YG_NODE_LAYOUT_RESOLVED_PROPERTY_IMPL(float, Padding, padding);

bool YGNodeLayoutGetDidLegacyStretchFlagAffectLayout(const YGNodeRef node) {
return node->getLayout().doesLegacyStretchFlagAffectsLayout;
return node->getLayout().doesLegacyStretchFlagAffectsLayout();
}

uint32_t gCurrentGenerationCount = 0;
Expand Down Expand Up @@ -2199,7 +2199,7 @@ static float YGDistributeFreeSpaceSecondPass(
currentRelativeChild,
childWidth,
childHeight,
node->getLayout().direction,
node->getLayout().direction(),
childWidthMeasureMode,
childHeightMeasureMode,
availableInnerWidth,
Expand All @@ -2213,8 +2213,8 @@ static float YGDistributeFreeSpaceSecondPass(
depth,
generationCount);
node->setLayoutHadOverflow(
node->getLayout().hadOverflow |
currentRelativeChild->getLayout().hadOverflow);
node->getLayout().hadOverflow() |
currentRelativeChild->getLayout().hadOverflow());
}
return deltaFreeSpace;
}
Expand Down Expand Up @@ -2973,7 +2973,7 @@ static void YGNodelayoutImpl(
}

node->setLayoutHadOverflow(
node->getLayout().hadOverflow |
node->getLayout().hadOverflow() |
(collectedFlexItemsValues.remainingFreeSpace < 0));

// STEP 6: MAIN-AXIS JUSTIFICATION & CROSS-AXIS SIZE DETERMINATION
Expand Down Expand Up @@ -4152,7 +4152,7 @@ void YGNodeCalculateLayoutWithContext(
0, // tree root
gCurrentGenerationCount)) {
node->setPosition(
node->getLayout().direction, ownerWidth, ownerHeight, ownerWidth);
node->getLayout().direction(), ownerWidth, ownerHeight, ownerWidth);
YGRoundToPixelGrid(node, node->getConfig()->pointScaleFactor, 0.0f, 0.0f);

#ifdef DEBUG
Expand Down Expand Up @@ -4202,7 +4202,7 @@ void YGNodeCalculateLayoutWithContext(
0, // tree root
gCurrentGenerationCount)) {
nodeWithoutLegacyFlag->setPosition(
nodeWithoutLegacyFlag->getLayout().direction,
nodeWithoutLegacyFlag->getLayout().direction(),
ownerWidth,
ownerHeight,
ownerWidth);
Expand Down

0 comments on commit f94360a

Please sign in to comment.