Skip to content

[web] Unify ui.ImageFilter between CanvasKit and Skwasm - #191021

Merged
auto-submit[bot] merged 4 commits into
flutter:masterfrom
harryterkelsen:unify-image-filter
Aug 17, 2026
Merged

auto-submit[bot] merged 4 commits into
flutter:masterfrom
harryterkelsen:unify-image-filter

Conversation

@harryterkelsen

Copy link
Copy Markdown
Contributor

Part of #175630.

Refactors ui.ImageFilter to share core business logic between CanvasKit and Skwasm through the EngineImageFilter layer. Backend-specific implementations are intentionally kept minimal, with both backends now only implementing BackendImageFilter to provide the final native calls.

Pre-launch Checklist

If you need help, consider asking for advice on the #hackers-new channel on Discord.

If this change needs to override an active code freeze, provide a comment explaining why. The code freeze workflow can be overridden by code reviewers. See pinned issues for any active code freezes with guidance.

Note: The Flutter team is currently trialing the use of Gemini Code Assist for GitHub. Comments from the gemini-code-assist bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed.

Part of flutter#175630.

Refactors `ui.ImageFilter` to share core business logic between CanvasKit and Skwasm through the `EngineImageFilter` layer. Backend-specific implementations are intentionally kept minimal, with both backends now only implementing `BackendImageFilter` to provide the final native calls.
@flutter-dashboard flutter-dashboard Bot added the CICD Run CI/CD label Aug 12, 2026
@harryterkelsen
harryterkelsen requested a review from mdebbar August 12, 2026 19:42
@github-actions github-actions Bot added engine flutter/engine related. See also e: labels. platform-web Web applications specifically team-web Owned by Web platform team labels Aug 12, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request refactors the web engine's image filter implementation by introducing a shared EngineImageFilter abstraction to manage the lifecycle and caching of backend-specific filters (CanvasKit and Skwasm), replacing the previous backend-coupled design. The review feedback highlights a performance concern where EngineBlurImageFilter and EngineComposeImageFilter only cache a single filter instance, leading to cache thrashing and redundant allocations when shared across different tile mode contexts. It is recommended to cache these filters in a map keyed by the resolved TileMode and to add tests verifying this multi-context caching behavior.


if (_cachedFilter != null) {
_imageFilterFinalizer.detach(_cachedFilter!);
_cachedFilter!.dispose();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I have two concerns here, but I'm not sure how valid they are, so feel free to push back:

  1. There's a potential of _cachedFilter leakage when the framework keeps creating throw-away ImageFilters. The framework doesn't explicitly dispose of image filters AFAICT.
  2. If the same ImageFilter instance is used in the same frame with multiple tile modes. The older ones will be disposed before Skia is done with them.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I found that instances created by CkPaint.toSkPaint are always immediately disposed after use (in canvas.dart). So maybe point 1 is not valid?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

  1. Even if the framework never explicitly disposes the ImageFilter, it should still get GC'ed and the finalizer will run for the cached filter.
  2. Even if they are disposed on the Dart side, the Skia side won't dispose of them if they are referenced by a DisplayList or Paint object because they use smart pointers. Disposing on the Dart side only decrements the ref count

final EngineImageFilter engineFilter;
if (filter is ui.ColorFilter) {
convertible = CkColorFilterImageFilter(colorFilter: filter as EngineColorFilter);
engineFilter = EngineColorFilterImageFilter(colorFilter: filter as EngineColorFilter);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This may not be a new issue, but this line of code means we are creating a new native/backend color filter on every frame. We are also not benefiting from any caching.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good point, I added caching here.

@harryterkelsen

Copy link
Copy Markdown
Contributor Author

Thanks, @mdebbar. PTAL!

@harryterkelsen
harryterkelsen requested a review from mdebbar August 14, 2026 19:09
@harryterkelsen harryterkelsen added the autosubmit Merge PR when tree becomes green via auto submit App label Aug 17, 2026
@auto-submit auto-submit Bot removed the autosubmit Merge PR when tree becomes green via auto submit App label Aug 17, 2026
@auto-submit

auto-submit Bot commented Aug 17, 2026

Copy link
Copy Markdown
Contributor

autosubmit label was removed for flutter/flutter/191021, because This PR has not met approval requirements for merging. The PR author is not a member of flutter-hackers and needs 2 more review(s) in order to merge this PR.

  • Merge guidelines: A PR needs at least one approved review if the author is already part of flutter-hackers or two member reviews if the author is not a member of flutter-hackers before re-applying the autosubmit label. Reviewers: If you left a comment approving, please use the "approve" review action instead.

@mdebbar mdebbar added the autosubmit Merge PR when tree becomes green via auto submit App label Aug 17, 2026
@auto-submit
auto-submit Bot added this pull request to the merge queue Aug 17, 2026
Merged via the queue into flutter:master with commit f6c6f91 Aug 17, 2026
28 checks passed
@flutter-dashboard flutter-dashboard Bot removed the autosubmit Merge PR when tree becomes green via auto submit App label Aug 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CICD Run CI/CD engine flutter/engine related. See also e: labels. platform-web Web applications specifically team-web Owned by Web platform team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants