Implement Paint.from(other) for dart:ui. (flutter/engine#51110)
(Tenatively) Closes https://github.com/flutter/flutter/issues/142871. I personally think this is a reasonable request, and we (framework authors) can make some users happy in a fairly simple way (https://github.com/flutter/flutter/issues/142871, https://github.com/flutter/flutter/issues/40497). Some questions: 1. Is the web implementation good enough or would we want to "ship" with an optimized impl? 2. Can folks imagine other edge cases to test beyond correctness and deep/immutable copies?
This commit is contained in:
@@ -1092,6 +1092,22 @@ final class Paint {
|
||||
/// their defaults.
|
||||
Paint();
|
||||
|
||||
/// Constructs a new [Paint] object with the same fields as [other].
|
||||
///
|
||||
/// Any changes made to the object returned will not affect [other], and
|
||||
/// changes to [other] will not affect the object returned.
|
||||
///
|
||||
/// Backends (for example web versus native) may have different performance
|
||||
/// characteristics. If the code is performance-sensitive, consider profiling
|
||||
/// and falling back to reusing a single [Paint] object if necessary.
|
||||
Paint.from(Paint other) {
|
||||
// Every field on Paint is deeply immutable, so to create a copy of a Paint
|
||||
// object, we copy the underlying data buffer and the list of objects (which
|
||||
// are also deeply immutable).
|
||||
_data.buffer.asUint32List().setAll(0, other._data.buffer.asUint32List());
|
||||
_objects = other._objects?.toList();
|
||||
}
|
||||
|
||||
// Paint objects are encoded in two buffers:
|
||||
//
|
||||
// * _data is binary data in four-byte fields, each of which is either a
|
||||
|
||||
@@ -217,6 +217,35 @@ enum Clip {
|
||||
|
||||
abstract class Paint {
|
||||
factory Paint() => engine.renderer.createPaint();
|
||||
|
||||
factory Paint.from(Paint other) {
|
||||
// This is less efficient than copying the underlying buffer or object but
|
||||
// it's a reasonable default, as if a user wanted to implement a copy of a
|
||||
// paint object themselves they are unable to do much better than this.
|
||||
//
|
||||
// TODO(matanlurey): Web team, if important to optimize, could:
|
||||
// 1. Add a `engine.renderer.copyPaint` method.
|
||||
// 2. Use the below code as the default implementation.
|
||||
// 3. Have renderer-specific implementations override with optimized code.
|
||||
final Paint paint = Paint();
|
||||
paint
|
||||
..blendMode = other.blendMode
|
||||
..color = other.color
|
||||
..colorFilter = other.colorFilter
|
||||
..filterQuality = other.filterQuality
|
||||
..imageFilter = other.imageFilter
|
||||
..invertColors = other.invertColors
|
||||
..isAntiAlias = other.isAntiAlias
|
||||
..maskFilter = other.maskFilter
|
||||
..shader = other.shader
|
||||
..strokeCap = other.strokeCap
|
||||
..strokeJoin = other.strokeJoin
|
||||
..strokeMiterLimit = other.strokeMiterLimit
|
||||
..strokeWidth = other.strokeWidth
|
||||
..style = other.style;
|
||||
return paint;
|
||||
}
|
||||
|
||||
BlendMode get blendMode;
|
||||
set blendMode(BlendMode value);
|
||||
PaintingStyle get style;
|
||||
|
||||
@@ -62,4 +62,52 @@ Future<void> testMain() async {
|
||||
')',
|
||||
);
|
||||
});
|
||||
|
||||
test('.from copies every field', () {
|
||||
final ui.Paint paint = ui.Paint();
|
||||
paint.blendMode = ui.BlendMode.darken;
|
||||
paint.style = ui.PaintingStyle.fill;
|
||||
paint.strokeWidth = 1.2;
|
||||
paint.strokeCap = ui.StrokeCap.square;
|
||||
paint.strokeJoin = ui.StrokeJoin.bevel;
|
||||
paint.isAntiAlias = true;
|
||||
paint.color = const ui.Color(0xaabbccdd);
|
||||
paint.invertColors = true;
|
||||
paint.shader = ui.Gradient.linear(
|
||||
const ui.Offset(0.1, 0.2),
|
||||
const ui.Offset(1.5, 1.6),
|
||||
const <ui.Color>[
|
||||
ui.Color(0xaabbccdd),
|
||||
ui.Color(0xbbccddee),
|
||||
],
|
||||
<double>[0.3, 0.4],
|
||||
ui.TileMode.decal,
|
||||
);
|
||||
paint.maskFilter = const ui.MaskFilter.blur(ui.BlurStyle.normal, 1.7);
|
||||
paint.filterQuality = ui.FilterQuality.high;
|
||||
paint.colorFilter = const ui.ColorFilter.linearToSrgbGamma();
|
||||
paint.strokeMiterLimit = 1.8;
|
||||
paint.imageFilter = ui.ImageFilter.blur(
|
||||
sigmaX: 1.9,
|
||||
sigmaY: 2.1,
|
||||
tileMode: ui.TileMode.mirror,
|
||||
);
|
||||
|
||||
final ui.Paint copy = ui.Paint.from(paint);
|
||||
|
||||
expect(copy.blendMode, paint.blendMode);
|
||||
expect(copy.style, paint.style);
|
||||
expect(copy.strokeWidth, paint.strokeWidth);
|
||||
expect(copy.strokeCap, paint.strokeCap);
|
||||
expect(copy.strokeJoin, paint.strokeJoin);
|
||||
expect(copy.isAntiAlias, paint.isAntiAlias);
|
||||
expect(copy.color, paint.color);
|
||||
expect(copy.invertColors, paint.invertColors);
|
||||
expect(copy.shader, paint.shader);
|
||||
expect(copy.maskFilter, paint.maskFilter);
|
||||
expect(copy.filterQuality, paint.filterQuality);
|
||||
expect(copy.colorFilter, paint.colorFilter);
|
||||
expect(copy.strokeMiterLimit, paint.strokeMiterLimit);
|
||||
expect(copy.imageFilter, paint.imageFilter);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1162,6 +1162,111 @@ void main() async {
|
||||
final Image image = await picture.toImage(200, 200);
|
||||
await comparer.addGoldenImage(image, 'text_decoration.png');
|
||||
});
|
||||
|
||||
test('Paint, when copied, has equivalent fields', () {
|
||||
final Paint paint = Paint()
|
||||
..color = const Color(0xFF0000FF)
|
||||
..strokeWidth = 10.0
|
||||
..strokeCap = StrokeCap.round
|
||||
..strokeJoin = StrokeJoin.round
|
||||
..style = PaintingStyle.stroke
|
||||
..blendMode = BlendMode.srcOver
|
||||
..maskFilter = const MaskFilter.blur(BlurStyle.normal, 10.0)
|
||||
..filterQuality = FilterQuality.high
|
||||
..colorFilter = const ColorFilter.mode(Color(0xFF00FF00), BlendMode.color)
|
||||
..imageFilter = ImageFilter.blur(sigmaX: 10.0, sigmaY: 10.0);
|
||||
|
||||
final Paint paintCopy = Paint.from(paint);
|
||||
expect(paintCopy.color, equals(const Color(0xFF0000FF)));
|
||||
expect(paintCopy.strokeWidth, equals(10.0));
|
||||
expect(paintCopy.strokeCap, equals(StrokeCap.round));
|
||||
expect(paintCopy.strokeJoin, equals(StrokeJoin.round));
|
||||
expect(paintCopy.style, equals(PaintingStyle.stroke));
|
||||
expect(paintCopy.blendMode, equals(BlendMode.srcOver));
|
||||
expect(paintCopy.maskFilter, equals(const MaskFilter.blur(BlurStyle.normal, 10.0)));
|
||||
expect(paintCopy.filterQuality, equals(FilterQuality.high));
|
||||
expect(paintCopy.colorFilter, equals(const ColorFilter.mode(Color(0xFF00FF00), BlendMode.color)));
|
||||
expect(paintCopy.imageFilter, equals(ImageFilter.blur(sigmaX: 10.0, sigmaY: 10.0)));
|
||||
});
|
||||
|
||||
test('Paint, when copied, does not mutate the original instance', () {
|
||||
final Paint paint = Paint()
|
||||
..color = const Color(0xFF0000FF)
|
||||
..strokeWidth = 10.0
|
||||
..strokeCap = StrokeCap.round
|
||||
..strokeJoin = StrokeJoin.round
|
||||
..style = PaintingStyle.stroke
|
||||
..blendMode = BlendMode.srcOver
|
||||
..maskFilter = const MaskFilter.blur(BlurStyle.normal, 10.0)
|
||||
..filterQuality = FilterQuality.high
|
||||
..colorFilter = const ColorFilter.mode(Color(0xFF00FF00), BlendMode.color)
|
||||
..imageFilter = ImageFilter.blur(sigmaX: 10.0, sigmaY: 10.0);
|
||||
|
||||
// Make a copy, and change every field of the copy.
|
||||
Paint.from(paint)
|
||||
..color = const Color(0xFF00FF00)
|
||||
..strokeWidth = 20.0
|
||||
..strokeCap = StrokeCap.butt
|
||||
..strokeJoin = StrokeJoin.bevel
|
||||
..style = PaintingStyle.fill
|
||||
..blendMode = BlendMode.srcIn
|
||||
..maskFilter = const MaskFilter.blur(BlurStyle.solid, 20.0)
|
||||
..filterQuality = FilterQuality.none
|
||||
..colorFilter = const ColorFilter.mode(Color(0xFFFF0000), BlendMode.modulate)
|
||||
..imageFilter = ImageFilter.blur(sigmaX: 20.0, sigmaY: 20.0);
|
||||
|
||||
// The original paint should not have changed.
|
||||
expect(paint.color, equals(const Color(0xFF0000FF)));
|
||||
expect(paint.strokeWidth, equals(10.0));
|
||||
expect(paint.strokeCap, equals(StrokeCap.round));
|
||||
expect(paint.strokeJoin, equals(StrokeJoin.round));
|
||||
expect(paint.style, equals(PaintingStyle.stroke));
|
||||
expect(paint.blendMode, equals(BlendMode.srcOver));
|
||||
expect(paint.maskFilter, equals(const MaskFilter.blur(BlurStyle.normal, 10.0)));
|
||||
expect(paint.filterQuality, equals(FilterQuality.high));
|
||||
expect(paint.colorFilter, equals(const ColorFilter.mode(Color(0xFF00FF00), BlendMode.color)));
|
||||
expect(paint.imageFilter, equals(ImageFilter.blur(sigmaX: 10.0, sigmaY: 10.0)));
|
||||
});
|
||||
|
||||
test('Paint, when copied, the original changing does not mutate the copy', () {
|
||||
final Paint paint = Paint()
|
||||
..color = const Color(0xFF0000FF)
|
||||
..strokeWidth = 10.0
|
||||
..strokeCap = StrokeCap.round
|
||||
..strokeJoin = StrokeJoin.round
|
||||
..style = PaintingStyle.stroke
|
||||
..blendMode = BlendMode.srcOver
|
||||
..maskFilter = const MaskFilter.blur(BlurStyle.normal, 10.0)
|
||||
..filterQuality = FilterQuality.high
|
||||
..colorFilter = const ColorFilter.mode(Color(0xFF00FF00), BlendMode.color)
|
||||
..imageFilter = ImageFilter.blur(sigmaX: 10.0, sigmaY: 10.0);
|
||||
|
||||
// Make a copy, and change every field of the original.
|
||||
final Paint paintCopy = Paint.from(paint);
|
||||
paint
|
||||
..color = const Color(0xFF00FF00)
|
||||
..strokeWidth = 20.0
|
||||
..strokeCap = StrokeCap.butt
|
||||
..strokeJoin = StrokeJoin.bevel
|
||||
..style = PaintingStyle.fill
|
||||
..blendMode = BlendMode.srcIn
|
||||
..maskFilter = const MaskFilter.blur(BlurStyle.solid, 20.0)
|
||||
..filterQuality = FilterQuality.none
|
||||
..colorFilter = const ColorFilter.mode(Color(0xFFFF0000), BlendMode.modulate)
|
||||
..imageFilter = ImageFilter.blur(sigmaX: 20.0, sigmaY: 20.0);
|
||||
|
||||
// The copy should not have changed.
|
||||
expect(paintCopy.color, equals(const Color(0xFF0000FF)));
|
||||
expect(paintCopy.strokeWidth, equals(10.0));
|
||||
expect(paintCopy.strokeCap, equals(StrokeCap.round));
|
||||
expect(paintCopy.strokeJoin, equals(StrokeJoin.round));
|
||||
expect(paintCopy.style, equals(PaintingStyle.stroke));
|
||||
expect(paintCopy.blendMode, equals(BlendMode.srcOver));
|
||||
expect(paintCopy.maskFilter, equals(const MaskFilter.blur(BlurStyle.normal, 10.0)));
|
||||
expect(paintCopy.filterQuality, equals(FilterQuality.high));
|
||||
expect(paintCopy.colorFilter, equals(const ColorFilter.mode(Color(0xFF00FF00), BlendMode.color)));
|
||||
expect(paintCopy.imageFilter, equals(ImageFilter.blur(sigmaX: 10.0, sigmaY: 10.0)));
|
||||
});
|
||||
}
|
||||
|
||||
Matcher listEquals(ByteData expected) => (dynamic v) {
|
||||
|
||||
Reference in New Issue
Block a user