Something went wrong. Try again.
Mirror — see github.com/blacksky-algorithms/blacksky.community
Something went wrong. Try again.
16 kB · 334 lines
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335diff --git a/ReactCommon/react/renderer/components/text/ParagraphShadowNode.cpp b/ReactCommon/react/renderer/components/text/ParagraphShadowNode.cppindex 17b14c4..9c46cd9 100644--- a/ReactCommon/react/renderer/components/text/ParagraphShadowNode.cpp+++ b/ReactCommon/react/renderer/components/text/ParagraphShadowNode.cpp@@ -315,6 +315,12 @@ void ParagraphShadowNode::layout(LayoutContext layoutContext) { auto measuredLayout = findUsableLayout(); +#ifdef __APPLE__+ // [Bluesky] The text view redraws when its attributed string changes.+ const bool attributedStringChanged =+ !(getStateData().attributedString == content.attributedString);+#endif+ if constexpr ( TextLayoutManagerExtended::supportsPreparedTextLayout() && std::is_constructible_v<@@ -342,6 +348,18 @@ void ParagraphShadowNode::layout(LayoutContext layoutContext) { updateStateIfNeeded(content); } +#ifdef __APPLE__+ // [Bluesky] Lay the text out now, off the main thread, for the content frame+ // the text view will draw into. Otherwise its first draw does it on the main+ // thread.+ if (attributedStringChanged && !content.attributedString.isEmpty()) {+ textLayoutManager_->prepareForDrawing(+ content.attributedString,+ content.paragraphAttributes,+ layoutMetrics.getContentFrame().size);+ }+#endif+ TextLayoutContext textLayoutContext{ .pointScaleFactor = layoutContext.pointScaleFactor, .surfaceId = getSurfaceId(),diff --git a/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/RCTTextLayoutManager.h b/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/RCTTextLayoutManager.hindex a4b9be7..a6e2fe7 100644--- a/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/RCTTextLayoutManager.h+++ b/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/RCTTextLayoutManager.h@@ -37,6 +37,15 @@ using RCTTextLayoutFragmentEnumerationBlock = layoutContext:(facebook::react::TextLayoutContext)layoutContext layoutConstraints:(facebook::react::LayoutConstraints)layoutConstraints; +/**+ * [Bluesky] Lays out `attributedString` for a text view with a content frame+ * of `size`, so that drawAttributedString: at that size doesn't lay it out+ * again on the main thread. Can be called from any thread.+ */+- (void)prepareAttributedStringForDrawing:(facebook::react::AttributedString)attributedString+ paragraphAttributes:(facebook::react::ParagraphAttributes)paragraphAttributes+ size:(CGSize)size;+ - (void)drawAttributedString:(facebook::react::AttributedString)attributedString paragraphAttributes:(facebook::react::ParagraphAttributes)paragraphAttributes frame:(CGRect)framediff --git a/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/RCTTextLayoutManager.mm b/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/RCTTextLayoutManager.mmindex 5f9847a..d10506f 100644--- a/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/RCTTextLayoutManager.mm+++ b/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/RCTTextLayoutManager.mm@@ -17,11 +17,133 @@ #import <react/renderer/textlayoutmanager/TextMeasurementRounding.h> #import <react/utils/ManagedObjectWrapper.h> #import <react/utils/SimpleThreadSafeCache.h>+#import <react/utils/hash_combine.h>++#include <deque>+#include <functional>+#include <mutex>+#include <unordered_map>+#include <vector> using namespace facebook::react; +/*+ * [Bluesky] Text layouts made off the main thread, so that a text view's first+ * draw doesn't lay its text out again on the main thread.+ *+ * - Measuring keeps its laid-out text storage ("measured layouts").+ * - After layout, ParagraphShadowNode asks for the text of every text view+ * whose content changed to be prepared at its final content frame size+ * ("prepared layouts"). That reuses a measured layout when it lays out the+ * same way, or lays the text out at exactly the frame size, as the draw+ * would.+ * - The draw takes the prepared layout for its exact size and otherwise lays+ * the text out itself, as React Native does by default.+ */+namespace {++struct RCTTextLayoutKey {+ AttributedString attributedString;+ ParagraphAttributes paragraphAttributes;++ bool operator==(const RCTTextLayoutKey &rhs) const+ {+ return attributedString == rhs.attributedString && paragraphAttributes == rhs.paragraphAttributes;+ }+};++struct RCTTextLayoutKeyHash {+ size_t operator()(const RCTTextLayoutKey &key) const+ {+ return hash_combine(key.attributedString, key.paragraphAttributes);+ }+};++struct RCTTextLayout {+ NSTextStorage *textStorage;+ // Size of the frame the text was laid out for. For measured layouts, the+ // width of the text container and the measured height.+ CGSize size;+};++/*+ * Text layouts waiting to be taken, a few per key. Each layout is handed out+ * once, so only one thread uses a text storage at a time. Keys that are never+ * taken (text that is never drawn, or drawn at another size) are evicted+ * oldest-first.+ */+template <size_t maxKeys_, size_t maxPerKey_>+class RCTTextLayoutPool {+ public:+ void add(const RCTTextLayoutKey &key, RCTTextLayout layout)+ {+ std::lock_guard<std::mutex> lock(mutex_);+ auto &layouts = layouts_[key];+ if (layouts.empty()) {+ order_.push_back(key);+ }+ if (layouts.size() >= maxPerKey_) {+ layouts.erase(layouts.begin());+ }+ layouts.push_back(std::move(layout));++ while (order_.size() > maxKeys_) {+ // A taken key stays in order_, so this can drop a newer layout for the+ // same text. That text then just takes the default path.+ layouts_.erase(order_.front());+ order_.pop_front();+ }+ }++ /*+ * Removes and returns the newest layout for `key` that satisfies `matches`.+ */+ NSTextStorage *_Nullable take(const RCTTextLayoutKey &key, const std::function<bool(CGSize)> &matches)+ {+ std::lock_guard<std::mutex> lock(mutex_);+ auto it = layouts_.find(key);+ if (it == layouts_.end()) {+ return nil;+ }++ auto &layouts = it->second;+ for (auto layout = layouts.rbegin(); layout != layouts.rend(); ++layout) {+ if (matches(layout->size)) {+ NSTextStorage *textStorage = layout->textStorage;+ layouts.erase(std::next(layout).base());+ if (layouts.empty()) {+ layouts_.erase(it);+ }+ return textStorage;+ }+ }++ return nil;+ }++ private:+ std::mutex mutex_;+ std::unordered_map<RCTTextLayoutKey, std::vector<RCTTextLayout>, RCTTextLayoutKeyHash> layouts_;+ std::deque<RCTTextLayoutKey> order_;+};++// Yoga rounds frames to the pixel grid, so a frame can differ from the+// measured size by a fraction of a point.+constexpr CGFloat kRCTMeasuredTextLayoutTolerance = 0.5;+// Prepared layouts are made for the same frame size the draw uses.+constexpr CGFloat kRCTPreparedTextLayoutTolerance = 0.01;++} // namespace+ @implementation RCTTextLayoutManager { SimpleThreadSafeCache<AttributedString, std::shared_ptr<void>, 256> _cache;+ /*+ * [Bluesky] See RCTTextLayoutPool. Yoga measures every text in a commit+ * before any of them is prepared, and a screen can have a few hundred. Texts+ * are often measured more than once while Yoga resolves flex sizes.+ */+ RCTTextLayoutPool<512, 3> _measuredLayouts;+ RCTTextLayoutPool<512, 2> _preparedLayouts; } static NSLineBreakMode RCTNSLineBreakModeFromEllipsizeMode(EllipsizeMode ellipsizeMode)@@ -63,10 +185,67 @@ - (TextMeasurement)measureAttributedString:(AttributedString)attributedString layoutContext:(TextLayoutContext)layoutContext layoutConstraints:(LayoutConstraints)layoutConstraints {- return [self measureNSAttributedString:[self _nsAttributedStringFromAttributedString:attributedString]- paragraphAttributes:paragraphAttributes- layoutContext:layoutContext- layoutConstraints:layoutConstraints];+ NSAttributedString *nsAttributedString = [self _nsAttributedStringFromAttributedString:attributedString];+ if (nsAttributedString.length == 0) {+ // See measureNSAttributedString:.+ return {};+ }++ CGSize maximumSize = CGSize{layoutConstraints.maximumSize.width, CGFLOAT_MAX};+ NSTextStorage *textStorage = [self _textStorageAndLayoutManagerWithAttributesString:nsAttributedString+ paragraphAttributes:paragraphAttributes+ size:maximumSize];+ TextMeasurement measurement = [self _measureTextStorage:textStorage+ paragraphAttributes:paragraphAttributes+ layoutContext:layoutContext];++ // [Bluesky] Font scaling depends on the final frame size, so those layouts+ // can't be reused.+ if (!paragraphAttributes.adjustsFontSizeToFit && isfinite(maximumSize.width)) {+ _measuredLayouts.add(+ RCTTextLayoutKey{.attributedString = attributedString, .paragraphAttributes = paragraphAttributes},+ RCTTextLayout{.textStorage = textStorage, .size = CGSize{maximumSize.width, measurement.size.height}});+ }++ return measurement;+}++- (void)prepareAttributedStringForDrawing:(AttributedString)attributedString+ paragraphAttributes:(ParagraphAttributes)paragraphAttributes+ size:(CGSize)size+{+ if (size.width <= 0 || size.height <= 0) {+ return;+ }++ RCTTextLayoutKey key{.attributedString = attributedString, .paragraphAttributes = paragraphAttributes};++ /*+ * Measuring lays text out at the full available width with unlimited+ * height. That gives the same result as a layout at `size` when the width+ * is the same and the frame is tall enough for every measured line.+ */+ NSTextStorage *textStorage = paragraphAttributes.adjustsFontSizeToFit+ ? nil+ : _measuredLayouts.take(key, [&](CGSize measured) {+ return fabs(measured.width - size.width) <= kRCTMeasuredTextLayoutTolerance &&+ size.height >= measured.height - kRCTMeasuredTextLayoutTolerance;+ });++ if (textStorage == nil) {+ NSAttributedString *nsAttributedString = [self _nsAttributedStringFromAttributedString:attributedString];+ if (nsAttributedString.length == 0) {+ return;+ }+ // Exactly what drawAttributedString: would make for this size.+ textStorage = [self _textStorageAndLayoutManagerWithAttributesString:nsAttributedString+ paragraphAttributes:paragraphAttributes+ size:size];+ NSLayoutManager *layoutManager = textStorage.layoutManagers.firstObject;+ [layoutManager ensureLayoutForTextContainer:layoutManager.textContainers.firstObject];+ }++ _preparedLayouts.add(key, RCTTextLayout{.textStorage = textStorage, .size = size}); } - (CGRect)drawingFrameForAttributedString:(AttributedString)attributedString@@ -132,10 +311,20 @@ - (void)drawAttributedString:(AttributedString)attributedString frame:(CGRect)frame drawHighlightPath:(void (^_Nullable)(UIBezierPath *highlightPath))block {- NSTextStorage *textStorage = [self- _textStorageAndLayoutManagerWithAttributesString:[self _nsAttributedStringFromAttributedString:attributedString]- paragraphAttributes:paragraphAttributes- size:frame.size];+ // [Bluesky] Use the layout prepared off the main thread for this size, if+ // any. Otherwise lay the text out here, as React Native does by default.+ NSTextStorage *textStorage = _preparedLayouts.take(+ RCTTextLayoutKey{.attributedString = attributedString, .paragraphAttributes = paragraphAttributes},+ [&](CGSize prepared) {+ return fabs(prepared.width - frame.size.width) <= kRCTPreparedTextLayoutTolerance &&+ fabs(prepared.height - frame.size.height) <= kRCTPreparedTextLayoutTolerance;+ });+ if (textStorage == nil) {+ textStorage = [self+ _textStorageAndLayoutManagerWithAttributesString:[self _nsAttributedStringFromAttributedString:attributedString]+ paragraphAttributes:paragraphAttributes+ size:frame.size];+ } NSLayoutManager *layoutManager = textStorage.layoutManagers.firstObject; NSTextContainer *textContainer = layoutManager.textContainers.firstObject; diff --git a/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/TextLayoutManager.h b/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/TextLayoutManager.hindex ce564b0..c842534 100644--- a/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/TextLayoutManager.h+++ b/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/TextLayoutManager.h@@ -61,6 +61,16 @@ class TextLayoutManager { */ std::shared_ptr<void> getNativeTextLayoutManager() const; + /*+ * [Bluesky] Lays out `attributedString` ahead of time for a text view whose+ * content frame has `size`, so its first draw on the main thread can reuse+ * it.+ */+ void prepareForDrawing(+ const AttributedString &attributedString,+ const ParagraphAttributes ¶graphAttributes,+ const Size &size) const;+ protected: std::shared_ptr<const ContextContainer> contextContainer_; std::shared_ptr<void> nativeTextLayoutManager_;diff --git a/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/TextLayoutManager.mm b/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/TextLayoutManager.mmindex 82cad8f..4f435b5 100644--- a/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/TextLayoutManager.mm+++ b/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/TextLayoutManager.mm@@ -100,6 +100,17 @@ return measurement; } +void TextLayoutManager::prepareForDrawing(+ const AttributedString &attributedString,+ const ParagraphAttributes ¶graphAttributes,+ const Size &size) const+{+ RCTTextLayoutManager *textLayoutManager = (RCTTextLayoutManager *)unwrapManagedObject(nativeTextLayoutManager_);+ [textLayoutManager prepareAttributedStringForDrawing:attributedString+ paragraphAttributes:paragraphAttributes+ size:CGSizeMake(size.width, size.height)];+}+ LinesMeasurements TextLayoutManager::measureLines( const AttributedStringBox &attributedStringBox, const ParagraphAttributes ¶graphAttributes,