Skip to content

Commit b16c22a

Browse files
Emil SjolanderFacebook Github Bot
authored andcommitted
Only skip updating computed flex basis within the same generation
Summary: computed flex basis was incorrectly being reused in some circumstances between calls to caluclateLayout also run format script. Reviewed By: dshahidehpour Differential Revision: D4207106 fbshipit-source-id: fc1063ca79ecf75f6101aadded53bca96cb0585d
1 parent 56ec33a commit b16c22a

4 files changed

Lines changed: 50 additions & 13 deletions

File tree

CSSLayout/CSSLayout.c

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ typedef struct CSSLayout {
4949
float dimensions[2];
5050
CSSDirection direction;
5151

52+
uint32_t computedFlexBasisGeneration;
5253
float computedFlexBasis;
5354

5455
// Instead of recomputing the entire layout every single time, we
@@ -967,7 +968,8 @@ static void computeChildFlexBasis(const CSSNodeRef node,
967968

968969
if (!CSSValueIsUndefined(CSSNodeStyleGetFlexBasis(child)) &&
969970
!CSSValueIsUndefined(isMainAxisRow ? width : height)) {
970-
if (CSSValueIsUndefined(child->layout.computedFlexBasis)) {
971+
if (CSSValueIsUndefined(child->layout.computedFlexBasis) ||
972+
child->layout.computedFlexBasisGeneration != gCurrentGenerationCount) {
971973
child->layout.computedFlexBasis =
972974
fmaxf(CSSNodeStyleGetFlexBasis(child), getPaddingAndBorderAxis(child, mainAxis));
973975
}
@@ -1052,6 +1054,8 @@ static void computeChildFlexBasis(const CSSNodeRef node,
10521054
: child->layout.measuredDimensions[CSSDimensionHeight],
10531055
getPaddingAndBorderAxis(child, mainAxis));
10541056
}
1057+
1058+
child->layout.computedFlexBasisGeneration = gCurrentGenerationCount;
10551059
}
10561060

10571061
static void absoluteLayoutChild(const CSSNodeRef node,
@@ -1477,6 +1481,7 @@ static void layoutNodeImpl(const CSSNodeRef node,
14771481
child->nextChild = NULL;
14781482
} else {
14791483
if (child == singleFlexChild) {
1484+
child->layout.computedFlexBasisGeneration = gCurrentGenerationCount;
14801485
child->layout.computedFlexBasis = 0;
14811486
} else {
14821487
computeChildFlexBasis(node,
@@ -1813,8 +1818,8 @@ static void layoutNodeImpl(const CSSNodeRef node,
18131818
if (!CSSValueIsUndefined(node->style.minDimensions[dim[mainAxis]]) &&
18141819
node->style.minDimensions[dim[mainAxis]] >= 0) {
18151820
remainingFreeSpace = fmaxf(0,
1816-
node->style.minDimensions[dim[mainAxis]] -
1817-
(availableInnerMainDim - remainingFreeSpace));
1821+
node->style.minDimensions[dim[mainAxis]] -
1822+
(availableInnerMainDim - remainingFreeSpace));
18181823
} else {
18191824
remainingFreeSpace = 0;
18201825
}
@@ -2542,10 +2547,9 @@ void CSSLayoutSetMemoryFuncs(CSSMalloc cssMalloc,
25422547
CSSCalloc cssCalloc,
25432548
CSSRealloc cssRealloc,
25442549
CSSFree cssFree) {
2545-
CSS_ASSERT(gNodeInstanceCount == 0,
2546-
"Cannot set memory functions: all node must be freed first");
2550+
CSS_ASSERT(gNodeInstanceCount == 0, "Cannot set memory functions: all node must be freed first");
25472551
CSS_ASSERT((cssMalloc == NULL && cssCalloc == NULL && cssRealloc == NULL && cssFree == NULL) ||
2548-
(cssMalloc != NULL && cssCalloc != NULL && cssRealloc != NULL && cssFree != NULL),
2552+
(cssMalloc != NULL && cssCalloc != NULL && cssRealloc != NULL && cssFree != NULL),
25492553
"Cannot set memory functions: functions must be all NULL or Non-NULL");
25502554

25512555
if (cssMalloc == NULL || cssCalloc == NULL || cssRealloc == NULL || cssFree == NULL) {

CSSLayout/CSSLayout.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ static const unsigned long __nan[2] = {0xffffffff, 0x7fffffff};
2828

2929
#define CSSUndefined NAN
3030

31-
#include "CSSMacros.h"
3231
#include "CSSEnums.h"
32+
#include "CSSMacros.h"
3333

3434
CSS_EXTERN_C_BEGIN
3535

@@ -160,7 +160,8 @@ CSS_NODE_LAYOUT_PROPERTY(CSSDirection, Direction);
160160
WIN_EXPORT void CSSLayoutSetLogger(CSSLogger logger);
161161
WIN_EXPORT void CSSLog(CSSLogLevel level, const char *message, ...);
162162

163-
WIN_EXPORT void CSSLayoutSetExperimentalFeatureEnabled(CSSExperimentalFeature feature, bool enabled);
163+
WIN_EXPORT void CSSLayoutSetExperimentalFeatureEnabled(CSSExperimentalFeature feature,
164+
bool enabled);
164165
WIN_EXPORT bool CSSLayoutIsExperimentalFeatureEnabled(CSSExperimentalFeature feature);
165166

166167
WIN_EXPORT void CSSLayoutSetMemoryFuncs(CSSMalloc cssMalloc,

java/jni/CSSJNI.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,15 @@ static int _jniLog(CSSLogLevel level, const char *format, va_list args) {
7777
char buffer[256];
7878
int result = vsnprintf(buffer, sizeof(buffer), format, args);
7979

80-
static auto logFunc =
81-
findClassLocal("com/facebook/csslayout/CSSLogger")->getMethod<void(local_ref<JCSSLogLevel>, jstring)>("log");
80+
static auto logFunc = findClassLocal("com/facebook/csslayout/CSSLogger")
81+
->getMethod<void(local_ref<JCSSLogLevel>, jstring)>("log");
8282

83-
static auto logLevelFromInt = JCSSLogLevel::javaClassStatic()->getStaticMethod<JCSSLogLevel::javaobject(jint)>("fromInt");
83+
static auto logLevelFromInt =
84+
JCSSLogLevel::javaClassStatic()->getStaticMethod<JCSSLogLevel::javaobject(jint)>("fromInt");
8485

85-
logFunc(jLogger->get(), logLevelFromInt(JCSSLogLevel::javaClassStatic(), static_cast<jint>(level)), Environment::current()->NewStringUTF(buffer));
86+
logFunc(jLogger->get(),
87+
logLevelFromInt(JCSSLogLevel::javaClassStatic(), static_cast<jint>(level)),
88+
Environment::current()->NewStringUTF(buffer));
8689

8790
return result;
8891
}
@@ -112,7 +115,9 @@ void jni_CSSLog(alias_ref<jclass> clazz, jint level, jstring message) {
112115
Environment::current()->ReleaseStringUTFChars(message, nMessage);
113116
}
114117

115-
void jni_CSSLayoutSetExperimentalFeatureEnabled(alias_ref<jclass> clazz, jint feature, jboolean enabled) {
118+
void jni_CSSLayoutSetExperimentalFeatureEnabled(alias_ref<jclass> clazz,
119+
jint feature,
120+
jboolean enabled) {
116121
CSSLayoutSetExperimentalFeatureEnabled(static_cast<CSSExperimentalFeature>(feature), enabled);
117122
}
118123

tests/CSSLayoutRelayoutTest.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Copyright (c) 2014-present, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*/
9+
10+
#include <CSSLayout/CSSLayout.h>
11+
#include <gtest/gtest.h>
12+
13+
TEST(CSSLayoutTest, dont_cache_computed_flex_basis_between_layouts) {
14+
const CSSNodeRef root = CSSNodeNew();
15+
16+
const CSSNodeRef root_child0 = CSSNodeNew();
17+
CSSNodeStyleSetHeight(root_child0, 10);
18+
CSSNodeStyleSetFlexBasis(root_child0, 20);
19+
CSSNodeInsertChild(root, root_child0, 0);
20+
21+
CSSNodeCalculateLayout(root, 100, CSSUndefined, CSSDirectionLTR);
22+
CSSNodeCalculateLayout(root, 100, 100, CSSDirectionLTR);
23+
24+
ASSERT_EQ(20, CSSNodeLayoutGetHeight(root_child0));
25+
26+
CSSNodeFreeRecursive(root);
27+
}

0 commit comments

Comments
 (0)