Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ class TextMeasureCacheKey final {
AttributedString attributedString{};
ParagraphAttributes paragraphAttributes{};
LayoutConstraints layoutConstraints{};
// The measured size depends on the pixel scale factor because layout metrics
// are rounded to the pixel grid. Two otherwise-identical measures at different
// densities are not interchangeable, so the scale factor is part of the key.
Float pointScaleFactor{};
};

// The Key type that is used for Line Measure Cache.
Expand All @@ -87,6 +91,9 @@ class PreparedTextCacheKey final {
AttributedString attributedString{};
ParagraphAttributes paragraphAttributes{};
LayoutConstraints layoutConstraints{};
// A prepared layout is rounded to the pixel grid, so it is only reusable at
// the pixel scale factor it was laid out at.
Float pointScaleFactor{};
};

/*
Expand Down Expand Up @@ -252,7 +259,8 @@ inline size_t attributedStringHashDisplayWise(const AttributedString &attributed
inline bool operator==(const TextMeasureCacheKey &lhs, const TextMeasureCacheKey &rhs)
{
return areAttributedStringsEquivalentLayoutWise(lhs.attributedString, rhs.attributedString) &&
lhs.paragraphAttributes == rhs.paragraphAttributes && lhs.layoutConstraints == rhs.layoutConstraints;
lhs.paragraphAttributes == rhs.paragraphAttributes && lhs.layoutConstraints == rhs.layoutConstraints &&
floatEquality(lhs.pointScaleFactor, rhs.pointScaleFactor);
}

inline bool operator==(const LineMeasureCacheKey &lhs, const LineMeasureCacheKey &rhs)
Expand All @@ -264,7 +272,8 @@ inline bool operator==(const LineMeasureCacheKey &lhs, const LineMeasureCacheKey
inline bool operator==(const PreparedTextCacheKey &lhs, const PreparedTextCacheKey &rhs)
{
return areAttributedStringsEquivalentDisplayWise(lhs.attributedString, rhs.attributedString) &&
lhs.paragraphAttributes == rhs.paragraphAttributes && lhs.layoutConstraints == rhs.layoutConstraints;
lhs.paragraphAttributes == rhs.paragraphAttributes && lhs.layoutConstraints == rhs.layoutConstraints &&
floatEquality(lhs.pointScaleFactor, rhs.pointScaleFactor);
}

} // namespace facebook::react
Expand All @@ -276,7 +285,10 @@ struct hash<facebook::react::TextMeasureCacheKey> {
size_t operator()(const facebook::react::TextMeasureCacheKey &key) const
{
return facebook::react::hash_combine(
attributedStringHashLayoutWise(key.attributedString), key.paragraphAttributes, key.layoutConstraints);
attributedStringHashLayoutWise(key.attributedString),
key.paragraphAttributes,
key.layoutConstraints,
key.pointScaleFactor);
}
};

Expand All @@ -294,7 +306,10 @@ struct hash<facebook::react::PreparedTextCacheKey> {
size_t operator()(const facebook::react::PreparedTextCacheKey &key) const
{
return facebook::react::hash_combine(
attributedStringHashDisplayWise(key.attributedString), key.paragraphAttributes, key.layoutConstraints);
attributedStringHashDisplayWise(key.attributedString),
key.paragraphAttributes,
key.layoutConstraints,
key.pointScaleFactor);
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,8 @@ TextMeasurement TextLayoutManager::measure(
: textMeasureCache_.get(
{.attributedString = attributedString,
.paragraphAttributes = paragraphAttributes,
.layoutConstraints = layoutConstraints},
.layoutConstraints = layoutConstraints,
.pointScaleFactor = layoutContext.pointScaleFactor},
std::move(measureText));

measurement.size = layoutConstraints.clamp(measurement.size);
Expand Down Expand Up @@ -315,7 +316,8 @@ TextLayoutManager::PreparedTextLayout TextLayoutManager::prepareLayout(
const auto [key, preparedText] = preparedTextCache_.getWithKey(
{.attributedString = attributedString,
.paragraphAttributes = paragraphAttributes,
.layoutConstraints = layoutConstraints},
.layoutConstraints = layoutConstraints,
.pointScaleFactor = layoutContext.pointScaleFactor},
[&]() {
const auto& fabricUIManager =
contextContainer_->at<jni::global_ref<jobject>>("FabricUIManager");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@
measurement = textMeasureCache_.get(
{.attributedString = attributedString,
.paragraphAttributes = paragraphAttributes,
.layoutConstraints = layoutConstraints},
.layoutConstraints = layoutConstraints,
.pointScaleFactor = layoutContext.pointScaleFactor},
[&]() {
auto telemetry = TransactionTelemetry::threadLocalTelemetry();
if (telemetry) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,56 @@ TEST(TextLayoutManagerTest, maxFontSizeMultiplierAffectsLayoutCacheHash) {
EXPECT_NE(
textAttributesHashLayoutWise(lhs), textAttributesHashLayoutWise(rhs));
}

// Measurements are rounded to the pixel grid, so a measurement cached at one
// pixel scale factor must not satisfy a lookup at another. Keys that differ
// only by pointScaleFactor must compare unequal.
TEST(TextLayoutManagerTest, pointScaleFactorAffectsTextMeasureCacheEquality) {
TextMeasureCacheKey lhs;
TextMeasureCacheKey rhs;

lhs.pointScaleFactor = 2.0;
rhs.pointScaleFactor = 1.6;
EXPECT_FALSE(lhs == rhs);

rhs.pointScaleFactor = 2.0;
EXPECT_TRUE(lhs == rhs);
}

TEST(TextLayoutManagerTest, pointScaleFactorAffectsTextMeasureCacheHash) {
TextMeasureCacheKey lhs;
TextMeasureCacheKey rhs;

lhs.pointScaleFactor = 2.0;
rhs.pointScaleFactor = 1.6;

EXPECT_NE(
std::hash<TextMeasureCacheKey>{}(lhs),
std::hash<TextMeasureCacheKey>{}(rhs));
}

// Same invariant for the prepared-text cache: a prepared layout is pixel-grid
// rounded and is only reusable at the pixel scale factor it was prepared at.
TEST(TextLayoutManagerTest, pointScaleFactorAffectsPreparedTextCacheEquality) {
PreparedTextCacheKey lhs;
PreparedTextCacheKey rhs;

lhs.pointScaleFactor = 2.0;
rhs.pointScaleFactor = 1.6;
EXPECT_FALSE(lhs == rhs);

rhs.pointScaleFactor = 2.0;
EXPECT_TRUE(lhs == rhs);
}

TEST(TextLayoutManagerTest, pointScaleFactorAffectsPreparedTextCacheHash) {
PreparedTextCacheKey lhs;
PreparedTextCacheKey rhs;

lhs.pointScaleFactor = 2.0;
rhs.pointScaleFactor = 1.6;

EXPECT_NE(
std::hash<PreparedTextCacheKey>{}(lhs),
std::hash<PreparedTextCacheKey>{}(rhs));
}
2 changes: 2 additions & 0 deletions scripts/cxx-api/api-snapshots/ReactAndroidDebugCxx.api
Original file line number Diff line number Diff line change
Expand Up @@ -4129,6 +4129,7 @@ class facebook::react::PointerHoverTracker {

class facebook::react::PreparedTextCacheKey {
public facebook::react::AttributedString attributedString;
public facebook::react::Float pointScaleFactor;
public facebook::react::LayoutConstraints layoutConstraints;
public facebook::react::ParagraphAttributes paragraphAttributes;
}
Expand Down Expand Up @@ -5064,6 +5065,7 @@ class facebook::react::TextLayoutManager {

class facebook::react::TextMeasureCacheKey {
public facebook::react::AttributedString attributedString;
public facebook::react::Float pointScaleFactor;
public facebook::react::LayoutConstraints layoutConstraints;
public facebook::react::ParagraphAttributes paragraphAttributes;
}
Expand Down
2 changes: 2 additions & 0 deletions scripts/cxx-api/api-snapshots/ReactAndroidNewarchCxx.api
Original file line number Diff line number Diff line change
Expand Up @@ -3973,6 +3973,7 @@ class facebook::react::PointerHoverTracker {

class facebook::react::PreparedTextCacheKey {
public facebook::react::AttributedString attributedString;
public facebook::react::Float pointScaleFactor;
public facebook::react::LayoutConstraints layoutConstraints;
public facebook::react::ParagraphAttributes paragraphAttributes;
}
Expand Down Expand Up @@ -4890,6 +4891,7 @@ class facebook::react::TextLayoutManager {

class facebook::react::TextMeasureCacheKey {
public facebook::react::AttributedString attributedString;
public facebook::react::Float pointScaleFactor;
public facebook::react::LayoutConstraints layoutConstraints;
public facebook::react::ParagraphAttributes paragraphAttributes;
}
Expand Down
2 changes: 2 additions & 0 deletions scripts/cxx-api/api-snapshots/ReactAndroidReleaseCxx.api
Original file line number Diff line number Diff line change
Expand Up @@ -4126,6 +4126,7 @@ class facebook::react::PointerHoverTracker {

class facebook::react::PreparedTextCacheKey {
public facebook::react::AttributedString attributedString;
public facebook::react::Float pointScaleFactor;
public facebook::react::LayoutConstraints layoutConstraints;
public facebook::react::ParagraphAttributes paragraphAttributes;
}
Expand Down Expand Up @@ -5055,6 +5056,7 @@ class facebook::react::TextLayoutManager {

class facebook::react::TextMeasureCacheKey {
public facebook::react::AttributedString attributedString;
public facebook::react::Float pointScaleFactor;
public facebook::react::LayoutConstraints layoutConstraints;
public facebook::react::ParagraphAttributes paragraphAttributes;
}
Expand Down
2 changes: 2 additions & 0 deletions scripts/cxx-api/api-snapshots/ReactAppleDebugCxx.api
Original file line number Diff line number Diff line change
Expand Up @@ -6316,6 +6316,7 @@ class facebook::react::PointerHoverTracker {

class facebook::react::PreparedTextCacheKey {
public facebook::react::AttributedString attributedString;
public facebook::react::Float pointScaleFactor;
public facebook::react::LayoutConstraints layoutConstraints;
public facebook::react::ParagraphAttributes paragraphAttributes;
}
Expand Down Expand Up @@ -7283,6 +7284,7 @@ class facebook::react::TextLayoutManager {

class facebook::react::TextMeasureCacheKey {
public facebook::react::AttributedString attributedString;
public facebook::react::Float pointScaleFactor;
public facebook::react::LayoutConstraints layoutConstraints;
public facebook::react::ParagraphAttributes paragraphAttributes;
}
Expand Down
2 changes: 2 additions & 0 deletions scripts/cxx-api/api-snapshots/ReactAppleNewarchCxx.api
Original file line number Diff line number Diff line change
Expand Up @@ -6188,6 +6188,7 @@ class facebook::react::PointerHoverTracker {

class facebook::react::PreparedTextCacheKey {
public facebook::react::AttributedString attributedString;
public facebook::react::Float pointScaleFactor;
public facebook::react::LayoutConstraints layoutConstraints;
public facebook::react::ParagraphAttributes paragraphAttributes;
}
Expand Down Expand Up @@ -7137,6 +7138,7 @@ class facebook::react::TextLayoutManager {

class facebook::react::TextMeasureCacheKey {
public facebook::react::AttributedString attributedString;
public facebook::react::Float pointScaleFactor;
public facebook::react::LayoutConstraints layoutConstraints;
public facebook::react::ParagraphAttributes paragraphAttributes;
}
Expand Down
2 changes: 2 additions & 0 deletions scripts/cxx-api/api-snapshots/ReactAppleReleaseCxx.api
Original file line number Diff line number Diff line change
Expand Up @@ -6313,6 +6313,7 @@ class facebook::react::PointerHoverTracker {

class facebook::react::PreparedTextCacheKey {
public facebook::react::AttributedString attributedString;
public facebook::react::Float pointScaleFactor;
public facebook::react::LayoutConstraints layoutConstraints;
public facebook::react::ParagraphAttributes paragraphAttributes;
}
Expand Down Expand Up @@ -7274,6 +7275,7 @@ class facebook::react::TextLayoutManager {

class facebook::react::TextMeasureCacheKey {
public facebook::react::AttributedString attributedString;
public facebook::react::Float pointScaleFactor;
public facebook::react::LayoutConstraints layoutConstraints;
public facebook::react::ParagraphAttributes paragraphAttributes;
}
Expand Down
2 changes: 2 additions & 0 deletions scripts/cxx-api/api-snapshots/ReactCommonDebugCxx.api
Original file line number Diff line number Diff line change
Expand Up @@ -2732,6 +2732,7 @@ class facebook::react::PointerHoverTracker {
}

class facebook::react::PreparedTextCacheKey {
public Float pointScaleFactor;
public facebook::react::AttributedString attributedString;
public facebook::react::LayoutConstraints layoutConstraints;
public facebook::react::ParagraphAttributes paragraphAttributes;
Expand Down Expand Up @@ -3554,6 +3555,7 @@ class facebook::react::TextInputState {
}

class facebook::react::TextMeasureCacheKey {
public Float pointScaleFactor;
public facebook::react::AttributedString attributedString;
public facebook::react::LayoutConstraints layoutConstraints;
public facebook::react::ParagraphAttributes paragraphAttributes;
Expand Down
2 changes: 2 additions & 0 deletions scripts/cxx-api/api-snapshots/ReactCommonNewarchCxx.api
Original file line number Diff line number Diff line change
Expand Up @@ -2616,6 +2616,7 @@ class facebook::react::PointerHoverTracker {
}

class facebook::react::PreparedTextCacheKey {
public Float pointScaleFactor;
public facebook::react::AttributedString attributedString;
public facebook::react::LayoutConstraints layoutConstraints;
public facebook::react::ParagraphAttributes paragraphAttributes;
Expand Down Expand Up @@ -3420,6 +3421,7 @@ class facebook::react::TextInputState {
}

class facebook::react::TextMeasureCacheKey {
public Float pointScaleFactor;
public facebook::react::AttributedString attributedString;
public facebook::react::LayoutConstraints layoutConstraints;
public facebook::react::ParagraphAttributes paragraphAttributes;
Expand Down
2 changes: 2 additions & 0 deletions scripts/cxx-api/api-snapshots/ReactCommonReleaseCxx.api
Original file line number Diff line number Diff line change
Expand Up @@ -2729,6 +2729,7 @@ class facebook::react::PointerHoverTracker {
}

class facebook::react::PreparedTextCacheKey {
public Float pointScaleFactor;
public facebook::react::AttributedString attributedString;
public facebook::react::LayoutConstraints layoutConstraints;
public facebook::react::ParagraphAttributes paragraphAttributes;
Expand Down Expand Up @@ -3545,6 +3546,7 @@ class facebook::react::TextInputState {
}

class facebook::react::TextMeasureCacheKey {
public Float pointScaleFactor;
public facebook::react::AttributedString attributedString;
public facebook::react::LayoutConstraints layoutConstraints;
public facebook::react::ParagraphAttributes paragraphAttributes;
Expand Down
Loading