From 3bcafff07ac549761004a9e1e5e29c05ea8f8f2f Mon Sep 17 00:00:00 2001 From: "auto-submit[bot]" Date: Sat, 7 Sep 2024 00:18:13 +0000 Subject: [PATCH] Revert "[skwasm] use temporary RawPaint objects (#54917)" This reverts commit 0fad4601a1fbc87d3ac68965c2a3cf5baecdebcf. --- .../src/engine/skwasm/skwasm_impl/canvas.dart | 156 ++++++-------- .../src/engine/skwasm/skwasm_impl/paint.dart | 204 ++++++++++-------- .../engine/skwasm/skwasm_impl/paragraph.dart | 8 +- .../skwasm/skwasm_impl/raw/raw_paint.dart | 69 ++++-- lib/web_ui/skwasm/paint.cpp | 82 +++++-- 5 files changed, 292 insertions(+), 227 deletions(-) diff --git a/lib/web_ui/lib/src/engine/skwasm/skwasm_impl/canvas.dart b/lib/web_ui/lib/src/engine/skwasm/skwasm_impl/canvas.dart index f04e502fd75ea..e33a515d281b9 100644 --- a/lib/web_ui/lib/src/engine/skwasm/skwasm_impl/canvas.dart +++ b/lib/web_ui/lib/src/engine/skwasm/skwasm_impl/canvas.dart @@ -29,29 +29,27 @@ class SkwasmCanvas implements SceneCanvas { @override void saveLayer(ui.Rect? bounds, ui.Paint paint) { - final paintHandle = (paint as SkwasmPaint).toRawPaint(); + paint as SkwasmPaint; if (bounds != null) { withStackScope((StackScope s) { - canvasSaveLayer(_handle, s.convertRectToNative(bounds), paintHandle, nullptr); + canvasSaveLayer(_handle, s.convertRectToNative(bounds), paint.handle, nullptr); }); } else { - canvasSaveLayer(_handle, nullptr, paintHandle, nullptr); + canvasSaveLayer(_handle, nullptr, paint.handle, nullptr); } - paintDispose(paintHandle); } @override void saveLayerWithFilter(ui.Rect? bounds, ui.Paint paint, ui.ImageFilter imageFilter) { final SkwasmImageFilter nativeFilter = SkwasmImageFilter.fromUiFilter(imageFilter); - final paintHandle = (paint as SkwasmPaint).toRawPaint(); + paint as SkwasmPaint; if (bounds != null) { withStackScope((StackScope s) { - canvasSaveLayer(_handle, s.convertRectToNative(bounds), paintHandle, nativeFilter.handle); + canvasSaveLayer(_handle, s.convertRectToNative(bounds), paint.handle, nativeFilter.handle); }); } else { - canvasSaveLayer(_handle, nullptr, paintHandle, nativeFilter.handle); + canvasSaveLayer(_handle, nullptr, paint.handle, nativeFilter.handle); } - paintDispose(paintHandle); } @override @@ -113,78 +111,71 @@ class SkwasmCanvas implements SceneCanvas { @override void drawLine(ui.Offset p1, ui.Offset p2, ui.Paint paint) { - final paintHandle = (paint as SkwasmPaint).toRawPaint(); - canvasDrawLine(_handle, p1.dx, p1.dy, p2.dx, p2.dy, paintHandle); - paintDispose(paintHandle); + paint as SkwasmPaint; + canvasDrawLine(_handle, p1.dx, p1.dy, p2.dx, p2.dy, paint.handle); } @override void drawPaint(ui.Paint paint) { - final paintHandle = (paint as SkwasmPaint).toRawPaint(); - canvasDrawPaint(_handle, paintHandle); - paintDispose(paintHandle); + paint as SkwasmPaint; + canvasDrawPaint(_handle, paint.handle); } @override void drawRect(ui.Rect rect, ui.Paint paint) { - final paintHandle = (paint as SkwasmPaint).toRawPaint(); + paint as SkwasmPaint; withStackScope((StackScope s) { canvasDrawRect( _handle, s.convertRectToNative(rect), - paintHandle + paint.handle ); }); - paintDispose(paintHandle); } @override void drawRRect(ui.RRect rrect, ui.Paint paint) { - final paintHandle = (paint as SkwasmPaint).toRawPaint(); + paint as SkwasmPaint; withStackScope((StackScope s) { canvasDrawRRect( _handle, s.convertRRectToNative(rrect), - paintHandle + paint.handle ); }); - paintDispose(paintHandle); } @override void drawDRRect(ui.RRect outer, ui.RRect inner, ui.Paint paint) { - final paintHandle = (paint as SkwasmPaint).toRawPaint(); + paint as SkwasmPaint; withStackScope((StackScope s) { canvasDrawDRRect( _handle, s.convertRRectToNative(outer), s.convertRRectToNative(inner), - paintHandle + paint.handle ); }); - paintDispose(paintHandle); } @override void drawOval(ui.Rect rect, ui.Paint paint) { - final paintHandle = (paint as SkwasmPaint).toRawPaint(); + paint as SkwasmPaint; withStackScope((StackScope s) { - canvasDrawOval(_handle, s.convertRectToNative(rect), paintHandle); + canvasDrawOval(_handle, s.convertRectToNative(rect), paint.handle); }); - paintDispose(paintHandle); } @override void drawCircle(ui.Offset center, double radius, ui.Paint paint) { - final paintHandle = (paint as SkwasmPaint).toRawPaint(); - canvasDrawCircle(_handle, center.dx, center.dy, radius, paintHandle); - paintDispose(paintHandle); + paint as SkwasmPaint; + canvasDrawCircle(_handle, center.dx, center.dy, radius, paint.handle); } @override void drawArc(ui.Rect rect, double startAngle, double sweepAngle, bool useCenter, ui.Paint paint) { - final paintHandle = (paint as SkwasmPaint).toRawPaint(); + paint as SkwasmPaint; withStackScope((StackScope s) { canvasDrawArc( _handle, @@ -192,79 +183,64 @@ class SkwasmCanvas implements SceneCanvas { ui.toDegrees(startAngle), ui.toDegrees(sweepAngle), useCenter, - paintHandle, + paint.handle ); }); - paintDispose(paintHandle); } @override void drawPath(ui.Path path, ui.Paint paint) { + paint as SkwasmPaint; path as SkwasmPath; - final paintHandle = (paint as SkwasmPaint).toRawPaint(); - canvasDrawPath(_handle, path.handle, paintHandle); - paintDispose(paintHandle); + canvasDrawPath(_handle, path.handle, paint.handle); } @override - void drawImage(ui.Image image, ui.Offset offset, ui.Paint paint) { - final paintHandle = (paint as SkwasmPaint).toRawPaint(); + void drawImage(ui.Image image, ui.Offset offset, ui.Paint paint) => canvasDrawImage( _handle, (image as SkwasmImage).handle, offset.dx, offset.dy, - paintHandle, + (paint as SkwasmPaint).handle, paint.filterQuality.index, ); - paintDispose(paintHandle); - } @override void drawImageRect( ui.Image image, ui.Rect src, ui.Rect dst, - ui.Paint paint, - ) { - withStackScope((StackScope scope) { - final Pointer sourceRect = scope.convertRectToNative(src); - final Pointer destRect = scope.convertRectToNative(dst); - final paintHandle = (paint as SkwasmPaint).toRawPaint(); - canvasDrawImageRect( - _handle, - (image as SkwasmImage).handle, - sourceRect, - destRect, - paintHandle, - paint.filterQuality.index, - ); - paintDispose(paintHandle); - }); - } + ui.Paint paint) => withStackScope((StackScope scope) { + final Pointer sourceRect = scope.convertRectToNative(src); + final Pointer destRect = scope.convertRectToNative(dst); + canvasDrawImageRect( + _handle, + (image as SkwasmImage).handle, + sourceRect, + destRect, + (paint as SkwasmPaint).handle, + paint.filterQuality.index, + ); + }); @override void drawImageNine( ui.Image image, ui.Rect center, ui.Rect dst, - ui.Paint paint, - ) { - withStackScope((StackScope scope) { - final Pointer centerRect = scope.convertIRectToNative(center); - final Pointer destRect = scope.convertRectToNative(dst); - final paintHandle = (paint as SkwasmPaint).toRawPaint(); - canvasDrawImageNine( - _handle, - (image as SkwasmImage).handle, - centerRect, - destRect, - paintHandle, - paint.filterQuality.index, - ); - paintDispose(paintHandle); - }); - } + ui.Paint paint) => withStackScope((StackScope scope) { + final Pointer centerRect = scope.convertIRectToNative(center); + final Pointer destRect = scope.convertRectToNative(dst); + canvasDrawImageNine( + _handle, + (image as SkwasmImage).handle, + centerRect, + destRect, + (paint as SkwasmPaint).handle, + paint.filterQuality.index, + ); + }); @override void drawPicture(ui.Picture picture) { @@ -288,15 +264,13 @@ class SkwasmCanvas implements SceneCanvas { ui.Paint paint ) => withStackScope((StackScope scope) { final RawPointArray rawPoints = scope.convertPointArrayToNative(points); - final paintHandle = (paint as SkwasmPaint).toRawPaint(); canvasDrawPoints( _handle, pointMode.index, rawPoints, points.length, - paintHandle, + (paint as SkwasmPaint).handle, ); - paintDispose(paintHandle); }); @override @@ -306,15 +280,13 @@ class SkwasmCanvas implements SceneCanvas { ui.Paint paint ) => withStackScope((StackScope scope) { final RawPointArray rawPoints = scope.convertDoublesToNative(points); - final paintHandle = (paint as SkwasmPaint).toRawPaint(); canvasDrawPoints( _handle, pointMode.index, rawPoints, points.length ~/ 2, - paintHandle, + (paint as SkwasmPaint).handle, ); - paintDispose(paintHandle); }); @override @@ -322,16 +294,12 @@ class SkwasmCanvas implements SceneCanvas { ui.Vertices vertices, ui.BlendMode blendMode, ui.Paint paint, - ) { - final paintHandle = (paint as SkwasmPaint).toRawPaint(); - canvasDrawVertices( - _handle, - (vertices as SkwasmVertices).handle, - blendMode.index, - paintHandle, - ); - paintDispose(paintHandle); - } + ) => canvasDrawVertices( + _handle, + (vertices as SkwasmVertices).handle, + blendMode.index, + (paint as SkwasmPaint).handle, + ); @override void drawAtlas( @@ -351,7 +319,6 @@ class SkwasmCanvas implements SceneCanvas { final RawRect rawCullRect = cullRect != null ? scope.convertRectToNative(cullRect) : nullptr; - final paintHandle = (paint as SkwasmPaint).toRawPaint(); canvasDrawAtlas( _handle, (atlas as SkwasmImage).handle, @@ -361,9 +328,8 @@ class SkwasmCanvas implements SceneCanvas { transforms.length, (blendMode ?? ui.BlendMode.src).index, rawCullRect, - paintHandle, + (paint as SkwasmPaint).handle, ); - paintDispose(paintHandle); }); @override @@ -384,7 +350,6 @@ class SkwasmCanvas implements SceneCanvas { final RawRect rawCullRect = cullRect != null ? scope.convertRectToNative(cullRect) : nullptr; - final paintHandle = (paint as SkwasmPaint).toRawPaint(); canvasDrawAtlas( _handle, (atlas as SkwasmImage).handle, @@ -394,9 +359,8 @@ class SkwasmCanvas implements SceneCanvas { rstTransforms.length ~/ 4, (blendMode ?? ui.BlendMode.src).index, rawCullRect, - paintHandle, + (paint as SkwasmPaint).handle, ); - paintDispose(paintHandle); }); @override diff --git a/lib/web_ui/lib/src/engine/skwasm/skwasm_impl/paint.dart b/lib/web_ui/lib/src/engine/skwasm/skwasm_impl/paint.dart index 0abe010e5e69a..507bf94c63ca6 100644 --- a/lib/web_ui/lib/src/engine/skwasm/skwasm_impl/paint.dart +++ b/lib/web_ui/lib/src/engine/skwasm/skwasm_impl/paint.dart @@ -8,73 +8,31 @@ import 'package:ui/src/engine.dart'; import 'package:ui/src/engine/skwasm/skwasm_impl.dart'; import 'package:ui/ui.dart' as ui; -class SkwasmPaint implements ui.Paint { - SkwasmPaint(); - - /// Creates the C++ side paint object based on the current state of this - /// paint object, and returns it with ownership. - /// - /// It is the responsibility of the caller to dispose of the returned handle - /// when it's no longer needed. - PaintHandle toRawPaint() { - final rawPaint = paintCreate( - isAntiAlias, - blendMode.index, - _colorValue, - style.index, - strokeWidth, - strokeCap.index, - strokeJoin.index, - strokeMiterLimit, - ); - - _maybeSetEffectiveColorFilter(rawPaint); - - final shaderHandle = _shader?.handle; - if (shaderHandle != null) { - paintSetShader(rawPaint, shaderHandle); - } +class SkwasmPaint extends SkwasmObjectWrapper implements ui.Paint { + SkwasmPaint() : super(paintCreate(), _registry); - final localMaskFilter = maskFilter; - if (localMaskFilter != null) { - final nativeFilter = SkwasmMaskFilter.fromUiMaskFilter(localMaskFilter); - paintSetMaskFilter(rawPaint, nativeFilter.handle); - nativeFilter.dispose(); - } + // Must be kept in sync with the default in paint.cc. + static const double _kStrokeMiterLimitDefault = 4.0; - final filter = imageFilter; - if (filter != null) { - final nativeImageFilter = SkwasmImageFilter.fromUiFilter(filter); - paintSetImageFilter(rawPaint, nativeImageFilter.handle); - nativeImageFilter.dispose(); - } + // Must be kept in sync with the default in paint.cc. + static const int _kColorDefault = 0xFF000000; - return rawPaint; - } + // Must be kept in sync with the default in paint.cc. + static final int _kBlendModeDefault = ui.BlendMode.srcOver.index; - /// If `invertColors` is true or `colorFilter` is not null, sets the - /// appropriate Skia color filter. Otherwise, does nothing. - void _maybeSetEffectiveColorFilter(Pointer handle) { - final nativeFilter = _colorFilter != null - ? SkwasmColorFilter.fromEngineColorFilter(_colorFilter!) - : null; - if (invertColors) { - if (nativeFilter != null) { - final composedFilter = SkwasmColorFilter.composed( - _invertColorFilter, - nativeFilter, - ); - nativeFilter.dispose(); - paintSetColorFilter(handle, composedFilter.handle); - composedFilter.dispose(); - } else { - paintSetColorFilter(handle, _invertColorFilter.handle); - } - } else if (nativeFilter != null) { - paintSetColorFilter(handle, nativeFilter.handle); - nativeFilter.dispose(); - } - } + static final SkwasmFinalizationRegistry _registry = + SkwasmFinalizationRegistry(paintDispose); + + ui.BlendMode _cachedBlendMode = ui.BlendMode.srcOver; + + SkwasmShader? _shader; + ui.ImageFilter? _imageFilter; + + EngineColorFilter? _colorFilter; + + ui.MaskFilter? _maskFilter; + + bool _invertColors = false; static final SkwasmColorFilter _invertColorFilter = SkwasmColorFilter.fromEngineColorFilter( const EngineColorFilter.matrix([ @@ -86,71 +44,143 @@ class SkwasmPaint implements ui.Paint { ); @override - ui.BlendMode blendMode = _kBlendModeDefault; + ui.BlendMode get blendMode { + return _cachedBlendMode; + } - // Must be kept in sync with the default in paint.cc. - static const ui.BlendMode _kBlendModeDefault = ui.BlendMode.srcOver; + @override + set blendMode(ui.BlendMode blendMode) { + if (_cachedBlendMode != blendMode) { + _cachedBlendMode = blendMode; + paintSetBlendMode(handle, blendMode.index); + } + } @override - ui.PaintingStyle style = ui.PaintingStyle.fill; + ui.PaintingStyle get style => ui.PaintingStyle.values[paintGetStyle(handle)]; @override - double strokeWidth = 0.0; + set style(ui.PaintingStyle style) => paintSetStyle(handle, style.index); @override - ui.StrokeCap strokeCap = ui.StrokeCap.butt; + double get strokeWidth => paintGetStrokeWidth(handle); @override - ui.StrokeJoin strokeJoin = ui.StrokeJoin.miter; + set strokeWidth(double width) => paintSetStrokeWidth(handle, width); @override - bool isAntiAlias = true; + ui.StrokeCap get strokeCap => ui.StrokeCap.values[paintGetStrokeCap(handle)]; @override - ui.Color get color => ui.Color(_colorValue); + set strokeCap(ui.StrokeCap cap) => paintSetStrokeCap(handle, cap.index); + @override - set color(ui.Color value) { - _colorValue = value.value; - } + ui.StrokeJoin get strokeJoin => ui.StrokeJoin.values[paintGetStrokeJoin(handle)]; - static const int _kColorDefault = 0xFF000000; - int _colorValue = _kColorDefault; + @override + set strokeJoin(ui.StrokeJoin join) => paintSetStrokeJoin(handle, join.index); @override - double strokeMiterLimit = _kStrokeMiterLimitDefault; - static const double _kStrokeMiterLimitDefault = 4.0; + bool get isAntiAlias => paintGetAntiAlias(handle); + + @override + set isAntiAlias(bool value) => paintSetAntiAlias(handle, value); + + @override + ui.Color get color => ui.Color(paintGetColorInt(handle)); + + @override + set color(ui.Color color) => paintSetColorInt(handle, color.value); + + @override + double get strokeMiterLimit => paintGetMiterLimit(handle); + + @override + set strokeMiterLimit(double limit) => paintSetMiterLimit(handle, limit); @override ui.Shader? get shader => _shader; @override set shader(ui.Shader? uiShader) { - uiShader as SkwasmShader?; - _shader = uiShader; + final SkwasmShader? skwasmShader = uiShader as SkwasmShader?; + _shader = skwasmShader; + final ShaderHandle shaderHandle = + skwasmShader != null ? skwasmShader.handle : nullptr; + paintSetShader(handle, shaderHandle); } - SkwasmShader? _shader; @override ui.FilterQuality filterQuality = ui.FilterQuality.none; @override - ui.ImageFilter? imageFilter; + ui.ImageFilter? get imageFilter => _imageFilter; + + @override + set imageFilter(ui.ImageFilter? filter) { + _imageFilter = filter; + + final SkwasmImageFilter? nativeImageFilter = filter != null + ? SkwasmImageFilter.fromUiFilter(filter) + : null; + paintSetImageFilter(handle, nativeImageFilter != null ? nativeImageFilter.handle : nullptr); + } @override ui.ColorFilter? get colorFilter => _colorFilter; + void _setEffectiveColorFilter() { + final SkwasmColorFilter? nativeFilter = _colorFilter != null + ? SkwasmColorFilter.fromEngineColorFilter(_colorFilter!) : null; + if (_invertColors) { + if (nativeFilter != null) { + final SkwasmColorFilter composedFilter = SkwasmColorFilter.composed(_invertColorFilter, nativeFilter); + nativeFilter.dispose(); + paintSetColorFilter(handle, composedFilter.handle); + composedFilter.dispose(); + } else { + paintSetColorFilter(handle, _invertColorFilter.handle); + } + } else if (nativeFilter != null) { + paintSetColorFilter(handle, nativeFilter.handle); + nativeFilter.dispose(); + } else { + paintSetColorFilter(handle, nullptr); + } + } + @override set colorFilter(ui.ColorFilter? filter) { _colorFilter = filter as EngineColorFilter?; + _setEffectiveColorFilter(); } - EngineColorFilter? _colorFilter; + @override + ui.MaskFilter? get maskFilter => _maskFilter; @override - ui.MaskFilter? maskFilter; + set maskFilter(ui.MaskFilter? filter) { + _maskFilter = filter; + if (filter == null) { + paintSetMaskFilter(handle, nullptr); + } else { + final SkwasmMaskFilter nativeFilter = SkwasmMaskFilter.fromUiMaskFilter(filter); + paintSetMaskFilter(handle, nativeFilter.handle); + nativeFilter.dispose(); + } + } @override - bool invertColors = false; + bool get invertColors => _invertColors; + + @override + set invertColors(bool invertColors) { + if (_invertColors == invertColors) { + return; + } + _invertColors = invertColors; + _setEffectiveColorFilter(); + } @override String toString() { @@ -187,7 +217,7 @@ class SkwasmPaint implements ui.Paint { result.write('$semicolon$color'); semicolon = '; '; } - if (blendMode.index != _kBlendModeDefault.index) { + if (blendMode.index != _kBlendModeDefault) { result.write('$semicolon$blendMode'); semicolon = '; '; } diff --git a/lib/web_ui/lib/src/engine/skwasm/skwasm_impl/paragraph.dart b/lib/web_ui/lib/src/engine/skwasm/skwasm_impl/paragraph.dart index 88cb9fdc54096..98dad74623e9f 100644 --- a/lib/web_ui/lib/src/engine/skwasm/skwasm_impl/paragraph.dart +++ b/lib/web_ui/lib/src/engine/skwasm/skwasm_impl/paragraph.dart @@ -398,14 +398,10 @@ class SkwasmTextStyle implements ui.TextStyle { skStringFree(localeHandle); } if (background != null) { - final backgroundPaint = (background! as SkwasmPaint).toRawPaint(); - textStyleSetBackground(handle, backgroundPaint); - paintDispose(backgroundPaint); + textStyleSetBackground(handle, (background! as SkwasmPaint).handle); } if (foreground != null) { - final foregroundPaint = (foreground! as SkwasmPaint).toRawPaint(); - textStyleSetForeground(handle, foregroundPaint); - paintDispose(foregroundPaint); + textStyleSetForeground(handle, (foreground! as SkwasmPaint).handle); } if (shadows != null) { for (final ui.Shadow shadow in shadows!) { diff --git a/lib/web_ui/lib/src/engine/skwasm/skwasm_impl/raw/raw_paint.dart b/lib/web_ui/lib/src/engine/skwasm/skwasm_impl/raw/raw_paint.dart index b74f14aff13ac..307e6968d517a 100644 --- a/lib/web_ui/lib/src/engine/skwasm/skwasm_impl/raw/raw_paint.dart +++ b/lib/web_ui/lib/src/engine/skwasm/skwasm_impl/raw/raw_paint.dart @@ -13,32 +13,57 @@ final class RawPaint extends Opaque {} typedef PaintHandle = Pointer; -typedef _PaintCreateInitSignature = PaintHandle Function( - Bool, - Int, - Int, - Int, - Float, - Int, - Int, - Float, -); - -@Native<_PaintCreateInitSignature>(symbol: 'paint_create', isLeaf: true) -external PaintHandle paintCreate( - bool isAntiAlias, - int blendMode, - int color, - int style, - double strokeWidth, - int strokeCap, - int strokeJoin, - double strokeMiterLimit, -); +@Native(symbol: 'paint_create', isLeaf: true) +external PaintHandle paintCreate(); @Native(symbol: 'paint_dispose', isLeaf: true) external void paintDispose(PaintHandle paint); +@Native(symbol: 'paint_setBlendMode', isLeaf: true) +external void paintSetBlendMode(PaintHandle paint, int blendMode); + +@Native(symbol: 'paint_setStyle', isLeaf: true) +external void paintSetStyle(PaintHandle paint, int paintStyle); + +@Native(symbol: 'paint_getStyle', isLeaf: true) +external int paintGetStyle(PaintHandle paint); + +@Native(symbol: 'paint_setStrokeWidth', isLeaf: true) +external void paintSetStrokeWidth(PaintHandle paint, double strokeWidth); + +@Native(symbol: 'paint_getStrokeWidth', isLeaf: true) +external double paintGetStrokeWidth(PaintHandle paint); + +@Native(symbol: 'paint_setStrokeCap', isLeaf: true) +external void paintSetStrokeCap(PaintHandle paint, int cap); + +@Native(symbol: 'paint_getStrokeCap', isLeaf: true) +external int paintGetStrokeCap(PaintHandle paint); + +@Native(symbol: 'paint_setStrokeJoin', isLeaf: true) +external void paintSetStrokeJoin(PaintHandle paint, int join); + +@Native(symbol: 'paint_getStrokeJoin', isLeaf: true) +external int paintGetStrokeJoin(PaintHandle paint); + +@Native(symbol: 'paint_setAntiAlias', isLeaf: true) +external void paintSetAntiAlias(PaintHandle paint, bool antiAlias); + +@Native(symbol: 'paint_getAntiAlias', isLeaf: true) +external bool paintGetAntiAlias(PaintHandle paint); + +@Native(symbol: 'paint_setColorInt', isLeaf: true) +external void paintSetColorInt(PaintHandle paint, int color); + +@Native(symbol: 'paint_getColorInt', isLeaf: true) +external int paintGetColorInt(PaintHandle paint); + +@Native(symbol: 'paint_setMiterLimit', isLeaf: true) +external void paintSetMiterLimit(PaintHandle paint, double miterLimit); + +@Native(symbol: 'paint_getMiterLimit', isLeaf: true) +external double paintGetMiterLimit(PaintHandle paint); + @Native(symbol: 'paint_setShader', isLeaf: true) external void paintSetShader(PaintHandle handle, ShaderHandle shader); diff --git a/lib/web_ui/skwasm/paint.cpp b/lib/web_ui/skwasm/paint.cpp index 1c131a561553f..8bba9a5f8a819 100644 --- a/lib/web_ui/skwasm/paint.cpp +++ b/lib/web_ui/skwasm/paint.cpp @@ -12,23 +12,11 @@ using namespace Skwasm; -SKWASM_EXPORT SkPaint* paint_create(bool isAntiAlias, - SkBlendMode blendMode, - SkColor color, - SkPaint::Style style, - SkScalar strokeWidth, - SkPaint::Cap strokeCap, - SkPaint::Join strokeJoin, - SkScalar strokeMiterLimit) { +SKWASM_EXPORT SkPaint* paint_create() { auto paint = new SkPaint(); - paint->setAntiAlias(isAntiAlias); - paint->setBlendMode(blendMode); - paint->setStyle(style); - paint->setStrokeWidth(strokeWidth); - paint->setStrokeCap(strokeCap); - paint->setStrokeJoin(strokeJoin); - paint->setColor(color); - paint->setStrokeMiter(strokeMiterLimit); + + // Antialias defaults to true in flutter. + paint->setAntiAlias(true); return paint; } @@ -36,6 +24,68 @@ SKWASM_EXPORT void paint_dispose(SkPaint* paint) { delete paint; } +SKWASM_EXPORT void paint_setBlendMode(SkPaint* paint, SkBlendMode mode) { + paint->setBlendMode(mode); +} + +// No getter for blend mode, as it's non trivial. Cache on the dart side. + +SKWASM_EXPORT void paint_setStyle(SkPaint* paint, SkPaint::Style style) { + paint->setStyle(style); +} + +SKWASM_EXPORT SkPaint::Style paint_getStyle(SkPaint* paint) { + return paint->getStyle(); +} + +SKWASM_EXPORT void paint_setStrokeWidth(SkPaint* paint, SkScalar width) { + paint->setStrokeWidth(width); +} + +SKWASM_EXPORT SkScalar paint_getStrokeWidth(SkPaint* paint) { + return paint->getStrokeWidth(); +} + +SKWASM_EXPORT void paint_setStrokeCap(SkPaint* paint, SkPaint::Cap cap) { + paint->setStrokeCap(cap); +} + +SKWASM_EXPORT SkPaint::Cap paint_getStrokeCap(SkPaint* paint) { + return paint->getStrokeCap(); +} + +SKWASM_EXPORT void paint_setStrokeJoin(SkPaint* paint, SkPaint::Join join) { + paint->setStrokeJoin(join); +} + +SKWASM_EXPORT SkPaint::Join paint_getStrokeJoin(SkPaint* paint) { + return paint->getStrokeJoin(); +} + +SKWASM_EXPORT void paint_setAntiAlias(SkPaint* paint, bool antiAlias) { + paint->setAntiAlias(antiAlias); +} + +SKWASM_EXPORT bool paint_getAntiAlias(SkPaint* paint) { + return paint->isAntiAlias(); +} + +SKWASM_EXPORT void paint_setColorInt(SkPaint* paint, SkColor colorInt) { + paint->setColor(colorInt); +} + +SKWASM_EXPORT SkColor paint_getColorInt(SkPaint* paint) { + return paint->getColor(); +} + +SKWASM_EXPORT void paint_setMiterLimit(SkPaint* paint, SkScalar miterLimit) { + paint->setStrokeMiter(miterLimit); +} + +SKWASM_EXPORT SkScalar paint_getMiterLimit(SkPaint* paint) { + return paint->getStrokeMiter(); +} + SKWASM_EXPORT void paint_setShader(SkPaint* paint, SkShader* shader) { paint->setShader(sk_ref_sp(shader)); }