From 46125d07db603d6b053270aab8c826721a16ef2f Mon Sep 17 00:00:00 2001 From: Ian Hickson Date: Thu, 13 Jul 2023 18:43:47 -0700 Subject: [PATCH] Mention saveLayer in the CustomPainter docs. (#130520) Fixes https://github.com/flutter/flutter/issues/11002 --- .../flutter/lib/src/rendering/custom_paint.dart | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/packages/flutter/lib/src/rendering/custom_paint.dart b/packages/flutter/lib/src/rendering/custom_paint.dart index e59d51ad99..cb2142315c 100644 --- a/packages/flutter/lib/src/rendering/custom_paint.dart +++ b/packages/flutter/lib/src/rendering/custom_paint.dart @@ -119,6 +119,23 @@ typedef SemanticsBuilderCallback = List Function(Size si /// ``` /// {@end-tool} /// +/// ## Composition and the sharing of canvases +/// +/// Widgets (or rather, render objects) are composited together using a minimum +/// number of [Canvas]es, for performance reasons. As a result, a +/// [CustomPainter]'s [Canvas] may be the same as that used by other widgets +/// (including other [CustomPaint] widgets). +/// +/// This is mostly unnoticeable, except when using unusual [BlendMode]s. For +/// example, trying to use [BlendMode.dstOut] to "punch a hole" through a +/// previously-drawn image may erase more than was intended, because previous +/// widgets will have been painted onto the same canvas. +/// +/// To avoid this issue, consider using [Canvas.saveLayer] and +/// [Canvas.restore] when using such blend modes. Creating new layers is +/// relatively expensive, however, and should be done sparingly to avoid +/// introducing jank. +/// /// See also: /// /// * [Canvas], the class that a custom painter uses to paint.