From f78d858ac0ab014936f73d6cd0ee05e38a3e653a Mon Sep 17 00:00:00 2001 From: Murdo R Ergeaux Date: Sat, 4 Jul 2020 16:47:17 +0100 Subject: [PATCH] Performance improvements --- .../OxyPlot.Avalonia/CanvasRenderContext.cs | 62 ++++++++++++------- 1 file changed, 38 insertions(+), 24 deletions(-) diff --git a/Source/OxyPlot.Avalonia/CanvasRenderContext.cs b/Source/OxyPlot.Avalonia/CanvasRenderContext.cs index 0b60d3c..5e950c1 100644 --- a/Source/OxyPlot.Avalonia/CanvasRenderContext.cs +++ b/Source/OxyPlot.Avalonia/CanvasRenderContext.cs @@ -80,7 +80,7 @@ public class CanvasRenderContext : IRenderContext public CanvasRenderContext(Canvas canvas) { this.canvas = canvas; - UseStreamGeometry = false; // Temporarily disabled because of Avalonia bug + UseStreamGeometry = true; RendersToScreen = true; BalancedLineDrawingThicknessLimit = 3.5; } @@ -112,17 +112,14 @@ public CanvasRenderContext(Canvas canvas) /// The thickness (in device independent units, 1/96 inch). public void DrawEllipse(OxyRect rect, OxyColor fill, OxyColor stroke, double thickness) { - var e = CreateAndAdd(rect.Left, rect.Top); - SetStroke(e, stroke, thickness); - if (fill.IsVisible()) + var path = CreateAndAdd(); + SetStroke(path, stroke, thickness); + if (!fill.IsUndefined()) { - e.Fill = GetCachedBrush(fill); + path.Fill = GetCachedBrush(fill); } - - e.Width = rect.Width; - e.Height = rect.Height; - Canvas.SetLeft(e, rect.Left); - Canvas.SetTop(e, rect.Top); + var ellipseGeometry = new EllipseGeometry(ToRect(rect)); + path.Data = ellipseGeometry; } /// @@ -346,6 +343,11 @@ public void DrawPolygons( } } + if (usg) + { + sgc.EndFigure(true); + } + count++; // Must limit the number of figures, otherwise drawing errors... @@ -389,18 +391,14 @@ public void DrawPolygons( /// The stroke thickness (in device independent units, 1/96 inch). public void DrawRectangle(OxyRect rect, OxyColor fill, OxyColor stroke, double thickness) { - var e = CreateAndAdd(rect.Left, rect.Top); - SetStroke(e, stroke, thickness, LineJoin.Miter, null, 0, true); - + var path = CreateAndAdd(); + SetStroke(path, stroke, thickness); if (!fill.IsUndefined()) { - e.Fill = GetCachedBrush(fill); + path.Fill = GetCachedBrush(fill); } - - e.Width = rect.Width; - e.Height = rect.Height; - Canvas.SetLeft(e, rect.Left); - Canvas.SetTop(e, rect.Top); + var rectangleGeometry = new RectangleGeometry(ToRect(rect)); + path.Data = rectangleGeometry; } /// @@ -413,10 +411,8 @@ public void DrawRectangle(OxyRect rect, OxyColor fill, OxyColor stroke, double t /// The stroke thickness (in device independent units, 1/96 inch). public void DrawRectangles(IList rectangles, OxyColor fill, OxyColor stroke, double thickness) { - foreach (var rect in rectangles) - { - DrawRectangle(rect, fill, stroke, thickness); - } + var polys = rectangles.Select(r => (IList)RectangleToPolygon(r)).ToList(); + DrawPolygons(polys, fill, stroke, thickness, null, LineJoin.Miter, false); } /// @@ -845,7 +841,8 @@ private void SetStroke( if (dashArray != null) { - shape.StrokeDashArray = new global::Avalonia.Collections.AvaloniaList(dashArray.Select(dash => dash + dashOffset)); + shape.StrokeDashArray = new global::Avalonia.Collections.AvaloniaList(dashArray.Select(dash => dash)); + shape.StrokeDashOffset = dashOffset; } } } @@ -1020,5 +1017,22 @@ private static List ToPointCollection(IEnumerable points, bo { return new List(aliased ? points.Select(ToPixelAlignedPoint) : points.Select(ToPoint)); } + + /// + /// Converts an to an array of . + /// + /// The rectangle. + /// A . + private static ScreenPoint[] RectangleToPolygon(OxyRect rect) + { + return new ScreenPoint[] + { + rect.BottomLeft, + rect.TopLeft, + rect.TopRight, + rect.BottomRight, + }; + } + } } \ No newline at end of file