Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Conversation

@yjbanov
Copy link
Contributor

@yjbanov yjbanov commented Jan 20, 2021

Fix text background/foreground and font family fallback in CanvasKit renderer. Add text style tests.

Fixes flutter/flutter#73473
Fixes flutter/flutter#74336

Comment on lines +760 to +789
_defaultTextForeground.setColorInt(
skStyle.color?.value ?? 0xFF000000,
);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a bad feeling about this mutation of a static default style. Doesn't it leak the color to other paragraphs?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We haven't found a better way to do this yet. JavaScript doesn't have value types so any structs we pass to WASM end up extra objects allocated in the heap creating GC pressure. So in a few cases where we know it's safe, we keep a shared statically allocated WASM object and use it to pass data to CanvasKit without allocating extra objects. There are a few more examples in canvaskit_api.dart where we do this, such as:

final SkFloat32List _sharedSkColor1 = mallocFloat32List(4);

I tested this specifically by invoking it twice in a row and I didn't see it leaking to another text style. But I agree, that this can indeed be dangerous. You have to know that internally Skia is taking the object by value rather than by pointer. We should avoid this pattern unless there's a strong reason to use it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One case I can think of is:

  1. First paragraph:
  • foreground == null
  • skStyle.color.value == GREEN
  1. Second paragraph:
  • foreground == null
  • skStyle.color.value == null

Expected: The first paragraph is green, the second paragraph has the default color (black?).

Actual: The green color leaked to the second paragraph.

That said, I agree with the GC pressure issue. Maybe add a quick comment here explaining the possible leak and the reason it was done this way?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I'm convinced that this is risky, so I added a test to make sure we don't leak styles: https://github.com/flutter/goldens/blob/76847ac130d9dc2ec4188694daff8fa16735a7d8/engine/web/canvaskit_text_styles_do_not_leak.png

@yjbanov yjbanov force-pushed the ck-text-colors branch 4 times, most recently from c9325f7 to 375b134 Compare January 26, 2021 19:09
@yjbanov yjbanov marked this pull request as ready for review January 26, 2021 20:02
@yjbanov yjbanov requested a review from mdebbar January 26, 2021 20:02
@yjbanov yjbanov changed the title [canvaskit] fix text background/foreground, font family fallback; add text style tests [canvaskit] fix text background, foreground, color; add text style tests Jan 26, 2021
addPlaceholder,
}

String _fontFamilyOrFallback(String? fontFamily) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shouldn't be necessary. Roboto is always added to the global fallback fonts

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

void pop() {
if (_styleStack.length <= 1) {
// The top-level text style is paragraph-level. We don't pop it off.
return;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warn?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@yjbanov yjbanov merged commit e598d2f into flutter:master Jan 27, 2021
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Jan 27, 2021
christopherfujino added a commit that referenced this pull request Feb 3, 2021
…24142)

* add ffi_allocation_patch.dart to libraries.yaml 2 (#23954)

* Use `runes` to get code units in CanvasKit. (#24024)

* [windows] Honor only valid resize requests (#23990)

* Load FlutterLoader when creating FlutterEngineGroup (#23980)

* [canvaskit] remove the DOM node of unrendered platform view (#24001)

* update dart to 2.12.0-259.8.beta

* [web] Fix svg based stroke rendering. (#23969)

* [canvaskit] fix text background, foreground, color; add text style tests (#23800)

* [canvaskit] fix text background, foreground, familyFallback; add style tests

* add leak test

* update goldens_lock.yaml

* remove solo

* Warn when popping out of empty text style stack

* [web] Fix alignment issue in rich paragraphs (#23965)

* [web] Implement CanvasParagraph.getLineBoundary (#24037)

* [web] Fix text alignment when transform + tight constraints + DOM rendering (#24036)

* update licenses_third_party golden

Co-authored-by: Daco Harkes <[email protected]>
Co-authored-by: Harry Terkelsen <[email protected]>
Co-authored-by: Kaushik Iska <[email protected]>
Co-authored-by: xster <[email protected]>
Co-authored-by: Yegor <[email protected]>
Co-authored-by: Ferhat <[email protected]>
Co-authored-by: Mouad Debbar <[email protected]>
hjfreyer pushed a commit to hjfreyer/engine that referenced this pull request Mar 22, 2021
…sts (flutter#23800)

* [canvaskit] fix text background, foreground, familyFallback; add style tests

* add leak test

* update goldens_lock.yaml

* remove solo

* Warn when popping out of empty text style stack
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[canvaskit] font fallback broken [Web] : TextStyle color does not work with backgroundColor using canvasKit

3 participants