diff --git a/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/android/react/renderer/textlayoutmanager/TextLayoutManager.h b/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/android/react/renderer/textlayoutmanager/TextLayoutManager.h index fd7b1e2f9fd7..dc2cfcd869dd 100644 --- a/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/android/react/renderer/textlayoutmanager/TextLayoutManager.h +++ b/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/android/react/renderer/textlayoutmanager/TextLayoutManager.h @@ -32,7 +32,31 @@ class TextLayoutManager; */ class TextLayoutManager { public: - using PreparedTextLayout = SafeReleaseJniRef>; + /* + * A prepared layout's Java object. Copies share one JNI global reference, so + * returning a cached layout or storing it in state doesn't create another. + */ + class PreparedTextLayout { + public: + PreparedTextLayout() = default; + explicit PreparedTextLayout(jni::global_ref ref) + : ref_(std::make_shared>>(std::move(ref))) + { + } + + JPreparedLayout::javaobject get() const + { + return ref_ ? ref_->get() : nullptr; + } + + explicit operator bool() const noexcept + { + return ref_ && *ref_; + } + + private: + std::shared_ptr>> ref_; + }; TextLayoutManager(const std::shared_ptr &contextContainer); diff --git a/scripts/cxx-api/api-snapshots/ReactAndroidFrameworksCxx.api b/scripts/cxx-api/api-snapshots/ReactAndroidFrameworksCxx.api index 5e5dad5f5ce5..16c6a2d49520 100644 --- a/scripts/cxx-api/api-snapshots/ReactAndroidFrameworksCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactAndroidFrameworksCxx.api @@ -4012,7 +4012,13 @@ class facebook::react::TextLayoutManager { public facebook::react::TextMeasurement measure(const facebook::react::AttributedStringBox& attributedStringBox, const facebook::react::ParagraphAttributes& paragraphAttributes, const facebook::react::TextLayoutContext& layoutContext, const facebook::react::LayoutConstraints& layoutConstraints) const; public facebook::react::TextMeasurement measureCachedSpannableById(int64_t cacheId, const facebook::react::ParagraphAttributes& paragraphAttributes, const facebook::react::TextLayoutContext& layoutContext, const facebook::react::LayoutConstraints& layoutConstraints) const; public facebook::react::TextMeasurement measurePreparedLayout(const facebook::react::TextLayoutManager::PreparedTextLayout& layout, const facebook::react::TextLayoutContext& layoutContext, const facebook::react::LayoutConstraints& layoutConstraints) const; - public using PreparedTextLayout = facebook::react::SafeReleaseJniRef>; +} + +class facebook::react::TextLayoutManager::PreparedTextLayout { + public JPreparedLayout::javaobject get() const; + public PreparedTextLayout() = default; + public PreparedTextLayout(jni::global_ref ref); + public operator bool() const noexcept; } class facebook::react::TextMeasureCacheKey {