From 73556f8a4232eb33095837f5b68508515a1f53a2 Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Mon, 10 May 2021 23:18:10 +0200 Subject: [PATCH 1/2] Added failing DrawingContextImpl tests. Skia `DrawingContextImpl` throws a NRE when passed a pen with 0 thickness. --- .../DrawingContextImplTests.cs | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 tests/Avalonia.Skia.UnitTests/DrawingContextImplTests.cs diff --git a/tests/Avalonia.Skia.UnitTests/DrawingContextImplTests.cs b/tests/Avalonia.Skia.UnitTests/DrawingContextImplTests.cs new file mode 100644 index 0000000000..df0cc2fc1a --- /dev/null +++ b/tests/Avalonia.Skia.UnitTests/DrawingContextImplTests.cs @@ -0,0 +1,30 @@ +using Avalonia.Media; +using Avalonia.Skia.Helpers; +using SkiaSharp; +using Xunit; + +namespace Avalonia.Skia.UnitTests +{ + public class DrawingContextImplTests + { + [Fact] + public void DrawLine_With_Zero_Thickness_Pen_Does_Not_Throw() + { + var target = CreateTarget(); + target.DrawLine(new Pen(Brushes.Black, 0), new Point(0, 0), new Point(10, 10)); + } + + [Fact] + public void DrawRectangle_With_Zero_Thickness_Pen_Does_Not_Throw() + { + var target = CreateTarget(); + target.DrawRectangle(Brushes.Black, new Pen(Brushes.Black, 0), new RoundedRect(new Rect(0, 0, 100, 100), new CornerRadius(4))); + } + + private DrawingContextImpl CreateTarget() + { + var canvas = new SKCanvas(new SKBitmap(100, 100)); + return (DrawingContextImpl)DrawingContextHelper.WrapSkiaCanvas(canvas, new Vector(96, 96)); + } + } +} From 64e0fb7f241d4f25c32f13ef680538518ad63e9b Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Mon, 10 May 2021 23:21:50 +0200 Subject: [PATCH 2/2] Check for null paint. `CreatePaint` when passed a `Pen` can return `default` which means that `paint.paint` will be null. Check for this before trying to use it. --- src/Skia/Avalonia.Skia/DrawingContextImpl.cs | 22 ++++++++++++-------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/Skia/Avalonia.Skia/DrawingContextImpl.cs b/src/Skia/Avalonia.Skia/DrawingContextImpl.cs index b7d5d3ec59..2c5c2acd52 100644 --- a/src/Skia/Avalonia.Skia/DrawingContextImpl.cs +++ b/src/Skia/Avalonia.Skia/DrawingContextImpl.cs @@ -166,7 +166,10 @@ namespace Avalonia.Skia { using (var paint = CreatePaint(_strokePaint, pen, new Size(Math.Abs(p2.X - p1.X), Math.Abs(p2.Y - p1.Y)))) { - Canvas.DrawLine((float) p1.X, (float) p1.Y, (float) p2.X, (float) p2.Y, paint.Paint); + if (paint.Paint is object) + { + Canvas.DrawLine((float)p1.X, (float)p1.Y, (float)p2.X, (float)p2.Y, paint.Paint); + } } } @@ -361,7 +364,6 @@ namespace Avalonia.Skia { Canvas.DrawRect(rc, paint.Paint); } - } } @@ -397,15 +399,17 @@ namespace Avalonia.Skia { using (var paint = CreatePaint(_strokePaint, pen, rect.Rect.Size)) { - if (isRounded) - { - Canvas.DrawRoundRect(skRoundRect, paint.Paint); - } - else + if (paint.Paint is object) { - Canvas.DrawRect(rc, paint.Paint); + if (isRounded) + { + Canvas.DrawRoundRect(skRoundRect, paint.Paint); + } + else + { + Canvas.DrawRect(rc, paint.Paint); + } } - } } }