-
Notifications
You must be signed in to change notification settings - Fork 28.6k
Enable dart:ui APIs for text measurement in secondary isolates #30604
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Can you provide the code that causes this error? |
void test(String str){
TextPainter(textDirection: TextDirection.ltr, text: TextSpan(text: str)).layout();
}
compute(test, '1'); |
The Flutter engine native APIs for the UI package are only available in the primary isolate. See https://github.com/flutter/engine/blob/master/lib/ui/dart_ui.cc#L102 |
I'm not sure we could even make sense of doing this async. If layout isn't finished, we can't paint anyway. If we block until layout is finished, we can't paint until layout is finished anyway, and we potentially introduce deadlocks. That said, it seems like it would be nice to be able to pre-layout text - or, alternatively, let the user paint text without specifying layout, which I think should still be possible today. @windrunner414 - what happens if you run your Something like: class StringAndOffset {
const StringAndOffset(this.string, this.offset) : assert(string != null), assert(offset != null);
final String string;
final Offset offset;
}
List<StringAndOffset> test(String str) {
// Break up `str` into strings and offsets, and return them.
}
final List<StringAndOffset> stringsAndOffsets = await compute(test, '1');
// iterate stringsAndOffsets to create TextPainters that should layout much more quickly now on the UI thread If that works out, it may be worth encapsulating somehow into an API, but hopefully if nothing else gets you unblocked. |
@dnfield how to get character's size to layout?we can't use TextPainter |
I find it interesting that textpainter seems to cache the size of each character. Caching is ineffective if you use compute |
I layout 5000 characters, including 2500 different characters.The first time took 1500ms!After that it only takes 15ms
i don't think it's a good way and I don't know what text is on every page. |
same question,so if we need to perform an expensive computation use textPainter,how to do it? |
right now you would have to write your own line-breaking logic - perhaps as a plugin. We don't currently have any API for doing text layout in a separate isolate. @GaryQian might have some other idea about this too. |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This is very useful for building map markers too, I mean, not only for building text but for map markers. In my app we have a lot of markers and building them every time is causing some UI freezes, but we use Exception: UI actions are only available on root isolate.
#0 PictureRecorder._constructor (dart:ui/painting.dart:4458:59)
#1 new PictureRecorder (dart:ui/painting.dart:4457:23)
#2 IconGenerator._drawCircle (package:client/src/common/utils/map/icon_generator.dart:87:32)
#3 IconGenerator.buildCircle (package:client/src/common/utils/map/icon_generator.dart:51:26)
#4 MarkerGenerator._buildDots.<anonymous closure> (package:client/src/common/utils/map/marker_generator.dart:62:37)
#5 MappedListIterable.elementAt (dart:_internal/iterable.dart:417:31)
#6 ListIterator.moveNext (dart:_internal/iterable.dart:343:26)
#7 Future.wait (dart:async/future.dart:406:26)
#8 MarkerGenerator._buildDots (package:client/src/common/utils/map/marker_generator.dart:68:19)
#9 MarkerGenerator.build (package:client/src/common/utils/map/marker_generator.dart:30:22)
#10 buildMarkers (package:client/src/widget |
That's a different problem and would be a bit more complicated to manage due to how these operations can access graphics contexts that can only be safely used on specific threads. Text layout on the other hand shoul dbe possible to do on any thread. |
😢 Got it, thanks for answering |
Can anyone update about the status of this issue ? I didn't find any update since long. |
Thumbs up, I can alse see performance troubles (half a second freezes) when displaying not small (±20kb) strings as Rich text composed of a number of nested text spans. 70% of CPU time spent on layout |
/cc @goderbauer fyi |
Can anyone update about the status of this issue ? this issue is open before 3 years. |
+1 |
Another option for this would be to expose a lower-level text drawing API and just let people do layout/shaping via whatever aPI they in some other isolate/plugin and then send the glyph IDs/positions back to the UI isolate for drawing. |
Sounds like a great idea, like android StaticLayout. |
FYI - There are a couple areas of overlap between the problem described here, and some of the issues I described in #115257 |
if use TextPainter.layout in isolate will get a compiler error
native function not found
I need layout many words and it takes a long time (100ms+), it will block UI thread
What should I do?
The text was updated successfully, but these errors were encountered: