From f71d6b64c1085a6b0d1aaaf221e1c56248eaf1dc Mon Sep 17 00:00:00 2001 From: Jumar Macato Date: Thu, 18 Mar 2021 10:30:46 +0800 Subject: [PATCH 01/38] add new Path measurement API on IGeometryImpl.cs --- .../Platform/IGeometryImpl.cs | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/Avalonia.Visuals/Platform/IGeometryImpl.cs b/src/Avalonia.Visuals/Platform/IGeometryImpl.cs index 7490ad912a..5c39afa846 100644 --- a/src/Avalonia.Visuals/Platform/IGeometryImpl.cs +++ b/src/Avalonia.Visuals/Platform/IGeometryImpl.cs @@ -11,6 +11,12 @@ namespace Avalonia.Platform /// Gets the geometry's bounding rectangle. /// Rect Bounds { get; } + + /// + /// Gets the geometry's total length as if all its contours are placed + /// in a straight line. + /// + double ContourLength { get; } /// /// Gets the geometry's bounding rectangle with the specified pen. @@ -47,5 +53,25 @@ namespace Avalonia.Platform /// The transform. /// The cloned geometry. ITransformedGeometryImpl WithTransform(Matrix transform); + + /// + /// Attempts to get the corresponding point from the + /// specified distance + /// + /// The contour distance to get from. + /// The point in the specified distance. + /// If there's valid point at the specified distance. + bool TryGetPointAtDistance(double distance, out Point point); + + /// + /// Attempts to get the corresponding point and + /// tangent from the specified distance along the + /// contour of the geometry. + /// + /// The contour distance to get from. + /// The point in the specified distance. + /// The tangent in the specified distance. + /// If there's valid point and tangent at the specified distance. + bool TryGetPositionAndTangentAtDistance (double distance, out Point position, out Point tangent); } } From ceb170e472cebb4ec1f60243deb9a91b1ee39722 Mon Sep 17 00:00:00 2001 From: Jumar Macato Date: Thu, 18 Mar 2021 11:01:02 +0800 Subject: [PATCH 02/38] add apicompat for the new api --- src/Avalonia.Controls.DataGrid/ApiCompatBaseline.txt | 1 + src/Avalonia.DesignerSupport/ApiCompatBaseline.txt | 1 + src/Avalonia.Desktop/ApiCompatBaseline.txt | 1 + src/Avalonia.DesktopRuntime/ApiCompatBaseline.txt | 1 + src/Avalonia.Diagnostics/ApiCompatBaseline.txt | 1 + src/Avalonia.Dialogs/ApiCompatBaseline.txt | 1 + src/Avalonia.Interactivity/ApiCompatBaseline.txt | 1 + src/Avalonia.Layout/ApiCompatBaseline.txt | 1 + src/Avalonia.Themes.Default/ApiCompatBaseline.txt | 1 + src/Avalonia.Themes.Fluent/ApiCompatBaseline.txt | 1 + src/Avalonia.Visuals/ApiCompatBaseline.txt | 6 ++++++ src/Markup/Avalonia.Markup.Xaml/ApiCompatBaseline.txt | 1 + src/Markup/Avalonia.Markup/ApiCompatBaseline.txt | 1 + 13 files changed, 18 insertions(+) create mode 100644 src/Avalonia.Controls.DataGrid/ApiCompatBaseline.txt create mode 100644 src/Avalonia.DesignerSupport/ApiCompatBaseline.txt create mode 100644 src/Avalonia.Desktop/ApiCompatBaseline.txt create mode 100644 src/Avalonia.DesktopRuntime/ApiCompatBaseline.txt create mode 100644 src/Avalonia.Diagnostics/ApiCompatBaseline.txt create mode 100644 src/Avalonia.Dialogs/ApiCompatBaseline.txt create mode 100644 src/Avalonia.Interactivity/ApiCompatBaseline.txt create mode 100644 src/Avalonia.Layout/ApiCompatBaseline.txt create mode 100644 src/Avalonia.Themes.Default/ApiCompatBaseline.txt create mode 100644 src/Avalonia.Themes.Fluent/ApiCompatBaseline.txt create mode 100644 src/Avalonia.Visuals/ApiCompatBaseline.txt create mode 100644 src/Markup/Avalonia.Markup.Xaml/ApiCompatBaseline.txt create mode 100644 src/Markup/Avalonia.Markup/ApiCompatBaseline.txt diff --git a/src/Avalonia.Controls.DataGrid/ApiCompatBaseline.txt b/src/Avalonia.Controls.DataGrid/ApiCompatBaseline.txt new file mode 100644 index 0000000000..fcc74cf864 --- /dev/null +++ b/src/Avalonia.Controls.DataGrid/ApiCompatBaseline.txt @@ -0,0 +1 @@ +Total Issues: 0 diff --git a/src/Avalonia.DesignerSupport/ApiCompatBaseline.txt b/src/Avalonia.DesignerSupport/ApiCompatBaseline.txt new file mode 100644 index 0000000000..fcc74cf864 --- /dev/null +++ b/src/Avalonia.DesignerSupport/ApiCompatBaseline.txt @@ -0,0 +1 @@ +Total Issues: 0 diff --git a/src/Avalonia.Desktop/ApiCompatBaseline.txt b/src/Avalonia.Desktop/ApiCompatBaseline.txt new file mode 100644 index 0000000000..fcc74cf864 --- /dev/null +++ b/src/Avalonia.Desktop/ApiCompatBaseline.txt @@ -0,0 +1 @@ +Total Issues: 0 diff --git a/src/Avalonia.DesktopRuntime/ApiCompatBaseline.txt b/src/Avalonia.DesktopRuntime/ApiCompatBaseline.txt new file mode 100644 index 0000000000..fcc74cf864 --- /dev/null +++ b/src/Avalonia.DesktopRuntime/ApiCompatBaseline.txt @@ -0,0 +1 @@ +Total Issues: 0 diff --git a/src/Avalonia.Diagnostics/ApiCompatBaseline.txt b/src/Avalonia.Diagnostics/ApiCompatBaseline.txt new file mode 100644 index 0000000000..fcc74cf864 --- /dev/null +++ b/src/Avalonia.Diagnostics/ApiCompatBaseline.txt @@ -0,0 +1 @@ +Total Issues: 0 diff --git a/src/Avalonia.Dialogs/ApiCompatBaseline.txt b/src/Avalonia.Dialogs/ApiCompatBaseline.txt new file mode 100644 index 0000000000..fcc74cf864 --- /dev/null +++ b/src/Avalonia.Dialogs/ApiCompatBaseline.txt @@ -0,0 +1 @@ +Total Issues: 0 diff --git a/src/Avalonia.Interactivity/ApiCompatBaseline.txt b/src/Avalonia.Interactivity/ApiCompatBaseline.txt new file mode 100644 index 0000000000..fcc74cf864 --- /dev/null +++ b/src/Avalonia.Interactivity/ApiCompatBaseline.txt @@ -0,0 +1 @@ +Total Issues: 0 diff --git a/src/Avalonia.Layout/ApiCompatBaseline.txt b/src/Avalonia.Layout/ApiCompatBaseline.txt new file mode 100644 index 0000000000..fcc74cf864 --- /dev/null +++ b/src/Avalonia.Layout/ApiCompatBaseline.txt @@ -0,0 +1 @@ +Total Issues: 0 diff --git a/src/Avalonia.Themes.Default/ApiCompatBaseline.txt b/src/Avalonia.Themes.Default/ApiCompatBaseline.txt new file mode 100644 index 0000000000..fcc74cf864 --- /dev/null +++ b/src/Avalonia.Themes.Default/ApiCompatBaseline.txt @@ -0,0 +1 @@ +Total Issues: 0 diff --git a/src/Avalonia.Themes.Fluent/ApiCompatBaseline.txt b/src/Avalonia.Themes.Fluent/ApiCompatBaseline.txt new file mode 100644 index 0000000000..fcc74cf864 --- /dev/null +++ b/src/Avalonia.Themes.Fluent/ApiCompatBaseline.txt @@ -0,0 +1 @@ +Total Issues: 0 diff --git a/src/Avalonia.Visuals/ApiCompatBaseline.txt b/src/Avalonia.Visuals/ApiCompatBaseline.txt new file mode 100644 index 0000000000..f51f26974e --- /dev/null +++ b/src/Avalonia.Visuals/ApiCompatBaseline.txt @@ -0,0 +1,6 @@ +Compat issues with assembly Avalonia.Visuals: +InterfacesShouldHaveSameMembers : Interface member 'public System.Double Avalonia.Platform.IGeometryImpl.ContourLength' is present in the implementation but not in the contract. +InterfacesShouldHaveSameMembers : Interface member 'public System.Double Avalonia.Platform.IGeometryImpl.ContourLength.get()' is present in the implementation but not in the contract. +InterfacesShouldHaveSameMembers : Interface member 'public System.Boolean Avalonia.Platform.IGeometryImpl.TryGetPointAndTangentAtDistance(System.Double, Avalonia.Point, Avalonia.Point)' is present in the implementation but not in the contract. +InterfacesShouldHaveSameMembers : Interface member 'public System.Boolean Avalonia.Platform.IGeometryImpl.TryGetPointAtDistance(System.Double, Avalonia.Point)' is present in the implementation but not in the contract. +Total Issues: 4 diff --git a/src/Markup/Avalonia.Markup.Xaml/ApiCompatBaseline.txt b/src/Markup/Avalonia.Markup.Xaml/ApiCompatBaseline.txt new file mode 100644 index 0000000000..fcc74cf864 --- /dev/null +++ b/src/Markup/Avalonia.Markup.Xaml/ApiCompatBaseline.txt @@ -0,0 +1 @@ +Total Issues: 0 diff --git a/src/Markup/Avalonia.Markup/ApiCompatBaseline.txt b/src/Markup/Avalonia.Markup/ApiCompatBaseline.txt new file mode 100644 index 0000000000..fcc74cf864 --- /dev/null +++ b/src/Markup/Avalonia.Markup/ApiCompatBaseline.txt @@ -0,0 +1 @@ +Total Issues: 0 From d7f149914d8ca307e4859fa4c4f3f5bad8173693 Mon Sep 17 00:00:00 2001 From: Jumar Macato Date: Thu, 18 Mar 2021 11:17:57 +0800 Subject: [PATCH 03/38] change api again --- src/Avalonia.Visuals/Platform/IGeometryImpl.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Avalonia.Visuals/Platform/IGeometryImpl.cs b/src/Avalonia.Visuals/Platform/IGeometryImpl.cs index 5c39afa846..c61e6c6112 100644 --- a/src/Avalonia.Visuals/Platform/IGeometryImpl.cs +++ b/src/Avalonia.Visuals/Platform/IGeometryImpl.cs @@ -72,6 +72,6 @@ namespace Avalonia.Platform /// The point in the specified distance. /// The tangent in the specified distance. /// If there's valid point and tangent at the specified distance. - bool TryGetPositionAndTangentAtDistance (double distance, out Point position, out Point tangent); + bool TryGetPointAndTangentAtDistance(double distance, out Point point, out Point tangent); } } From c6581cf02023c99b21e64cd973d2490576563136 Mon Sep 17 00:00:00 2001 From: Jumar Macato Date: Thu, 18 Mar 2021 11:22:55 +0800 Subject: [PATCH 04/38] api compat update --- src/Avalonia.Animation/ApiCompatBaseline.txt | 1 + src/Avalonia.Base/ApiCompatBaseline.txt | 1 + src/Avalonia.Remote.Protocol/ApiCompatBaseline.txt | 1 + 3 files changed, 3 insertions(+) create mode 100644 src/Avalonia.Animation/ApiCompatBaseline.txt create mode 100644 src/Avalonia.Base/ApiCompatBaseline.txt create mode 100644 src/Avalonia.Remote.Protocol/ApiCompatBaseline.txt diff --git a/src/Avalonia.Animation/ApiCompatBaseline.txt b/src/Avalonia.Animation/ApiCompatBaseline.txt new file mode 100644 index 0000000000..fcc74cf864 --- /dev/null +++ b/src/Avalonia.Animation/ApiCompatBaseline.txt @@ -0,0 +1 @@ +Total Issues: 0 diff --git a/src/Avalonia.Base/ApiCompatBaseline.txt b/src/Avalonia.Base/ApiCompatBaseline.txt new file mode 100644 index 0000000000..fcc74cf864 --- /dev/null +++ b/src/Avalonia.Base/ApiCompatBaseline.txt @@ -0,0 +1 @@ +Total Issues: 0 diff --git a/src/Avalonia.Remote.Protocol/ApiCompatBaseline.txt b/src/Avalonia.Remote.Protocol/ApiCompatBaseline.txt new file mode 100644 index 0000000000..fcc74cf864 --- /dev/null +++ b/src/Avalonia.Remote.Protocol/ApiCompatBaseline.txt @@ -0,0 +1 @@ +Total Issues: 0 From 60d111b074fa51c61d0f83d284deadd5adf9a869 Mon Sep 17 00:00:00 2001 From: Jumar Macato Date: Thu, 18 Mar 2021 11:24:12 +0800 Subject: [PATCH 05/38] add skia implementation --- src/Skia/Avalonia.Skia/GeometryImpl.cs | 43 +++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/src/Skia/Avalonia.Skia/GeometryImpl.cs b/src/Skia/Avalonia.Skia/GeometryImpl.cs index 879b18742e..e8b5795f60 100644 --- a/src/Skia/Avalonia.Skia/GeometryImpl.cs +++ b/src/Skia/Avalonia.Skia/GeometryImpl.cs @@ -11,9 +11,20 @@ namespace Avalonia.Skia internal abstract class GeometryImpl : IGeometryImpl { private PathCache _pathCache; - + /// public abstract Rect Bounds { get; } + + /// + public double ContourLength + { + get + { + if (EffectivePath is null) return 0; + return new SKPathMeasure(EffectivePath).Length; + } + } + public abstract SKPath EffectivePath { get; } /// @@ -103,6 +114,36 @@ namespace Avalonia.Skia { return new TransformedGeometryImpl(this, transform); } + + /// + public bool TryGetPointAtDistance(double distance, out Point point) + { + if (EffectivePath is null) + { + point = new Point(); + return false; + } + + var res = new SKPathMeasure(EffectivePath).GetPosition((float)distance, out var skPoint); + point = new Point(skPoint.X, skPoint.Y); + return res; + } + + /// + public bool TryGetPointAndTangentAtDistance(double distance, out Point point, out Point tangent) + { + if (EffectivePath is null) + { + point = new Point(); + tangent = new Point(); + return false; + } + + var res = new SKPathMeasure(EffectivePath).GetPositionAndTangent((float)distance, out var skPoint, out var skTangent); + point = new Point(skPoint.X, skPoint.Y); + tangent = new Point(skTangent.X, skTangent.Y); + return res; + } /// /// Invalidate all caches. Call after chaining path contents. From 38b322b02b5aa2b62d86038ba97bc2484a2e987c Mon Sep 17 00:00:00 2001 From: Jumar Macato Date: Thu, 18 Mar 2021 11:24:22 +0800 Subject: [PATCH 06/38] add partial d2d implementation --- .../Avalonia.Direct2D1/Media/GeometryImpl.cs | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/Windows/Avalonia.Direct2D1/Media/GeometryImpl.cs b/src/Windows/Avalonia.Direct2D1/Media/GeometryImpl.cs index d04e2b3110..81c21c8648 100644 --- a/src/Windows/Avalonia.Direct2D1/Media/GeometryImpl.cs +++ b/src/Windows/Avalonia.Direct2D1/Media/GeometryImpl.cs @@ -1,3 +1,4 @@ +using Avalonia.Logging; using Avalonia.Platform; using SharpDX.Direct2D1; @@ -8,6 +9,8 @@ namespace Avalonia.Direct2D1.Media /// public abstract class GeometryImpl : IGeometryImpl { + private const float ContourApproximation = 0.0001f; + public GeometryImpl(Geometry geometry) { Geometry = geometry; @@ -16,6 +19,9 @@ namespace Avalonia.Direct2D1.Media /// public Rect Bounds => Geometry.GetWidenedBounds(0).ToAvalonia(); + /// + public double ContourLength => Geometry.ComputeLength(null, ContourApproximation); + public Geometry Geometry { get; } /// @@ -57,6 +63,24 @@ namespace Avalonia.Direct2D1.Media transform.ToDirect2D()), this); } + + /// + public bool TryGetPointAtDistance(double distance, out Point point) + { + Geometry.ComputePointAtLength((float)distance, ContourApproximation, out var tangentVector); + point = new Point(tangentVector.X, tangentVector.Y); + return true; + } + + /// + public bool TryGetPointAndTangentAtDistance(double distance, out Point point, out Point tangent) + { + // Direct2D doesnt have this sadly. + Logger.TryGet(LogEventLevel.Warning, LogArea.Visual)?.Log(this, "TryGetPointAndTangentAtDistance is not available in Direct2D."); + point = new Point(); + tangent = new Point(); + return false; + } protected virtual Geometry GetSourceGeometry() => Geometry; } From 386a318abe2cdc4811832d6f68f56e232217b06b Mon Sep 17 00:00:00 2001 From: Jumar Macato Date: Thu, 18 Mar 2021 11:24:37 +0800 Subject: [PATCH 07/38] add stubs for headless/mock --- .../HeadlessPlatformRenderInterface.cs | 16 ++++++++++++++++ .../Avalonia.UnitTests/MockStreamGeometryImpl.cs | 15 +++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/src/Avalonia.Headless/HeadlessPlatformRenderInterface.cs b/src/Avalonia.Headless/HeadlessPlatformRenderInterface.cs index 6a78f4c6e7..ce5a8a2e86 100644 --- a/src/Avalonia.Headless/HeadlessPlatformRenderInterface.cs +++ b/src/Avalonia.Headless/HeadlessPlatformRenderInterface.cs @@ -104,6 +104,9 @@ namespace Avalonia.Headless } public Rect Bounds { get; set; } + + public double ContourLength { get; } = 0; + public virtual bool FillContains(Point point) => Bounds.Contains(point); public Rect GetRenderBounds(IPen pen) @@ -126,6 +129,19 @@ namespace Avalonia.Headless public ITransformedGeometryImpl WithTransform(Matrix transform) => new HeadlessTransformedGeometryStub(this, transform); + + public bool TryGetPointAtDistance(double distance, out Point point) + { + point = new Point(); + return false; + } + + public bool TryGetPointAndTangentAtDistance(double distance, out Point point, out Point tangent) + { + point = new Point(); + tangent = new Point(); + return false; + } } class HeadlessTransformedGeometryStub : HeadlessGeometryStub, ITransformedGeometryImpl diff --git a/tests/Avalonia.UnitTests/MockStreamGeometryImpl.cs b/tests/Avalonia.UnitTests/MockStreamGeometryImpl.cs index 4fa3fbf523..4e66eaeb24 100644 --- a/tests/Avalonia.UnitTests/MockStreamGeometryImpl.cs +++ b/tests/Avalonia.UnitTests/MockStreamGeometryImpl.cs @@ -30,6 +30,8 @@ namespace Avalonia.UnitTests public IGeometryImpl SourceGeometry { get; } public Rect Bounds => _context.CalculateBounds(); + + public double ContourLength { get; } public Matrix Transform { get; } @@ -69,6 +71,19 @@ namespace Avalonia.UnitTests return new MockStreamGeometryImpl(transform, _context); } + public bool TryGetPointAtDistance(double distance, out Point point) + { + point = new Point(); + return false; + } + + public bool TryGetPointAndTangentAtDistance(double distance, out Point point, out Point tangent) + { + point = new Point(); + tangent = new Point(); + return false; + } + class MockStreamGeometryContext : IStreamGeometryContextImpl { private List points = new List(); From ce655d715cb197ba3bb62bb0e56cc8ebf7eac7fe Mon Sep 17 00:00:00 2001 From: Jumar Macato <16554748+jmacato@users.noreply.github.com> Date: Thu, 18 Mar 2021 11:43:17 +0800 Subject: [PATCH 08/38] Apply suggestions from code review --- src/Avalonia.DesignerSupport/ApiCompatBaseline.txt | 2 +- src/Avalonia.Desktop/ApiCompatBaseline.txt | 2 +- src/Avalonia.DesktopRuntime/ApiCompatBaseline.txt | 2 +- src/Avalonia.Diagnostics/ApiCompatBaseline.txt | 2 +- src/Avalonia.Dialogs/ApiCompatBaseline.txt | 2 +- src/Avalonia.Interactivity/ApiCompatBaseline.txt | 2 +- src/Avalonia.Layout/ApiCompatBaseline.txt | 2 +- src/Avalonia.Remote.Protocol/ApiCompatBaseline.txt | 2 +- src/Avalonia.Themes.Default/ApiCompatBaseline.txt | 2 +- src/Avalonia.Themes.Fluent/ApiCompatBaseline.txt | 2 +- src/Markup/Avalonia.Markup.Xaml/ApiCompatBaseline.txt | 2 +- src/Markup/Avalonia.Markup/ApiCompatBaseline.txt | 2 +- 12 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/Avalonia.DesignerSupport/ApiCompatBaseline.txt b/src/Avalonia.DesignerSupport/ApiCompatBaseline.txt index fcc74cf864..8b13789179 100644 --- a/src/Avalonia.DesignerSupport/ApiCompatBaseline.txt +++ b/src/Avalonia.DesignerSupport/ApiCompatBaseline.txt @@ -1 +1 @@ -Total Issues: 0 + diff --git a/src/Avalonia.Desktop/ApiCompatBaseline.txt b/src/Avalonia.Desktop/ApiCompatBaseline.txt index fcc74cf864..8b13789179 100644 --- a/src/Avalonia.Desktop/ApiCompatBaseline.txt +++ b/src/Avalonia.Desktop/ApiCompatBaseline.txt @@ -1 +1 @@ -Total Issues: 0 + diff --git a/src/Avalonia.DesktopRuntime/ApiCompatBaseline.txt b/src/Avalonia.DesktopRuntime/ApiCompatBaseline.txt index fcc74cf864..8b13789179 100644 --- a/src/Avalonia.DesktopRuntime/ApiCompatBaseline.txt +++ b/src/Avalonia.DesktopRuntime/ApiCompatBaseline.txt @@ -1 +1 @@ -Total Issues: 0 + diff --git a/src/Avalonia.Diagnostics/ApiCompatBaseline.txt b/src/Avalonia.Diagnostics/ApiCompatBaseline.txt index fcc74cf864..8b13789179 100644 --- a/src/Avalonia.Diagnostics/ApiCompatBaseline.txt +++ b/src/Avalonia.Diagnostics/ApiCompatBaseline.txt @@ -1 +1 @@ -Total Issues: 0 + diff --git a/src/Avalonia.Dialogs/ApiCompatBaseline.txt b/src/Avalonia.Dialogs/ApiCompatBaseline.txt index fcc74cf864..8b13789179 100644 --- a/src/Avalonia.Dialogs/ApiCompatBaseline.txt +++ b/src/Avalonia.Dialogs/ApiCompatBaseline.txt @@ -1 +1 @@ -Total Issues: 0 + diff --git a/src/Avalonia.Interactivity/ApiCompatBaseline.txt b/src/Avalonia.Interactivity/ApiCompatBaseline.txt index fcc74cf864..8b13789179 100644 --- a/src/Avalonia.Interactivity/ApiCompatBaseline.txt +++ b/src/Avalonia.Interactivity/ApiCompatBaseline.txt @@ -1 +1 @@ -Total Issues: 0 + diff --git a/src/Avalonia.Layout/ApiCompatBaseline.txt b/src/Avalonia.Layout/ApiCompatBaseline.txt index fcc74cf864..8b13789179 100644 --- a/src/Avalonia.Layout/ApiCompatBaseline.txt +++ b/src/Avalonia.Layout/ApiCompatBaseline.txt @@ -1 +1 @@ -Total Issues: 0 + diff --git a/src/Avalonia.Remote.Protocol/ApiCompatBaseline.txt b/src/Avalonia.Remote.Protocol/ApiCompatBaseline.txt index fcc74cf864..8b13789179 100644 --- a/src/Avalonia.Remote.Protocol/ApiCompatBaseline.txt +++ b/src/Avalonia.Remote.Protocol/ApiCompatBaseline.txt @@ -1 +1 @@ -Total Issues: 0 + diff --git a/src/Avalonia.Themes.Default/ApiCompatBaseline.txt b/src/Avalonia.Themes.Default/ApiCompatBaseline.txt index fcc74cf864..8b13789179 100644 --- a/src/Avalonia.Themes.Default/ApiCompatBaseline.txt +++ b/src/Avalonia.Themes.Default/ApiCompatBaseline.txt @@ -1 +1 @@ -Total Issues: 0 + diff --git a/src/Avalonia.Themes.Fluent/ApiCompatBaseline.txt b/src/Avalonia.Themes.Fluent/ApiCompatBaseline.txt index fcc74cf864..8b13789179 100644 --- a/src/Avalonia.Themes.Fluent/ApiCompatBaseline.txt +++ b/src/Avalonia.Themes.Fluent/ApiCompatBaseline.txt @@ -1 +1 @@ -Total Issues: 0 + diff --git a/src/Markup/Avalonia.Markup.Xaml/ApiCompatBaseline.txt b/src/Markup/Avalonia.Markup.Xaml/ApiCompatBaseline.txt index fcc74cf864..8b13789179 100644 --- a/src/Markup/Avalonia.Markup.Xaml/ApiCompatBaseline.txt +++ b/src/Markup/Avalonia.Markup.Xaml/ApiCompatBaseline.txt @@ -1 +1 @@ -Total Issues: 0 + diff --git a/src/Markup/Avalonia.Markup/ApiCompatBaseline.txt b/src/Markup/Avalonia.Markup/ApiCompatBaseline.txt index fcc74cf864..8b13789179 100644 --- a/src/Markup/Avalonia.Markup/ApiCompatBaseline.txt +++ b/src/Markup/Avalonia.Markup/ApiCompatBaseline.txt @@ -1 +1 @@ -Total Issues: 0 + From 518d4022c85d33a8a12d08430c1b5cf2a6d6e80f Mon Sep 17 00:00:00 2001 From: Jumar Macato <16554748+jmacato@users.noreply.github.com> Date: Thu, 18 Mar 2021 11:44:01 +0800 Subject: [PATCH 09/38] Delete ApiCompatBaseline.txt --- src/Avalonia.Animation/ApiCompatBaseline.txt | 1 - src/Avalonia.Base/ApiCompatBaseline.txt | 1 - src/Avalonia.Controls.DataGrid/ApiCompatBaseline.txt | 1 - src/Avalonia.DesignerSupport/ApiCompatBaseline.txt | 1 - src/Avalonia.Desktop/ApiCompatBaseline.txt | 1 - src/Avalonia.DesktopRuntime/ApiCompatBaseline.txt | 1 - src/Avalonia.Diagnostics/ApiCompatBaseline.txt | 1 - src/Avalonia.Dialogs/ApiCompatBaseline.txt | 1 - src/Avalonia.Interactivity/ApiCompatBaseline.txt | 1 - src/Avalonia.Layout/ApiCompatBaseline.txt | 1 - src/Avalonia.Remote.Protocol/ApiCompatBaseline.txt | 1 - src/Avalonia.Themes.Default/ApiCompatBaseline.txt | 1 - src/Avalonia.Themes.Fluent/ApiCompatBaseline.txt | 1 - src/Markup/Avalonia.Markup.Xaml/ApiCompatBaseline.txt | 1 - src/Markup/Avalonia.Markup/ApiCompatBaseline.txt | 1 - 15 files changed, 15 deletions(-) delete mode 100644 src/Avalonia.Animation/ApiCompatBaseline.txt delete mode 100644 src/Avalonia.Base/ApiCompatBaseline.txt delete mode 100644 src/Avalonia.Controls.DataGrid/ApiCompatBaseline.txt delete mode 100644 src/Avalonia.DesignerSupport/ApiCompatBaseline.txt delete mode 100644 src/Avalonia.Desktop/ApiCompatBaseline.txt delete mode 100644 src/Avalonia.DesktopRuntime/ApiCompatBaseline.txt delete mode 100644 src/Avalonia.Diagnostics/ApiCompatBaseline.txt delete mode 100644 src/Avalonia.Dialogs/ApiCompatBaseline.txt delete mode 100644 src/Avalonia.Interactivity/ApiCompatBaseline.txt delete mode 100644 src/Avalonia.Layout/ApiCompatBaseline.txt delete mode 100644 src/Avalonia.Remote.Protocol/ApiCompatBaseline.txt delete mode 100644 src/Avalonia.Themes.Default/ApiCompatBaseline.txt delete mode 100644 src/Avalonia.Themes.Fluent/ApiCompatBaseline.txt delete mode 100644 src/Markup/Avalonia.Markup.Xaml/ApiCompatBaseline.txt delete mode 100644 src/Markup/Avalonia.Markup/ApiCompatBaseline.txt diff --git a/src/Avalonia.Animation/ApiCompatBaseline.txt b/src/Avalonia.Animation/ApiCompatBaseline.txt deleted file mode 100644 index fcc74cf864..0000000000 --- a/src/Avalonia.Animation/ApiCompatBaseline.txt +++ /dev/null @@ -1 +0,0 @@ -Total Issues: 0 diff --git a/src/Avalonia.Base/ApiCompatBaseline.txt b/src/Avalonia.Base/ApiCompatBaseline.txt deleted file mode 100644 index fcc74cf864..0000000000 --- a/src/Avalonia.Base/ApiCompatBaseline.txt +++ /dev/null @@ -1 +0,0 @@ -Total Issues: 0 diff --git a/src/Avalonia.Controls.DataGrid/ApiCompatBaseline.txt b/src/Avalonia.Controls.DataGrid/ApiCompatBaseline.txt deleted file mode 100644 index fcc74cf864..0000000000 --- a/src/Avalonia.Controls.DataGrid/ApiCompatBaseline.txt +++ /dev/null @@ -1 +0,0 @@ -Total Issues: 0 diff --git a/src/Avalonia.DesignerSupport/ApiCompatBaseline.txt b/src/Avalonia.DesignerSupport/ApiCompatBaseline.txt deleted file mode 100644 index 8b13789179..0000000000 --- a/src/Avalonia.DesignerSupport/ApiCompatBaseline.txt +++ /dev/null @@ -1 +0,0 @@ - diff --git a/src/Avalonia.Desktop/ApiCompatBaseline.txt b/src/Avalonia.Desktop/ApiCompatBaseline.txt deleted file mode 100644 index 8b13789179..0000000000 --- a/src/Avalonia.Desktop/ApiCompatBaseline.txt +++ /dev/null @@ -1 +0,0 @@ - diff --git a/src/Avalonia.DesktopRuntime/ApiCompatBaseline.txt b/src/Avalonia.DesktopRuntime/ApiCompatBaseline.txt deleted file mode 100644 index 8b13789179..0000000000 --- a/src/Avalonia.DesktopRuntime/ApiCompatBaseline.txt +++ /dev/null @@ -1 +0,0 @@ - diff --git a/src/Avalonia.Diagnostics/ApiCompatBaseline.txt b/src/Avalonia.Diagnostics/ApiCompatBaseline.txt deleted file mode 100644 index 8b13789179..0000000000 --- a/src/Avalonia.Diagnostics/ApiCompatBaseline.txt +++ /dev/null @@ -1 +0,0 @@ - diff --git a/src/Avalonia.Dialogs/ApiCompatBaseline.txt b/src/Avalonia.Dialogs/ApiCompatBaseline.txt deleted file mode 100644 index 8b13789179..0000000000 --- a/src/Avalonia.Dialogs/ApiCompatBaseline.txt +++ /dev/null @@ -1 +0,0 @@ - diff --git a/src/Avalonia.Interactivity/ApiCompatBaseline.txt b/src/Avalonia.Interactivity/ApiCompatBaseline.txt deleted file mode 100644 index 8b13789179..0000000000 --- a/src/Avalonia.Interactivity/ApiCompatBaseline.txt +++ /dev/null @@ -1 +0,0 @@ - diff --git a/src/Avalonia.Layout/ApiCompatBaseline.txt b/src/Avalonia.Layout/ApiCompatBaseline.txt deleted file mode 100644 index 8b13789179..0000000000 --- a/src/Avalonia.Layout/ApiCompatBaseline.txt +++ /dev/null @@ -1 +0,0 @@ - diff --git a/src/Avalonia.Remote.Protocol/ApiCompatBaseline.txt b/src/Avalonia.Remote.Protocol/ApiCompatBaseline.txt deleted file mode 100644 index 8b13789179..0000000000 --- a/src/Avalonia.Remote.Protocol/ApiCompatBaseline.txt +++ /dev/null @@ -1 +0,0 @@ - diff --git a/src/Avalonia.Themes.Default/ApiCompatBaseline.txt b/src/Avalonia.Themes.Default/ApiCompatBaseline.txt deleted file mode 100644 index 8b13789179..0000000000 --- a/src/Avalonia.Themes.Default/ApiCompatBaseline.txt +++ /dev/null @@ -1 +0,0 @@ - diff --git a/src/Avalonia.Themes.Fluent/ApiCompatBaseline.txt b/src/Avalonia.Themes.Fluent/ApiCompatBaseline.txt deleted file mode 100644 index 8b13789179..0000000000 --- a/src/Avalonia.Themes.Fluent/ApiCompatBaseline.txt +++ /dev/null @@ -1 +0,0 @@ - diff --git a/src/Markup/Avalonia.Markup.Xaml/ApiCompatBaseline.txt b/src/Markup/Avalonia.Markup.Xaml/ApiCompatBaseline.txt deleted file mode 100644 index 8b13789179..0000000000 --- a/src/Markup/Avalonia.Markup.Xaml/ApiCompatBaseline.txt +++ /dev/null @@ -1 +0,0 @@ - diff --git a/src/Markup/Avalonia.Markup/ApiCompatBaseline.txt b/src/Markup/Avalonia.Markup/ApiCompatBaseline.txt deleted file mode 100644 index 8b13789179..0000000000 --- a/src/Markup/Avalonia.Markup/ApiCompatBaseline.txt +++ /dev/null @@ -1 +0,0 @@ - From 88fef2fdd5f2ba6dc01d639de40bc65e571bc56b Mon Sep 17 00:00:00 2001 From: Jumar Macato Date: Thu, 18 Mar 2021 15:57:08 +0800 Subject: [PATCH 10/38] Add Bitmap Blending Mode API --- .../HeadlessPlatformRenderInterface.cs | 2 +- src/Avalonia.Visuals/ApiCompatBaseline.txt | 5 +- .../Media/Imaging/BitmapBlendingMode.cs | 57 +++++++++++++++++++ .../Platform/IDrawingContextImpl.cs | 3 +- .../SceneGraph/DeferredDrawingContextImpl.cs | 6 +- .../Rendering/SceneGraph/ImageNode.cs | 28 ++++++--- src/Skia/Avalonia.Skia/DrawingContextImpl.cs | 5 +- src/Skia/Avalonia.Skia/SkiaSharpExtensions.cs | 33 +++++++++++ .../Media/DrawingContextImpl.cs | 36 +++++++++++- 9 files changed, 156 insertions(+), 19 deletions(-) create mode 100644 src/Avalonia.Visuals/Media/Imaging/BitmapBlendingMode.cs diff --git a/src/Avalonia.Headless/HeadlessPlatformRenderInterface.cs b/src/Avalonia.Headless/HeadlessPlatformRenderInterface.cs index ce5a8a2e86..a00dc1f618 100644 --- a/src/Avalonia.Headless/HeadlessPlatformRenderInterface.cs +++ b/src/Avalonia.Headless/HeadlessPlatformRenderInterface.cs @@ -393,7 +393,7 @@ namespace Avalonia.Headless { } - public void DrawBitmap(IRef source, double opacity, Rect sourceRect, Rect destRect, BitmapInterpolationMode bitmapInterpolationMode = BitmapInterpolationMode.Default) + public void DrawBitmap(IRef source, double opacity, Rect sourceRect, Rect destRect, BitmapInterpolationMode bitmapInterpolationMode = BitmapInterpolationMode.Default, BitmapBlendingMode bitmapBlendingMode = BitmapBlendingMode.SourceOver) { } diff --git a/src/Avalonia.Visuals/ApiCompatBaseline.txt b/src/Avalonia.Visuals/ApiCompatBaseline.txt index f51f26974e..e781cb1d3e 100644 --- a/src/Avalonia.Visuals/ApiCompatBaseline.txt +++ b/src/Avalonia.Visuals/ApiCompatBaseline.txt @@ -1,6 +1,9 @@ Compat issues with assembly Avalonia.Visuals: +InterfacesShouldHaveSameMembers : Interface member 'public void Avalonia.Platform.IDrawingContextImpl.DrawBitmap(Avalonia.Utilities.IRef, System.Double, Avalonia.Rect, Avalonia.Rect, Avalonia.Visuals.Media.Imaging.BitmapInterpolationMode)' is present in the contract but not in the implementation. +MembersMustExist : Member 'public void Avalonia.Platform.IDrawingContextImpl.DrawBitmap(Avalonia.Utilities.IRef, System.Double, Avalonia.Rect, Avalonia.Rect, Avalonia.Visuals.Media.Imaging.BitmapInterpolationMode)' does not exist in the implementation but it does exist in the contract. +InterfacesShouldHaveSameMembers : Interface member 'public void Avalonia.Platform.IDrawingContextImpl.DrawBitmap(Avalonia.Utilities.IRef, System.Double, Avalonia.Rect, Avalonia.Rect, Avalonia.Visuals.Media.Imaging.BitmapInterpolationMode, Avalonia.Visuals.Media.Imaging.BitmapBlendingMode)' is present in the implementation but not in the contract. InterfacesShouldHaveSameMembers : Interface member 'public System.Double Avalonia.Platform.IGeometryImpl.ContourLength' is present in the implementation but not in the contract. InterfacesShouldHaveSameMembers : Interface member 'public System.Double Avalonia.Platform.IGeometryImpl.ContourLength.get()' is present in the implementation but not in the contract. InterfacesShouldHaveSameMembers : Interface member 'public System.Boolean Avalonia.Platform.IGeometryImpl.TryGetPointAndTangentAtDistance(System.Double, Avalonia.Point, Avalonia.Point)' is present in the implementation but not in the contract. InterfacesShouldHaveSameMembers : Interface member 'public System.Boolean Avalonia.Platform.IGeometryImpl.TryGetPointAtDistance(System.Double, Avalonia.Point)' is present in the implementation but not in the contract. -Total Issues: 4 +Total Issues: 7 diff --git a/src/Avalonia.Visuals/Media/Imaging/BitmapBlendingMode.cs b/src/Avalonia.Visuals/Media/Imaging/BitmapBlendingMode.cs new file mode 100644 index 0000000000..473b43dab3 --- /dev/null +++ b/src/Avalonia.Visuals/Media/Imaging/BitmapBlendingMode.cs @@ -0,0 +1,57 @@ +namespace Avalonia.Visuals.Media.Imaging +{ + /// + /// Controls the way the bitmaps are drawn together. + /// + public enum BitmapBlendingMode + { + /// + /// Source is placed over the destination. + /// + SourceOver, + /// + /// Only the source will be present. + /// + Source, + /// + /// Only the destination will be present. + /// + Destination, + /// + /// Destination is placed over the source. + /// + DestinationOver, + /// + /// The source that overlaps the destination, replaces the destination. + /// + SourceIn, + /// + /// Destination which overlaps the source, replaces the source. + /// + DestinationIn, + /// + /// Source is placed, where it falls outside of the destination. + /// + SourceOut, + /// + /// Destination is placed, where it falls outside of the source. + /// + DestinationOut, + /// + /// Source which overlaps the destination, replaces the destination. + /// + SourceAtop, + /// + /// Destination which overlaps the source replaces the source. + /// + DestinationAtop, + /// + /// The non-overlapping regions of source and destination are combined. + /// + Xor, + /// + /// Display the sum of the source image and destination image. + /// + Plus, + } +} diff --git a/src/Avalonia.Visuals/Platform/IDrawingContextImpl.cs b/src/Avalonia.Visuals/Platform/IDrawingContextImpl.cs index d6e88a7507..e9a1a2a6ae 100644 --- a/src/Avalonia.Visuals/Platform/IDrawingContextImpl.cs +++ b/src/Avalonia.Visuals/Platform/IDrawingContextImpl.cs @@ -30,7 +30,8 @@ namespace Avalonia.Platform /// The rect in the image to draw. /// The rect in the output to draw to. /// The bitmap interpolation mode. - void DrawBitmap(IRef source, double opacity, Rect sourceRect, Rect destRect, BitmapInterpolationMode bitmapInterpolationMode = BitmapInterpolationMode.Default); + /// The bitmap blending mode. + void DrawBitmap(IRef source, double opacity, Rect sourceRect, Rect destRect, BitmapInterpolationMode bitmapInterpolationMode = BitmapInterpolationMode.Default, BitmapBlendingMode bitmapBlendMode = BitmapBlendingMode.SourceOver); /// /// Draws a bitmap image. diff --git a/src/Avalonia.Visuals/Rendering/SceneGraph/DeferredDrawingContextImpl.cs b/src/Avalonia.Visuals/Rendering/SceneGraph/DeferredDrawingContextImpl.cs index 28f426266d..1705bc6b1c 100644 --- a/src/Avalonia.Visuals/Rendering/SceneGraph/DeferredDrawingContextImpl.cs +++ b/src/Avalonia.Visuals/Rendering/SceneGraph/DeferredDrawingContextImpl.cs @@ -112,13 +112,13 @@ namespace Avalonia.Rendering.SceneGraph } /// - public void DrawBitmap(IRef source, double opacity, Rect sourceRect, Rect destRect, BitmapInterpolationMode bitmapInterpolationMode) + public void DrawBitmap(IRef source, double opacity, Rect sourceRect, Rect destRect, BitmapInterpolationMode bitmapInterpolationMode, BitmapBlendingMode bitmapBlendingMode) { var next = NextDrawAs(); - if (next == null || !next.Item.Equals(Transform, source, opacity, sourceRect, destRect, bitmapInterpolationMode)) + if (next == null || !next.Item.Equals(Transform, source, opacity, sourceRect, destRect, bitmapInterpolationMode, bitmapBlendingMode)) { - Add(new ImageNode(Transform, source, opacity, sourceRect, destRect, bitmapInterpolationMode)); + Add(new ImageNode(Transform, source, opacity, sourceRect, destRect, bitmapInterpolationMode, bitmapBlendingMode)); } else { diff --git a/src/Avalonia.Visuals/Rendering/SceneGraph/ImageNode.cs b/src/Avalonia.Visuals/Rendering/SceneGraph/ImageNode.cs index c9052c6ef2..c50601120f 100644 --- a/src/Avalonia.Visuals/Rendering/SceneGraph/ImageNode.cs +++ b/src/Avalonia.Visuals/Rendering/SceneGraph/ImageNode.cs @@ -18,7 +18,7 @@ namespace Avalonia.Rendering.SceneGraph /// The source rect. /// The destination rect. /// The bitmap interpolation mode. - public ImageNode(Matrix transform, IRef source, double opacity, Rect sourceRect, Rect destRect, BitmapInterpolationMode bitmapInterpolationMode) + public ImageNode(Matrix transform, IRef source, double opacity, Rect sourceRect, Rect destRect, BitmapInterpolationMode bitmapInterpolationMode, BitmapBlendingMode bitmapBlendingMode) : base(destRect, transform) { Transform = transform; @@ -27,6 +27,7 @@ namespace Avalonia.Rendering.SceneGraph SourceRect = sourceRect; DestRect = destRect; BitmapInterpolationMode = bitmapInterpolationMode; + BitmapBlendingMode = bitmapBlendingMode; SourceVersion = Source.Item.Version; } @@ -67,6 +68,14 @@ namespace Avalonia.Rendering.SceneGraph /// The scaling mode. /// public BitmapInterpolationMode BitmapInterpolationMode { get; } + + /// + /// The bitmap blending mode. + /// + /// + /// The blending mode. + /// + public BitmapBlendingMode BitmapBlendingMode { get; } /// /// Determines if this draw operation equals another. @@ -82,22 +91,23 @@ namespace Avalonia.Rendering.SceneGraph /// The properties of the other draw operation are passed in as arguments to prevent /// allocation of a not-yet-constructed draw operation object. /// - public bool Equals(Matrix transform, IRef source, double opacity, Rect sourceRect, Rect destRect, BitmapInterpolationMode bitmapInterpolationMode) + public bool Equals(Matrix transform, IRef source, double opacity, Rect sourceRect, Rect destRect, BitmapInterpolationMode bitmapInterpolationMode, BitmapBlendingMode bitmapBlendingMode) { return transform == Transform && - Equals(source.Item, Source.Item) && - source.Item.Version == SourceVersion && - opacity == Opacity && - sourceRect == SourceRect && - destRect == DestRect && - bitmapInterpolationMode == BitmapInterpolationMode; + Equals(source.Item, Source.Item) && + source.Item.Version == SourceVersion && + opacity == Opacity && + sourceRect == SourceRect && + destRect == DestRect && + bitmapInterpolationMode == BitmapInterpolationMode && + bitmapBlendingMode == BitmapBlendingMode; } /// public override void Render(IDrawingContextImpl context) { context.Transform = Transform; - context.DrawBitmap(Source, Opacity, SourceRect, DestRect, BitmapInterpolationMode); + context.DrawBitmap(Source, Opacity, SourceRect, DestRect, BitmapInterpolationMode, BitmapBlendingMode); } /// diff --git a/src/Skia/Avalonia.Skia/DrawingContextImpl.cs b/src/Skia/Avalonia.Skia/DrawingContextImpl.cs index 2a79a4bb50..692d40a653 100644 --- a/src/Skia/Avalonia.Skia/DrawingContextImpl.cs +++ b/src/Skia/Avalonia.Skia/DrawingContextImpl.cs @@ -132,7 +132,7 @@ namespace Avalonia.Skia } /// - public void DrawBitmap(IRef source, double opacity, Rect sourceRect, Rect destRect, BitmapInterpolationMode bitmapInterpolationMode) + public void DrawBitmap(IRef source, double opacity, Rect sourceRect, Rect destRect, BitmapInterpolationMode bitmapInterpolationMode, BitmapBlendingMode bitmapBlendingMode) { var drawableImage = (IDrawableBitmapImpl)source.Item; var s = sourceRect.ToSKRect(); @@ -145,6 +145,7 @@ namespace Avalonia.Skia }) { paint.FilterQuality = bitmapInterpolationMode.ToSKFilterQuality(); + paint.BlendMode = bitmapBlendingMode.ToSKBlendMode(); drawableImage.Draw(this, s, d, paint); } @@ -154,7 +155,7 @@ namespace Avalonia.Skia public void DrawBitmap(IRef source, IBrush opacityMask, Rect opacityMaskRect, Rect destRect) { PushOpacityMask(opacityMask, opacityMaskRect); - DrawBitmap(source, 1, new Rect(0, 0, source.Item.PixelSize.Width, source.Item.PixelSize.Height), destRect, BitmapInterpolationMode.Default); + DrawBitmap(source, 1, new Rect(0, 0, source.Item.PixelSize.Width, source.Item.PixelSize.Height), destRect, BitmapInterpolationMode.Default, BitmapBlendingMode.SourceOver); PopOpacityMask(); } diff --git a/src/Skia/Avalonia.Skia/SkiaSharpExtensions.cs b/src/Skia/Avalonia.Skia/SkiaSharpExtensions.cs index bb3dbbfadc..75b4231640 100644 --- a/src/Skia/Avalonia.Skia/SkiaSharpExtensions.cs +++ b/src/Skia/Avalonia.Skia/SkiaSharpExtensions.cs @@ -25,6 +25,39 @@ namespace Avalonia.Skia } } + public static SKBlendMode ToSKBlendMode(this BitmapBlendingMode blendingMode) + { + switch (blendingMode) + { + case BitmapBlendingMode.SourceOver: + return SKBlendMode.SrcOver; + case BitmapBlendingMode.Source: + return SKBlendMode.Src; + case BitmapBlendingMode.SourceIn: + return SKBlendMode.SrcIn; + case BitmapBlendingMode.SourceOut: + return SKBlendMode.SrcOut; + case BitmapBlendingMode.SourceAtop: + return SKBlendMode.SrcATop; + case BitmapBlendingMode.Destination: + return SKBlendMode.Dst; + case BitmapBlendingMode.DestinationIn: + return SKBlendMode.DstIn; + case BitmapBlendingMode.DestinationOut: + return SKBlendMode.DstOut; + case BitmapBlendingMode.DestinationOver: + return SKBlendMode.DstOver; + case BitmapBlendingMode.DestinationAtop: + return SKBlendMode.DstATop; + case BitmapBlendingMode.Xor: + return SKBlendMode.Xor; + case BitmapBlendingMode.Plus: + return SKBlendMode.Plus; + default: + throw new ArgumentOutOfRangeException(nameof(blendingMode), blendingMode, null); + } + } + public static SKPoint ToSKPoint(this Point p) { return new SKPoint((float)p.X, (float)p.Y); diff --git a/src/Windows/Avalonia.Direct2D1/Media/DrawingContextImpl.cs b/src/Windows/Avalonia.Direct2D1/Media/DrawingContextImpl.cs index 47a19aad8c..0f5a932ef3 100644 --- a/src/Windows/Avalonia.Direct2D1/Media/DrawingContextImpl.cs +++ b/src/Windows/Avalonia.Direct2D1/Media/DrawingContextImpl.cs @@ -5,6 +5,7 @@ using Avalonia.Platform; using Avalonia.Rendering; using Avalonia.Rendering.SceneGraph; using Avalonia.Utilities; +using Avalonia.Visuals.Media.Imaging; using SharpDX; using SharpDX.Direct2D1; using SharpDX.Mathematics.Interop; @@ -116,12 +117,14 @@ namespace Avalonia.Direct2D1.Media /// The rect in the image to draw. /// The rect in the output to draw to. /// The bitmap interpolation mode. - public void DrawBitmap(IRef source, double opacity, Rect sourceRect, Rect destRect, BitmapInterpolationMode bitmapInterpolationMode) + public void DrawBitmap(IRef source, double opacity, Rect sourceRect, Rect destRect, BitmapInterpolationMode bitmapInterpolationMode, BitmapBlendingMode bitmapBlendingMode) { using (var d2d = ((BitmapImpl)source.Item).GetDirect2DBitmap(_deviceContext)) { var interpolationMode = GetInterpolationMode(bitmapInterpolationMode); - + + // TODO: How to implement CompositeMode here? + _deviceContext.DrawBitmap( d2d.Value, destRect.ToSharpDX(), @@ -149,6 +152,35 @@ namespace Avalonia.Direct2D1.Media } } + public static CompositeMode GetCompositeMode(BitmapBlendingMode blendingMode) + { + switch (blendingMode) + { + case BitmapBlendingMode.SourceIn: + return CompositeMode.SourceIn; + case BitmapBlendingMode.SourceOut: + return CompositeMode.SourceOut; + case BitmapBlendingMode.SourceOver: + return CompositeMode.SourceOver; + case BitmapBlendingMode.SourceAtop: + return CompositeMode.SourceAtop; + case BitmapBlendingMode.DestinationIn: + return CompositeMode.DestinationIn; + case BitmapBlendingMode.DestinationOut: + return CompositeMode.DestinationOut; + case BitmapBlendingMode.DestinationOver: + return CompositeMode.DestinationOver; + case BitmapBlendingMode.DestinationAtop: + return CompositeMode.DestinationAtop; + case BitmapBlendingMode.Xor: + return CompositeMode.Xor; + case BitmapBlendingMode.Plus: + return CompositeMode.Plus; + default: + throw new ArgumentOutOfRangeException(nameof(blendingMode), blendingMode, null); + } + } + /// /// Draws a bitmap image. /// From 4bffeaf3276a86c11d7b84e9a886713d6099620e Mon Sep 17 00:00:00 2001 From: Jumar Macato Date: Thu, 18 Mar 2021 21:24:38 +0800 Subject: [PATCH 11/38] use pop/push model for blendingmode instead. --- .../Platform/IDrawingContextImpl.cs | 13 +++++++++++- src/Skia/Avalonia.Skia/DrawingContextImpl.cs | 21 ++++++++++++++++--- .../Media/DrawingContextImpl.cs | 12 ++++++++++- 3 files changed, 41 insertions(+), 5 deletions(-) diff --git a/src/Avalonia.Visuals/Platform/IDrawingContextImpl.cs b/src/Avalonia.Visuals/Platform/IDrawingContextImpl.cs index e9a1a2a6ae..e2fefffecc 100644 --- a/src/Avalonia.Visuals/Platform/IDrawingContextImpl.cs +++ b/src/Avalonia.Visuals/Platform/IDrawingContextImpl.cs @@ -31,7 +31,7 @@ namespace Avalonia.Platform /// The rect in the output to draw to. /// The bitmap interpolation mode. /// The bitmap blending mode. - void DrawBitmap(IRef source, double opacity, Rect sourceRect, Rect destRect, BitmapInterpolationMode bitmapInterpolationMode = BitmapInterpolationMode.Default, BitmapBlendingMode bitmapBlendMode = BitmapBlendingMode.SourceOver); + void DrawBitmap(IRef source, double opacity, Rect sourceRect, Rect destRect, BitmapInterpolationMode bitmapInterpolationMode = BitmapInterpolationMode.Default); /// /// Draws a bitmap image. @@ -149,6 +149,17 @@ namespace Avalonia.Platform /// Pops the latest pushed geometry clip. /// void PopGeometryClip(); + + /// + /// Pushes an bitmap blending value. + /// + /// The opacity. + void PushBitmapBlendMode(BitmapBlendingMode blendingMode); + + /// + /// Pops the latest pushed bitmap blending value. + /// + void PopBitmapBlendMode(); /// /// Adds a custom draw operation diff --git a/src/Skia/Avalonia.Skia/DrawingContextImpl.cs b/src/Skia/Avalonia.Skia/DrawingContextImpl.cs index 692d40a653..b7d5d3ec59 100644 --- a/src/Skia/Avalonia.Skia/DrawingContextImpl.cs +++ b/src/Skia/Avalonia.Skia/DrawingContextImpl.cs @@ -23,9 +23,11 @@ namespace Avalonia.Skia private readonly Vector _dpi; private readonly Stack _maskStack = new Stack(); private readonly Stack _opacityStack = new Stack(); + private readonly Stack _blendingModeStack = new Stack(); private readonly Matrix? _postTransform; private readonly IVisualBrushRenderer _visualBrushRenderer; private double _currentOpacity = 1.0f; + private BitmapBlendingMode _currentBlendingMode = BitmapBlendingMode.SourceOver; private readonly bool _canTextUseLcdRendering; private Matrix _currentTransform; private bool _disposed; @@ -132,7 +134,7 @@ namespace Avalonia.Skia } /// - public void DrawBitmap(IRef source, double opacity, Rect sourceRect, Rect destRect, BitmapInterpolationMode bitmapInterpolationMode, BitmapBlendingMode bitmapBlendingMode) + public void DrawBitmap(IRef source, double opacity, Rect sourceRect, Rect destRect, BitmapInterpolationMode bitmapInterpolationMode) { var drawableImage = (IDrawableBitmapImpl)source.Item; var s = sourceRect.ToSKRect(); @@ -145,7 +147,7 @@ namespace Avalonia.Skia }) { paint.FilterQuality = bitmapInterpolationMode.ToSKFilterQuality(); - paint.BlendMode = bitmapBlendingMode.ToSKBlendMode(); + paint.BlendMode = _currentBlendingMode.ToSKBlendMode(); drawableImage.Draw(this, s, d, paint); } @@ -155,7 +157,7 @@ namespace Avalonia.Skia public void DrawBitmap(IRef source, IBrush opacityMask, Rect opacityMaskRect, Rect destRect) { PushOpacityMask(opacityMask, opacityMaskRect); - DrawBitmap(source, 1, new Rect(0, 0, source.Item.PixelSize.Width, source.Item.PixelSize.Height), destRect, BitmapInterpolationMode.Default, BitmapBlendingMode.SourceOver); + DrawBitmap(source, 1, new Rect(0, 0, source.Item.PixelSize.Width, source.Item.PixelSize.Height), destRect, BitmapInterpolationMode.Default); PopOpacityMask(); } @@ -509,6 +511,19 @@ namespace Avalonia.Skia Canvas.Restore(); } + /// + public void PushBitmapBlendMode(BitmapBlendingMode blendingMode) + { + _blendingModeStack.Push(_currentBlendingMode); + _currentBlendingMode = blendingMode; + } + + /// + public void PopBitmapBlendMode() + { + _currentBlendingMode = _blendingModeStack.Pop(); + } + public void Custom(ICustomDrawOperation custom) => custom.Render(this); /// diff --git a/src/Windows/Avalonia.Direct2D1/Media/DrawingContextImpl.cs b/src/Windows/Avalonia.Direct2D1/Media/DrawingContextImpl.cs index 0f5a932ef3..efe581ae0d 100644 --- a/src/Windows/Avalonia.Direct2D1/Media/DrawingContextImpl.cs +++ b/src/Windows/Avalonia.Direct2D1/Media/DrawingContextImpl.cs @@ -117,7 +117,7 @@ namespace Avalonia.Direct2D1.Media /// The rect in the image to draw. /// The rect in the output to draw to. /// The bitmap interpolation mode. - public void DrawBitmap(IRef source, double opacity, Rect sourceRect, Rect destRect, BitmapInterpolationMode bitmapInterpolationMode, BitmapBlendingMode bitmapBlendingMode) + public void DrawBitmap(IRef source, double opacity, Rect sourceRect, Rect destRect, BitmapInterpolationMode bitmapInterpolationMode) { using (var d2d = ((BitmapImpl)source.Item).GetDirect2DBitmap(_deviceContext)) { @@ -557,6 +557,16 @@ namespace Avalonia.Direct2D1.Media PopLayer(); } + public void PushBitmapBlendMode(BitmapBlendingMode blendingMode) + { + // Stubs for now + } + + public void PopBitmapBlendMode() + { + // Stubs for now + } + public void PushOpacityMask(IBrush mask, Rect bounds) { var parameters = new LayerParameters From c6491400fbbd4c5e5e96ad23b38d1365674573f7 Mon Sep 17 00:00:00 2001 From: Jumar Macato Date: Thu, 18 Mar 2021 21:31:59 +0800 Subject: [PATCH 12/38] use path cache helpers and eliminate spurious instantiations of SKPathMeasure --- src/Skia/Avalonia.Skia/GeometryImpl.cs | 30 +++++++++++++------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/src/Skia/Avalonia.Skia/GeometryImpl.cs b/src/Skia/Avalonia.Skia/GeometryImpl.cs index e8b5795f60..8b4ba66cf6 100644 --- a/src/Skia/Avalonia.Skia/GeometryImpl.cs +++ b/src/Skia/Avalonia.Skia/GeometryImpl.cs @@ -11,19 +11,12 @@ namespace Avalonia.Skia internal abstract class GeometryImpl : IGeometryImpl { private PathCache _pathCache; - + /// public abstract Rect Bounds { get; } /// - public double ContourLength - { - get - { - if (EffectivePath is null) return 0; - return new SKPathMeasure(EffectivePath).Length; - } - } + public double ContourLength => _pathCache.CachePathMeasure?.Length ?? 0; public abstract SKPath EffectivePath { get; } @@ -118,13 +111,13 @@ namespace Avalonia.Skia /// public bool TryGetPointAtDistance(double distance, out Point point) { - if (EffectivePath is null) + if (_pathCache.CachePathMeasure is null) { point = new Point(); return false; } - var res = new SKPathMeasure(EffectivePath).GetPosition((float)distance, out var skPoint); + var res = _pathCache.CachePathMeasure.GetPosition((float)distance, out var skPoint); point = new Point(skPoint.X, skPoint.Y); return res; } @@ -132,14 +125,14 @@ namespace Avalonia.Skia /// public bool TryGetPointAndTangentAtDistance(double distance, out Point point, out Point tangent) { - if (EffectivePath is null) + if (_pathCache.CachePathMeasure is null) { point = new Point(); tangent = new Point(); return false; } - var res = new SKPathMeasure(EffectivePath).GetPositionAndTangent((float)distance, out var skPoint, out var skTangent); + var res = _pathCache.CachePathMeasure.GetPositionAndTangent((float)distance, out var skPoint, out var skTangent); point = new Point(skPoint.X, skPoint.Y); tangent = new Point(skTangent.X, skTangent.Y); return res; @@ -156,7 +149,7 @@ namespace Avalonia.Skia private struct PathCache { private float _cachedStrokeWidth; - + /// /// Tolerance for two stroke widths to be deemed equal /// @@ -171,6 +164,11 @@ namespace Avalonia.Skia /// Cached geometry render bounds. /// public Rect CachedGeometryRenderBounds { get; private set; } + + /// + /// Cached path measurement helper. + /// + public SKPathMeasure CachePathMeasure { get; private set; } /// /// Is cached valid for given stroke width. @@ -196,7 +194,8 @@ namespace Avalonia.Skia } CachedStrokePath = path; - CachedGeometryRenderBounds = geometryRenderBounds; + CachedGeometryRenderBounds = geometryRenderBounds; + CachePathMeasure = new SKPathMeasure(path); _cachedStrokeWidth = strokeWidth; } @@ -206,6 +205,7 @@ namespace Avalonia.Skia public void Invalidate() { CachedStrokePath?.Dispose(); + CachePathMeasure?.Dispose(); CachedGeometryRenderBounds = Rect.Empty; _cachedStrokeWidth = default(float); } From cd35e2c35ea04b6f4b44d419c64e024eba4ddcbb Mon Sep 17 00:00:00 2001 From: Jumar Macato <16554748+jmacato@users.noreply.github.com> Date: Thu, 18 Mar 2021 21:34:50 +0800 Subject: [PATCH 13/38] Update src/Avalonia.Headless/HeadlessPlatformRenderInterface.cs --- src/Avalonia.Headless/HeadlessPlatformRenderInterface.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Avalonia.Headless/HeadlessPlatformRenderInterface.cs b/src/Avalonia.Headless/HeadlessPlatformRenderInterface.cs index a00dc1f618..ce5a8a2e86 100644 --- a/src/Avalonia.Headless/HeadlessPlatformRenderInterface.cs +++ b/src/Avalonia.Headless/HeadlessPlatformRenderInterface.cs @@ -393,7 +393,7 @@ namespace Avalonia.Headless { } - public void DrawBitmap(IRef source, double opacity, Rect sourceRect, Rect destRect, BitmapInterpolationMode bitmapInterpolationMode = BitmapInterpolationMode.Default, BitmapBlendingMode bitmapBlendingMode = BitmapBlendingMode.SourceOver) + public void DrawBitmap(IRef source, double opacity, Rect sourceRect, Rect destRect, BitmapInterpolationMode bitmapInterpolationMode = BitmapInterpolationMode.Default) { } From f9a432ec9dbc43288ddbc69dcff4076fef26eec7 Mon Sep 17 00:00:00 2001 From: Jumar Macato Date: Thu, 18 Mar 2021 21:40:39 +0800 Subject: [PATCH 14/38] fix HeadlessPlatformRenderInterface.cs --- .../HeadlessPlatformRenderInterface.cs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/Avalonia.Headless/HeadlessPlatformRenderInterface.cs b/src/Avalonia.Headless/HeadlessPlatformRenderInterface.cs index ce5a8a2e86..48c46f50a1 100644 --- a/src/Avalonia.Headless/HeadlessPlatformRenderInterface.cs +++ b/src/Avalonia.Headless/HeadlessPlatformRenderInterface.cs @@ -375,6 +375,16 @@ namespace Avalonia.Headless } + public void PushBitmapBlendMode(BitmapBlendingMode blendingMode) + { + + } + + public void PopBitmapBlendMode() + { + + } + public void Custom(ICustomDrawOperation custom) { From de3795514d0e1a345f1a65d82bb8b6d3a7841d10 Mon Sep 17 00:00:00 2001 From: Jumar Macato Date: Thu, 18 Mar 2021 22:17:50 +0800 Subject: [PATCH 15/38] add DeferredDrawingContextImpl implementations for blendmode --- .../SceneGraph/BitmapBlendModeNode.cs | 68 +++++++++++++++++++ .../SceneGraph/DeferredDrawingContextImpl.cs | 37 +++++++++- .../Rendering/SceneGraph/ImageNode.cs | 10 ++- 3 files changed, 106 insertions(+), 9 deletions(-) create mode 100644 src/Avalonia.Visuals/Rendering/SceneGraph/BitmapBlendModeNode.cs diff --git a/src/Avalonia.Visuals/Rendering/SceneGraph/BitmapBlendModeNode.cs b/src/Avalonia.Visuals/Rendering/SceneGraph/BitmapBlendModeNode.cs new file mode 100644 index 0000000000..b486b5060a --- /dev/null +++ b/src/Avalonia.Visuals/Rendering/SceneGraph/BitmapBlendModeNode.cs @@ -0,0 +1,68 @@ +using Avalonia.Platform; +using Avalonia.Visuals.Media.Imaging; + +namespace Avalonia.Rendering.SceneGraph +{ + /// + /// A node in the scene graph which represents an opacity push or pop. + /// + internal class BitmapBlendModeNode : IDrawOperation + { + /// + /// Initializes a new instance of the class that represents an + /// push. + /// + /// The to push. + public BitmapBlendModeNode(BitmapBlendingMode bitmapBlend) + { + BlendingMode = bitmapBlend; + } + + /// + /// Initializes a new instance of the class that represents an + /// pop. + /// + public BitmapBlendModeNode() + { + } + + /// + public Rect Bounds => Rect.Empty; + + /// + /// Gets the BitmapBlend to be pushed or null if the operation represents a pop. + /// + public BitmapBlendingMode? BlendingMode { get; } + + /// + public bool HitTest(Point p) => false; + + /// + /// Determines if this draw operation equals another. + /// + /// The opacity of the other draw operation. + /// True if the draw operations are the same, otherwise false. + /// + /// The properties of the other draw operation are passed in as arguments to prevent + /// allocation of a not-yet-constructed draw operation object. + /// + public bool Equals(BitmapBlendingMode? blendingMode) => BlendingMode == blendingMode; + + /// + public void Render(IDrawingContextImpl context) + { + if (BlendingMode.HasValue) + { + context.PushBitmapBlendMode(BlendingMode.Value); + } + else + { + context.PopBitmapBlendMode(); + } + } + + public void Dispose() + { + } + } +} diff --git a/src/Avalonia.Visuals/Rendering/SceneGraph/DeferredDrawingContextImpl.cs b/src/Avalonia.Visuals/Rendering/SceneGraph/DeferredDrawingContextImpl.cs index 1705bc6b1c..d4564c9f29 100644 --- a/src/Avalonia.Visuals/Rendering/SceneGraph/DeferredDrawingContextImpl.cs +++ b/src/Avalonia.Visuals/Rendering/SceneGraph/DeferredDrawingContextImpl.cs @@ -112,13 +112,13 @@ namespace Avalonia.Rendering.SceneGraph } /// - public void DrawBitmap(IRef source, double opacity, Rect sourceRect, Rect destRect, BitmapInterpolationMode bitmapInterpolationMode, BitmapBlendingMode bitmapBlendingMode) + public void DrawBitmap(IRef source, double opacity, Rect sourceRect, Rect destRect, BitmapInterpolationMode bitmapInterpolationMode) { var next = NextDrawAs(); - if (next == null || !next.Item.Equals(Transform, source, opacity, sourceRect, destRect, bitmapInterpolationMode, bitmapBlendingMode)) + if (next == null || !next.Item.Equals(Transform, source, opacity, sourceRect, destRect, bitmapInterpolationMode)) { - Add(new ImageNode(Transform, source, opacity, sourceRect, destRect, bitmapInterpolationMode, bitmapBlendingMode)); + Add(new ImageNode(Transform, source, opacity, sourceRect, destRect, bitmapInterpolationMode)); } else { @@ -179,6 +179,7 @@ namespace Avalonia.Rendering.SceneGraph } } + public void Custom(ICustomDrawOperation custom) { var next = NextDrawAs(); @@ -253,6 +254,21 @@ namespace Avalonia.Rendering.SceneGraph } } + /// + public void PopBitmapBlendMode() + { + var next = NextDrawAs(); + + if (next == null || !next.Item.Equals(null)) + { + Add(new BitmapBlendModeNode()); + } + else + { + ++_drawOperationindex; + } + } + /// public void PopOpacity() { @@ -358,6 +374,21 @@ namespace Avalonia.Rendering.SceneGraph } } + /// + public void PushBitmapBlendMode(BitmapBlendingMode blendingMode) + { + var next = NextDrawAs(); + + if (next == null || !next.Item.Equals(blendingMode)) + { + Add(new BitmapBlendModeNode(blendingMode)); + } + else + { + ++_drawOperationindex; + } + } + public readonly struct UpdateState : IDisposable { public UpdateState( diff --git a/src/Avalonia.Visuals/Rendering/SceneGraph/ImageNode.cs b/src/Avalonia.Visuals/Rendering/SceneGraph/ImageNode.cs index c50601120f..d3da19d8c9 100644 --- a/src/Avalonia.Visuals/Rendering/SceneGraph/ImageNode.cs +++ b/src/Avalonia.Visuals/Rendering/SceneGraph/ImageNode.cs @@ -18,7 +18,7 @@ namespace Avalonia.Rendering.SceneGraph /// The source rect. /// The destination rect. /// The bitmap interpolation mode. - public ImageNode(Matrix transform, IRef source, double opacity, Rect sourceRect, Rect destRect, BitmapInterpolationMode bitmapInterpolationMode, BitmapBlendingMode bitmapBlendingMode) + public ImageNode(Matrix transform, IRef source, double opacity, Rect sourceRect, Rect destRect, BitmapInterpolationMode bitmapInterpolationMode) : base(destRect, transform) { Transform = transform; @@ -27,7 +27,6 @@ namespace Avalonia.Rendering.SceneGraph SourceRect = sourceRect; DestRect = destRect; BitmapInterpolationMode = bitmapInterpolationMode; - BitmapBlendingMode = bitmapBlendingMode; SourceVersion = Source.Item.Version; } @@ -91,7 +90,7 @@ namespace Avalonia.Rendering.SceneGraph /// The properties of the other draw operation are passed in as arguments to prevent /// allocation of a not-yet-constructed draw operation object. /// - public bool Equals(Matrix transform, IRef source, double opacity, Rect sourceRect, Rect destRect, BitmapInterpolationMode bitmapInterpolationMode, BitmapBlendingMode bitmapBlendingMode) + public bool Equals(Matrix transform, IRef source, double opacity, Rect sourceRect, Rect destRect, BitmapInterpolationMode bitmapInterpolationMode) { return transform == Transform && Equals(source.Item, Source.Item) && @@ -99,15 +98,14 @@ namespace Avalonia.Rendering.SceneGraph opacity == Opacity && sourceRect == SourceRect && destRect == DestRect && - bitmapInterpolationMode == BitmapInterpolationMode && - bitmapBlendingMode == BitmapBlendingMode; + bitmapInterpolationMode == BitmapInterpolationMode; } /// public override void Render(IDrawingContextImpl context) { context.Transform = Transform; - context.DrawBitmap(Source, Opacity, SourceRect, DestRect, BitmapInterpolationMode, BitmapBlendingMode); + context.DrawBitmap(Source, Opacity, SourceRect, DestRect, BitmapInterpolationMode); } /// From db70c9f94c4027e5919992f1666bbdb8e254f5a2 Mon Sep 17 00:00:00 2001 From: Jumar Macato Date: Thu, 18 Mar 2021 22:26:45 +0800 Subject: [PATCH 16/38] apicompat stuff --- src/Avalonia.Visuals/ApiCompatBaseline.txt | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/Avalonia.Visuals/ApiCompatBaseline.txt b/src/Avalonia.Visuals/ApiCompatBaseline.txt index e781cb1d3e..0be3bedbd5 100644 --- a/src/Avalonia.Visuals/ApiCompatBaseline.txt +++ b/src/Avalonia.Visuals/ApiCompatBaseline.txt @@ -1,9 +1,8 @@ Compat issues with assembly Avalonia.Visuals: -InterfacesShouldHaveSameMembers : Interface member 'public void Avalonia.Platform.IDrawingContextImpl.DrawBitmap(Avalonia.Utilities.IRef, System.Double, Avalonia.Rect, Avalonia.Rect, Avalonia.Visuals.Media.Imaging.BitmapInterpolationMode)' is present in the contract but not in the implementation. -MembersMustExist : Member 'public void Avalonia.Platform.IDrawingContextImpl.DrawBitmap(Avalonia.Utilities.IRef, System.Double, Avalonia.Rect, Avalonia.Rect, Avalonia.Visuals.Media.Imaging.BitmapInterpolationMode)' does not exist in the implementation but it does exist in the contract. -InterfacesShouldHaveSameMembers : Interface member 'public void Avalonia.Platform.IDrawingContextImpl.DrawBitmap(Avalonia.Utilities.IRef, System.Double, Avalonia.Rect, Avalonia.Rect, Avalonia.Visuals.Media.Imaging.BitmapInterpolationMode, Avalonia.Visuals.Media.Imaging.BitmapBlendingMode)' is present in the implementation but not in the contract. +InterfacesShouldHaveSameMembers : Interface member 'public void Avalonia.Platform.IDrawingContextImpl.PopBitmapBlendMode()' is present in the implementation but not in the contract. +InterfacesShouldHaveSameMembers : Interface member 'public void Avalonia.Platform.IDrawingContextImpl.PushBitmapBlendMode(Avalonia.Visuals.Media.Imaging.BitmapBlendingMode)' is present in the implementation but not in the contract. InterfacesShouldHaveSameMembers : Interface member 'public System.Double Avalonia.Platform.IGeometryImpl.ContourLength' is present in the implementation but not in the contract. InterfacesShouldHaveSameMembers : Interface member 'public System.Double Avalonia.Platform.IGeometryImpl.ContourLength.get()' is present in the implementation but not in the contract. InterfacesShouldHaveSameMembers : Interface member 'public System.Boolean Avalonia.Platform.IGeometryImpl.TryGetPointAndTangentAtDistance(System.Double, Avalonia.Point, Avalonia.Point)' is present in the implementation but not in the contract. InterfacesShouldHaveSameMembers : Interface member 'public System.Boolean Avalonia.Platform.IGeometryImpl.TryGetPointAtDistance(System.Double, Avalonia.Point)' is present in the implementation but not in the contract. -Total Issues: 7 +Total Issues: 6 From f8596a60ea8965bb6c08e516c03b2c4f996a4c03 Mon Sep 17 00:00:00 2001 From: Jumar Macato Date: Thu, 18 Mar 2021 22:49:01 +0800 Subject: [PATCH 17/38] add unimplemented stuff on MockRenderInterface.cs --- .../VisualTree/MockRenderInterface.cs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/Avalonia.Visuals.UnitTests/VisualTree/MockRenderInterface.cs b/tests/Avalonia.Visuals.UnitTests/VisualTree/MockRenderInterface.cs index 02236e4107..bb6301365a 100644 --- a/tests/Avalonia.Visuals.UnitTests/VisualTree/MockRenderInterface.cs +++ b/tests/Avalonia.Visuals.UnitTests/VisualTree/MockRenderInterface.cs @@ -112,6 +112,8 @@ namespace Avalonia.Visuals.UnitTests.VisualTree } } + public double ContourLength { get; } + public IStreamGeometryImpl Clone() { return this; @@ -151,6 +153,16 @@ namespace Avalonia.Visuals.UnitTests.VisualTree throw new NotImplementedException(); } + public bool TryGetPointAtDistance(double distance, out Point point) + { + throw new NotImplementedException(); + } + + public bool TryGetPointAndTangentAtDistance(double distance, out Point point, out Point tangent) + { + throw new NotImplementedException(); + } + class MockStreamGeometryContext : IStreamGeometryContextImpl { private List points = new List(); From 4ef47bfdb05d04e5f4be199da7c3e47893dfe5fa Mon Sep 17 00:00:00 2001 From: Jumar Macato Date: Fri, 19 Mar 2021 12:14:03 +0800 Subject: [PATCH 18/38] Add TryGetSegment --- .../HeadlessPlatformRenderInterface.cs | 6 +++++ .../Platform/IGeometryImpl.cs | 13 +++++++++++ src/Skia/Avalonia.Skia/GeometryImpl.cs | 22 +++++++++++++++++++ .../Avalonia.Direct2D1/Media/GeometryImpl.cs | 9 ++++++++ .../MockStreamGeometryImpl.cs | 6 +++++ .../VisualTree/MockRenderInterface.cs | 5 +++++ 6 files changed, 61 insertions(+) diff --git a/src/Avalonia.Headless/HeadlessPlatformRenderInterface.cs b/src/Avalonia.Headless/HeadlessPlatformRenderInterface.cs index 48c46f50a1..1563c66a96 100644 --- a/src/Avalonia.Headless/HeadlessPlatformRenderInterface.cs +++ b/src/Avalonia.Headless/HeadlessPlatformRenderInterface.cs @@ -142,6 +142,12 @@ namespace Avalonia.Headless tangent = new Point(); return false; } + + public bool TryGetSegment(float startDistance, float stopDistance, bool startOnBeginFigure, out IGeometryImpl segmentGeometry) + { + segmentGeometry = null; + return false; + } } class HeadlessTransformedGeometryStub : HeadlessGeometryStub, ITransformedGeometryImpl diff --git a/src/Avalonia.Visuals/Platform/IGeometryImpl.cs b/src/Avalonia.Visuals/Platform/IGeometryImpl.cs index c61e6c6112..33092ca2b4 100644 --- a/src/Avalonia.Visuals/Platform/IGeometryImpl.cs +++ b/src/Avalonia.Visuals/Platform/IGeometryImpl.cs @@ -73,5 +73,18 @@ namespace Avalonia.Platform /// The tangent in the specified distance. /// If there's valid point and tangent at the specified distance. bool TryGetPointAndTangentAtDistance(double distance, out Point point, out Point tangent); + + /// + /// Attempts to get the corresponding path segment + /// given by the two distances specified. + /// Imagine it like snipping a part of the current + /// geometry. + /// + /// The contour distance to start snipping from. + /// The contour distance to stop snipping to. + /// If ture, the resulting snipped path will start with a BeginFigure call. + /// The resulting snipped path. + /// If the snipping operation is successful. + bool TryGetSegment (float startDistance, float stopDistance, bool startOnBeginFigure, out IGeometryImpl segmentGeometry); } } diff --git a/src/Skia/Avalonia.Skia/GeometryImpl.cs b/src/Skia/Avalonia.Skia/GeometryImpl.cs index 8b4ba66cf6..0884399f4e 100644 --- a/src/Skia/Avalonia.Skia/GeometryImpl.cs +++ b/src/Skia/Avalonia.Skia/GeometryImpl.cs @@ -138,6 +138,28 @@ namespace Avalonia.Skia return res; } + public bool TryGetSegment(float startDistance, float stopDistance, bool startOnBeginFigure, out IGeometryImpl segmentGeometry) + { + if (_pathCache.CachePathMeasure is null) + { + segmentGeometry = null; + return false; + } + + segmentGeometry = null; + + SKPath _skPathSegment = null; + + var res = _pathCache.CachePathMeasure.GetSegment(startDistance, stopDistance, _skPathSegment, startOnBeginFigure); + + if (res) + { + segmentGeometry = new StreamGeometryImpl(_skPathSegment); + } + + return res; + } + /// /// Invalidate all caches. Call after chaining path contents. /// diff --git a/src/Windows/Avalonia.Direct2D1/Media/GeometryImpl.cs b/src/Windows/Avalonia.Direct2D1/Media/GeometryImpl.cs index 81c21c8648..e03c049ec2 100644 --- a/src/Windows/Avalonia.Direct2D1/Media/GeometryImpl.cs +++ b/src/Windows/Avalonia.Direct2D1/Media/GeometryImpl.cs @@ -82,6 +82,15 @@ namespace Avalonia.Direct2D1.Media return false; } + public bool TryGetSegment(float startDistance, float stopDistance, bool startOnBeginFigure, out IGeometryImpl segmentGeometry) + { + // Direct2D doesnt have this too sadly. + Logger.TryGet(LogEventLevel.Warning, LogArea.Visual)?.Log(this, "TryGetSegment is not available in Direct2D."); + + segmentGeometry = null; + return false; + } + protected virtual Geometry GetSourceGeometry() => Geometry; } } diff --git a/tests/Avalonia.UnitTests/MockStreamGeometryImpl.cs b/tests/Avalonia.UnitTests/MockStreamGeometryImpl.cs index 4e66eaeb24..dfe2021cc2 100644 --- a/tests/Avalonia.UnitTests/MockStreamGeometryImpl.cs +++ b/tests/Avalonia.UnitTests/MockStreamGeometryImpl.cs @@ -84,6 +84,12 @@ namespace Avalonia.UnitTests return false; } + public bool TryGetSegment(float startDistance, float stopDistance, bool startOnBeginFigure, out IGeometryImpl segmentGeometry) + { + segmentGeometry = null; + return false; + } + class MockStreamGeometryContext : IStreamGeometryContextImpl { private List points = new List(); diff --git a/tests/Avalonia.Visuals.UnitTests/VisualTree/MockRenderInterface.cs b/tests/Avalonia.Visuals.UnitTests/VisualTree/MockRenderInterface.cs index bb6301365a..666ac75c4b 100644 --- a/tests/Avalonia.Visuals.UnitTests/VisualTree/MockRenderInterface.cs +++ b/tests/Avalonia.Visuals.UnitTests/VisualTree/MockRenderInterface.cs @@ -163,6 +163,11 @@ namespace Avalonia.Visuals.UnitTests.VisualTree throw new NotImplementedException(); } + public bool TryGetSegment(float startDistance, float stopDistance, bool startOnBeginFigure, out IGeometryImpl segmentGeometry) + { + throw new NotImplementedException(); + } + class MockStreamGeometryContext : IStreamGeometryContextImpl { private List points = new List(); From edf18a44370cd93c6fded0778ae06ba0e7883608 Mon Sep 17 00:00:00 2001 From: Jumar Macato Date: Fri, 19 Mar 2021 12:22:37 +0800 Subject: [PATCH 19/38] api compat --- src/Avalonia.Visuals/ApiCompatBaseline.txt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Avalonia.Visuals/ApiCompatBaseline.txt b/src/Avalonia.Visuals/ApiCompatBaseline.txt index 0be3bedbd5..6c7b3efaf8 100644 --- a/src/Avalonia.Visuals/ApiCompatBaseline.txt +++ b/src/Avalonia.Visuals/ApiCompatBaseline.txt @@ -5,4 +5,5 @@ InterfacesShouldHaveSameMembers : Interface member 'public System.Double Avaloni InterfacesShouldHaveSameMembers : Interface member 'public System.Double Avalonia.Platform.IGeometryImpl.ContourLength.get()' is present in the implementation but not in the contract. InterfacesShouldHaveSameMembers : Interface member 'public System.Boolean Avalonia.Platform.IGeometryImpl.TryGetPointAndTangentAtDistance(System.Double, Avalonia.Point, Avalonia.Point)' is present in the implementation but not in the contract. InterfacesShouldHaveSameMembers : Interface member 'public System.Boolean Avalonia.Platform.IGeometryImpl.TryGetPointAtDistance(System.Double, Avalonia.Point)' is present in the implementation but not in the contract. -Total Issues: 6 +InterfacesShouldHaveSameMembers : Interface member 'public System.Boolean Avalonia.Platform.IGeometryImpl.TryGetSegment(System.Single, System.Single, System.Boolean, Avalonia.Platform.IGeometryImpl)' is present in the implementation but not in the contract. +Total Issues: 7 From 58e71b6fa9c73eaf04441e01ee7aa58420c39c69 Mon Sep 17 00:00:00 2001 From: Jumar Macato Date: Fri, 19 Mar 2021 14:46:07 +0800 Subject: [PATCH 20/38] fix init --- src/Skia/Avalonia.Skia/GeometryImpl.cs | 88 ++++++++++++++++---------- 1 file changed, 56 insertions(+), 32 deletions(-) diff --git a/src/Skia/Avalonia.Skia/GeometryImpl.cs b/src/Skia/Avalonia.Skia/GeometryImpl.cs index 0884399f4e..5042e6ebd2 100644 --- a/src/Skia/Avalonia.Skia/GeometryImpl.cs +++ b/src/Skia/Avalonia.Skia/GeometryImpl.cs @@ -11,12 +11,27 @@ namespace Avalonia.Skia internal abstract class GeometryImpl : IGeometryImpl { private PathCache _pathCache; - + private SKPathMeasure _pathMeasureCache; + /// public abstract Rect Bounds { get; } - + /// - public double ContourLength => _pathCache.CachePathMeasure?.Length ?? 0; + public double ContourLength + { + get + { + if (EffectivePath is null) + return 0; + + if (_pathMeasureCache is null) + { + _pathMeasureCache = new SKPathMeasure(EffectivePath); + } + + return (double)_pathMeasureCache?.Length; + } + } public abstract SKPath EffectivePath { get; } @@ -34,12 +49,12 @@ namespace Avalonia.Skia // Usually this function is being called with same stroke width per path, so this saves a lot of Skia traffic. var strokeWidth = (float)(pen?.Thickness ?? 0); - + if (!_pathCache.HasCacheFor(strokeWidth)) { UpdatePathCache(strokeWidth); } - + return PathContainsCore(_pathCache.CachedStrokePath, point); } @@ -62,7 +77,7 @@ namespace Avalonia.Skia { paint.IsStroke = true; paint.StrokeWidth = strokeWidth; - + paint.GetFillPath(EffectivePath, strokePath); _pathCache.Cache(strokePath, strokeWidth, strokePath.TightBounds.ToAvaloniaRect()); @@ -78,13 +93,13 @@ namespace Avalonia.Skia /// True, if point is contained in a path. private static bool PathContainsCore(SKPath path, Point point) { - return path.Contains((float)point.X, (float)point.Y); + return path.Contains((float)point.X, (float)point.Y); } /// public IGeometryImpl Intersect(IGeometryImpl geometry) { - var result = EffectivePath.Op(((GeometryImpl) geometry).EffectivePath, SKPathOp.Intersect); + var result = EffectivePath.Op(((GeometryImpl)geometry).EffectivePath, SKPathOp.Intersect); return result == null ? null : new StreamGeometryImpl(result); } @@ -93,70 +108,86 @@ namespace Avalonia.Skia public Rect GetRenderBounds(IPen pen) { var strokeWidth = (float)(pen?.Thickness ?? 0); - + if (!_pathCache.HasCacheFor(strokeWidth)) { UpdatePathCache(strokeWidth); } - + return _pathCache.CachedGeometryRenderBounds; } - + /// public ITransformedGeometryImpl WithTransform(Matrix transform) { return new TransformedGeometryImpl(this, transform); } - + /// public bool TryGetPointAtDistance(double distance, out Point point) { - if (_pathCache.CachePathMeasure is null) + if (EffectivePath is null) { point = new Point(); return false; } - - var res = _pathCache.CachePathMeasure.GetPosition((float)distance, out var skPoint); + + if (_pathMeasureCache is null) + { + _pathMeasureCache = new SKPathMeasure(EffectivePath); + } + + var res = _pathMeasureCache.GetPosition((float)distance, out var skPoint); point = new Point(skPoint.X, skPoint.Y); return res; } - + /// public bool TryGetPointAndTangentAtDistance(double distance, out Point point, out Point tangent) { - if (_pathCache.CachePathMeasure is null) + if (EffectivePath is null) { point = new Point(); tangent = new Point(); return false; } - - var res = _pathCache.CachePathMeasure.GetPositionAndTangent((float)distance, out var skPoint, out var skTangent); + + if (_pathMeasureCache is null) + { + _pathMeasureCache = new SKPathMeasure(EffectivePath); + } + + var res = _pathMeasureCache.GetPositionAndTangent((float)distance, out var skPoint, out var skTangent); point = new Point(skPoint.X, skPoint.Y); tangent = new Point(skTangent.X, skTangent.Y); return res; } - public bool TryGetSegment(float startDistance, float stopDistance, bool startOnBeginFigure, out IGeometryImpl segmentGeometry) + public bool TryGetSegment(float startDistance, float stopDistance, bool startOnBeginFigure, + out IGeometryImpl segmentGeometry) { - if (_pathCache.CachePathMeasure is null) + if (EffectivePath is null) { segmentGeometry = null; return false; } + + if (_pathMeasureCache is null) + { + _pathMeasureCache = new SKPathMeasure(EffectivePath); + } segmentGeometry = null; SKPath _skPathSegment = null; - var res = _pathCache.CachePathMeasure.GetSegment(startDistance, stopDistance, _skPathSegment, startOnBeginFigure); + var res = _pathMeasureCache.GetSegment(startDistance, stopDistance, _skPathSegment, startOnBeginFigure); if (res) { segmentGeometry = new StreamGeometryImpl(_skPathSegment); } - + return res; } @@ -176,7 +207,7 @@ namespace Avalonia.Skia /// Tolerance for two stroke widths to be deemed equal /// public const float Tolerance = float.Epsilon; - + /// /// Cached contour path. /// @@ -186,11 +217,6 @@ namespace Avalonia.Skia /// Cached geometry render bounds. /// public Rect CachedGeometryRenderBounds { get; private set; } - - /// - /// Cached path measurement helper. - /// - public SKPathMeasure CachePathMeasure { get; private set; } /// /// Is cached valid for given stroke width. @@ -216,8 +242,7 @@ namespace Avalonia.Skia } CachedStrokePath = path; - CachedGeometryRenderBounds = geometryRenderBounds; - CachePathMeasure = new SKPathMeasure(path); + CachedGeometryRenderBounds = geometryRenderBounds; _cachedStrokeWidth = strokeWidth; } @@ -227,7 +252,6 @@ namespace Avalonia.Skia public void Invalidate() { CachedStrokePath?.Dispose(); - CachePathMeasure?.Dispose(); CachedGeometryRenderBounds = Rect.Empty; _cachedStrokeWidth = default(float); } From 74a74acf0ced08ea2380109e6fc1987900e8c1a1 Mon Sep 17 00:00:00 2001 From: Jumar Macato Date: Fri, 19 Mar 2021 19:16:14 +0800 Subject: [PATCH 21/38] set from float to double --- src/Avalonia.Headless/HeadlessPlatformRenderInterface.cs | 2 +- src/Avalonia.Visuals/Platform/IGeometryImpl.cs | 2 +- src/Skia/Avalonia.Skia/GeometryImpl.cs | 4 ++-- src/Windows/Avalonia.Direct2D1/Media/GeometryImpl.cs | 2 +- tests/Avalonia.UnitTests/MockStreamGeometryImpl.cs | 2 +- .../VisualTree/MockRenderInterface.cs | 2 +- 6 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Avalonia.Headless/HeadlessPlatformRenderInterface.cs b/src/Avalonia.Headless/HeadlessPlatformRenderInterface.cs index 1563c66a96..62cac378d7 100644 --- a/src/Avalonia.Headless/HeadlessPlatformRenderInterface.cs +++ b/src/Avalonia.Headless/HeadlessPlatformRenderInterface.cs @@ -143,7 +143,7 @@ namespace Avalonia.Headless return false; } - public bool TryGetSegment(float startDistance, float stopDistance, bool startOnBeginFigure, out IGeometryImpl segmentGeometry) + public bool TryGetSegment(double startDistance, double stopDistance, bool startOnBeginFigure, out IGeometryImpl segmentGeometry) { segmentGeometry = null; return false; diff --git a/src/Avalonia.Visuals/Platform/IGeometryImpl.cs b/src/Avalonia.Visuals/Platform/IGeometryImpl.cs index 33092ca2b4..9dfaab9575 100644 --- a/src/Avalonia.Visuals/Platform/IGeometryImpl.cs +++ b/src/Avalonia.Visuals/Platform/IGeometryImpl.cs @@ -85,6 +85,6 @@ namespace Avalonia.Platform /// If ture, the resulting snipped path will start with a BeginFigure call. /// The resulting snipped path. /// If the snipping operation is successful. - bool TryGetSegment (float startDistance, float stopDistance, bool startOnBeginFigure, out IGeometryImpl segmentGeometry); + bool TryGetSegment (double startDistance, double stopDistance, bool startOnBeginFigure, out IGeometryImpl segmentGeometry); } } diff --git a/src/Skia/Avalonia.Skia/GeometryImpl.cs b/src/Skia/Avalonia.Skia/GeometryImpl.cs index 5042e6ebd2..1511640c56 100644 --- a/src/Skia/Avalonia.Skia/GeometryImpl.cs +++ b/src/Skia/Avalonia.Skia/GeometryImpl.cs @@ -163,7 +163,7 @@ namespace Avalonia.Skia return res; } - public bool TryGetSegment(float startDistance, float stopDistance, bool startOnBeginFigure, + public bool TryGetSegment(double startDistance, double stopDistance, bool startOnBeginFigure, out IGeometryImpl segmentGeometry) { if (EffectivePath is null) @@ -181,7 +181,7 @@ namespace Avalonia.Skia SKPath _skPathSegment = null; - var res = _pathMeasureCache.GetSegment(startDistance, stopDistance, _skPathSegment, startOnBeginFigure); + var res = _pathMeasureCache.GetSegment((float)startDistance, (float)stopDistance, _skPathSegment, startOnBeginFigure); if (res) { diff --git a/src/Windows/Avalonia.Direct2D1/Media/GeometryImpl.cs b/src/Windows/Avalonia.Direct2D1/Media/GeometryImpl.cs index e03c049ec2..ec88347a17 100644 --- a/src/Windows/Avalonia.Direct2D1/Media/GeometryImpl.cs +++ b/src/Windows/Avalonia.Direct2D1/Media/GeometryImpl.cs @@ -82,7 +82,7 @@ namespace Avalonia.Direct2D1.Media return false; } - public bool TryGetSegment(float startDistance, float stopDistance, bool startOnBeginFigure, out IGeometryImpl segmentGeometry) + public bool TryGetSegment(double startDistance, double stopDistance, bool startOnBeginFigure, out IGeometryImpl segmentGeometry) { // Direct2D doesnt have this too sadly. Logger.TryGet(LogEventLevel.Warning, LogArea.Visual)?.Log(this, "TryGetSegment is not available in Direct2D."); diff --git a/tests/Avalonia.UnitTests/MockStreamGeometryImpl.cs b/tests/Avalonia.UnitTests/MockStreamGeometryImpl.cs index dfe2021cc2..864e2efbaf 100644 --- a/tests/Avalonia.UnitTests/MockStreamGeometryImpl.cs +++ b/tests/Avalonia.UnitTests/MockStreamGeometryImpl.cs @@ -84,7 +84,7 @@ namespace Avalonia.UnitTests return false; } - public bool TryGetSegment(float startDistance, float stopDistance, bool startOnBeginFigure, out IGeometryImpl segmentGeometry) + public bool TryGetSegment(double startDistance, double stopDistance, bool startOnBeginFigure, out IGeometryImpl segmentGeometry) { segmentGeometry = null; return false; diff --git a/tests/Avalonia.Visuals.UnitTests/VisualTree/MockRenderInterface.cs b/tests/Avalonia.Visuals.UnitTests/VisualTree/MockRenderInterface.cs index 666ac75c4b..6d0683e699 100644 --- a/tests/Avalonia.Visuals.UnitTests/VisualTree/MockRenderInterface.cs +++ b/tests/Avalonia.Visuals.UnitTests/VisualTree/MockRenderInterface.cs @@ -163,7 +163,7 @@ namespace Avalonia.Visuals.UnitTests.VisualTree throw new NotImplementedException(); } - public bool TryGetSegment(float startDistance, float stopDistance, bool startOnBeginFigure, out IGeometryImpl segmentGeometry) + public bool TryGetSegment(double startDistance, double stopDistance, bool startOnBeginFigure, out IGeometryImpl segmentGeometry) { throw new NotImplementedException(); } From dd33693eb738bf1baa6acd58a07bca5ecec9a822 Mon Sep 17 00:00:00 2001 From: Jumar Macato Date: Fri, 19 Mar 2021 19:32:30 +0800 Subject: [PATCH 22/38] fix getsegment --- src/Skia/Avalonia.Skia/GeometryImpl.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Skia/Avalonia.Skia/GeometryImpl.cs b/src/Skia/Avalonia.Skia/GeometryImpl.cs index 1511640c56..e5794c1a64 100644 --- a/src/Skia/Avalonia.Skia/GeometryImpl.cs +++ b/src/Skia/Avalonia.Skia/GeometryImpl.cs @@ -179,7 +179,7 @@ namespace Avalonia.Skia segmentGeometry = null; - SKPath _skPathSegment = null; + var _skPathSegment = new SKPath(); var res = _pathMeasureCache.GetSegment((float)startDistance, (float)stopDistance, _skPathSegment, startOnBeginFigure); From 9291a8f74a79e2f651085bfdc241d8582954e971 Mon Sep 17 00:00:00 2001 From: Jumar Macato Date: Fri, 19 Mar 2021 19:37:29 +0800 Subject: [PATCH 23/38] extend DrawingContext.cs to accept IGeometryImpl in DrawGeometry method --- src/Avalonia.Visuals/Media/DrawingContext.cs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/Avalonia.Visuals/Media/DrawingContext.cs b/src/Avalonia.Visuals/Media/DrawingContext.cs index ae4c927ae2..4e3dc8699c 100644 --- a/src/Avalonia.Visuals/Media/DrawingContext.cs +++ b/src/Avalonia.Visuals/Media/DrawingContext.cs @@ -121,12 +121,23 @@ namespace Avalonia.Media /// The stroke pen. /// The geometry. public void DrawGeometry(IBrush brush, IPen pen, Geometry geometry) + { + DrawGeometry(brush, pen, geometry.PlatformImpl); + } + + /// + /// Draws a geometry. + /// + /// The fill brush. + /// The stroke pen. + /// The geometry. + public void DrawGeometry(IBrush brush, IPen pen, IGeometryImpl geometry) { Contract.Requires(geometry != null); if (brush != null || PenIsVisible(pen)) { - PlatformImpl.DrawGeometry(brush, pen, geometry.PlatformImpl); + PlatformImpl.DrawGeometry(brush, pen, geometry); } } From 9ebdef12a89d38f432acbb8898126786dac9fc77 Mon Sep 17 00:00:00 2001 From: Jumar Macato Date: Fri, 19 Mar 2021 19:37:50 +0800 Subject: [PATCH 24/38] add path measure example --- .../RenderDemo/Pages/PathMeasurementPage.cs | 119 ++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100644 samples/RenderDemo/Pages/PathMeasurementPage.cs diff --git a/samples/RenderDemo/Pages/PathMeasurementPage.cs b/samples/RenderDemo/Pages/PathMeasurementPage.cs new file mode 100644 index 0000000000..9f033e961f --- /dev/null +++ b/samples/RenderDemo/Pages/PathMeasurementPage.cs @@ -0,0 +1,119 @@ +using System.Diagnostics; +using System.Drawing.Drawing2D; +using Avalonia; +using Avalonia.Controls; +using Avalonia.LogicalTree; +using Avalonia.Media; +using Avalonia.Media.Imaging; +using Avalonia.Media.Immutable; +using Avalonia.Threading; +using Avalonia.Visuals.Media.Imaging; + +namespace RenderDemo.Pages +{ + public class PathMeasurementPage : Control + { + private RenderTargetBitmap _bitmap; + + protected override void OnAttachedToLogicalTree(LogicalTreeAttachmentEventArgs e) + { + _bitmap = new RenderTargetBitmap(new PixelSize(500, 500), new Vector(96, 96)); + base.OnAttachedToLogicalTree(e); + } + + protected override void OnDetachedFromLogicalTree(LogicalTreeAttachmentEventArgs e) + { + _bitmap.Dispose(); + _bitmap = null; + base.OnDetachedFromLogicalTree(e); + } + + readonly Stopwatch _st = Stopwatch.StartNew(); + + + readonly IPen strokePen = new ImmutablePen(Brushes.DarkBlue, 10d, null, PenLineCap.Round, PenLineJoin.Round); + readonly IPen strokePen1 = new ImmutablePen(Brushes.Purple, 10d, null, PenLineCap.Round, PenLineJoin.Round); + readonly IPen strokePen2 = new ImmutablePen(Brushes.Green, 10d, null, PenLineCap.Round, PenLineJoin.Round); + readonly IPen strokePen3 = new ImmutablePen(Brushes.LightBlue, 10d, null, PenLineCap.Round, PenLineJoin.Round); + + public override void Render(DrawingContext context) + { + using (var ctxi = _bitmap.CreateDrawingContext(null)) + using (var ctx = new DrawingContext(ctxi, false)) + { + ctxi.Clear(default); + + var x = new PathGeometry(); + + using (var xsad = x.Open()) + { + xsad.BeginFigure(new Point(20, 20), false); + xsad.LineTo(new Point(400, 50)); + xsad.LineTo(new Point(80, 100)); + xsad.LineTo(new Point(300, 150)); + xsad.EndFigure(false); + } + + ctx.DrawGeometry(null, strokePen, x); + + + var length = x.PlatformImpl.ContourLength; + + + if (x.PlatformImpl.TryGetSegment(length * 0.05, length * 0.2, true, out var dst1)) + ctx.DrawGeometry(null, strokePen1, (Geometry)dst1); + + if (x.PlatformImpl.TryGetSegment(length * 0.2, length * 0.8, true, out var dst2)) + ctx.DrawGeometry(null, strokePen2, (Geometry)dst2); + + if (x.PlatformImpl.TryGetSegment(length * 0.8, length * 0.95, true, out var dst3)) + ctx.DrawGeometry(null, strokePen3, (Geometry)dst3); + + /* + * paint.Style = SKPaintStyle.Stroke;z + paint.StrokeWidth = 10;z + paint.IsAntialias = true;z + paint.StrokeCap = SKStrokeCap.Round;z + paint.StrokeJoin = SKStrokeJoin.Round;x + + path.MoveTo(20, 20); + path.LineTo(400, 50); + path.LineTo(80, 100); + path.LineTo(300, 150); + + paint.Color = SampleMedia.Colors.XamarinDarkBlue; + canvas.DrawPath(path, paint); + + using (var measure = new SKPathMeasure(path, false)) + using (var dst = new SKPath()) + { + var length = measure.Length; + + dst.Reset(); + measure.GetSegment(length * 0.05f, length * 0.2f, dst, true); + paint.Color = SampleMedia.Colors.XamarinPurple; + canvas.DrawPath(dst, paint); + + dst.Reset(); + measure.GetSegment( dst, true); + paint.Color = SampleMedia.Colors.XamarinGreen; + canvas.DrawPath(dst, paint); + + dst.Reset(); + measure.GetSegment(length * 0.8f, length * 0.95f, dst, true); + paint.Color = SampleMedia.Colors.XamarinLightBlue; + canvas.DrawPath(dst, paint); + } + */ + // + // ctx.FillRectangle(Brushes.Fuchsia, new Rect(50, 50, 100, 100)); + } + + context.DrawImage(_bitmap, + new Rect(0, 0, 500, 500), + new Rect(0, 0, 500, 500)); + Dispatcher.UIThread.Post(InvalidateVisual, DispatcherPriority.Background); + base.Render(context); + } + } +} From b01dc1932a80d38767b40996e173e7a25b925698 Mon Sep 17 00:00:00 2001 From: Jumar Macato Date: Fri, 19 Mar 2021 20:01:56 +0800 Subject: [PATCH 25/38] add path measure example --- .../RenderDemo/Pages/PathMeasurementPage.cs | 84 ++++++------------- 1 file changed, 25 insertions(+), 59 deletions(-) diff --git a/samples/RenderDemo/Pages/PathMeasurementPage.cs b/samples/RenderDemo/Pages/PathMeasurementPage.cs index 9f033e961f..6373e39ac5 100644 --- a/samples/RenderDemo/Pages/PathMeasurementPage.cs +++ b/samples/RenderDemo/Pages/PathMeasurementPage.cs @@ -1,5 +1,7 @@ +using System; using System.Diagnostics; using System.Drawing.Drawing2D; +using System.Security.Cryptography; using Avalonia; using Avalonia.Controls; using Avalonia.LogicalTree; @@ -19,6 +21,7 @@ namespace RenderDemo.Pages { _bitmap = new RenderTargetBitmap(new PixelSize(500, 500), new Vector(96, 96)); base.OnAttachedToLogicalTree(e); + AffectsRender(BoundsProperty); } protected override void OnDetachedFromLogicalTree(LogicalTreeAttachmentEventArgs e) @@ -28,91 +31,54 @@ namespace RenderDemo.Pages base.OnDetachedFromLogicalTree(e); } - readonly Stopwatch _st = Stopwatch.StartNew(); - - readonly IPen strokePen = new ImmutablePen(Brushes.DarkBlue, 10d, null, PenLineCap.Round, PenLineJoin.Round); readonly IPen strokePen1 = new ImmutablePen(Brushes.Purple, 10d, null, PenLineCap.Round, PenLineJoin.Round); readonly IPen strokePen2 = new ImmutablePen(Brushes.Green, 10d, null, PenLineCap.Round, PenLineJoin.Round); readonly IPen strokePen3 = new ImmutablePen(Brushes.LightBlue, 10d, null, PenLineCap.Round, PenLineJoin.Round); + readonly IPen strokePen4 = new ImmutablePen(Brushes.Red, 1d, null, PenLineCap.Round, PenLineJoin.Round); public override void Render(DrawingContext context) { using (var ctxi = _bitmap.CreateDrawingContext(null)) - using (var ctx = new DrawingContext(ctxi, false)) + using (var bitmapCtx = new DrawingContext(ctxi, false)) { ctxi.Clear(default); - var x = new PathGeometry(); + var basePath = new PathGeometry(); - using (var xsad = x.Open()) + using (var basePathCtx = basePath.Open()) { - xsad.BeginFigure(new Point(20, 20), false); - xsad.LineTo(new Point(400, 50)); - xsad.LineTo(new Point(80, 100)); - xsad.LineTo(new Point(300, 150)); - xsad.EndFigure(false); + basePathCtx.BeginFigure(new Point(20, 20), false); + basePathCtx.LineTo(new Point(400, 50)); + basePathCtx.LineTo(new Point(80, 100)); + basePathCtx.LineTo(new Point(300, 150)); + basePathCtx.EndFigure(false); } - ctx.DrawGeometry(null, strokePen, x); + bitmapCtx.DrawGeometry(null, strokePen, basePath); - var length = x.PlatformImpl.ContourLength; + var length = basePath.PlatformImpl.ContourLength; + if (basePath.PlatformImpl.TryGetSegment(length * 0.05, length * 0.2, true, out var dst1)) + bitmapCtx.DrawGeometry(null, strokePen1, dst1); - if (x.PlatformImpl.TryGetSegment(length * 0.05, length * 0.2, true, out var dst1)) - ctx.DrawGeometry(null, strokePen1, (Geometry)dst1); + if (basePath.PlatformImpl.TryGetSegment(length * 0.2, length * 0.8, true, out var dst2)) + bitmapCtx.DrawGeometry(null, strokePen2, dst2); - if (x.PlatformImpl.TryGetSegment(length * 0.2, length * 0.8, true, out var dst2)) - ctx.DrawGeometry(null, strokePen2, (Geometry)dst2); + if (basePath.PlatformImpl.TryGetSegment(length * 0.8, length * 0.95, true, out var dst3)) + bitmapCtx.DrawGeometry(null, strokePen3, dst3); - if (x.PlatformImpl.TryGetSegment(length * 0.8, length * 0.95, true, out var dst3)) - ctx.DrawGeometry(null, strokePen3, (Geometry)dst3); - - /* - * paint.Style = SKPaintStyle.Stroke;z - paint.StrokeWidth = 10;z - paint.IsAntialias = true;z - paint.StrokeCap = SKStrokeCap.Round;z - paint.StrokeJoin = SKStrokeJoin.Round;x - - path.MoveTo(20, 20); - path.LineTo(400, 50); - path.LineTo(80, 100); - path.LineTo(300, 150); - - paint.Color = SampleMedia.Colors.XamarinDarkBlue; - canvas.DrawPath(path, paint); - - using (var measure = new SKPathMeasure(path, false)) - using (var dst = new SKPath()) - { - var length = measure.Length; - - dst.Reset(); - measure.GetSegment(length * 0.05f, length * 0.2f, dst, true); - paint.Color = SampleMedia.Colors.XamarinPurple; - canvas.DrawPath(dst, paint); - - dst.Reset(); - measure.GetSegment( dst, true); - paint.Color = SampleMedia.Colors.XamarinGreen; - canvas.DrawPath(dst, paint); - - dst.Reset(); - measure.GetSegment(length * 0.8f, length * 0.95f, dst, true); - paint.Color = SampleMedia.Colors.XamarinLightBlue; - canvas.DrawPath(dst, paint); - } - */ - // - // ctx.FillRectangle(Brushes.Fuchsia, new Rect(50, 50, 100, 100)); + var pathBounds = basePath.GetRenderBounds(strokePen); + + bitmapCtx.DrawRectangle(null, strokePen4, pathBounds); } + context.DrawImage(_bitmap, new Rect(0, 0, 500, 500), new Rect(0, 0, 500, 500)); - Dispatcher.UIThread.Post(InvalidateVisual, DispatcherPriority.Background); + base.Render(context); } } From 8b8f4c6669adf58b8037992d8ebeaa8f85ba9c15 Mon Sep 17 00:00:00 2001 From: Jumar Macato Date: Fri, 19 Mar 2021 20:33:52 +0800 Subject: [PATCH 26/38] add path measure example --- samples/RenderDemo/MainWindow.xaml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/samples/RenderDemo/MainWindow.xaml b/samples/RenderDemo/MainWindow.xaml index 93fbe5e412..aa165d13f7 100644 --- a/samples/RenderDemo/MainWindow.xaml +++ b/samples/RenderDemo/MainWindow.xaml @@ -57,6 +57,9 @@ + + + From 149f1a09587f41de335aa56cc6a05c84121568d3 Mon Sep 17 00:00:00 2001 From: Jumar Macato Date: Fri, 19 Mar 2021 20:34:04 +0800 Subject: [PATCH 27/38] api compat again --- src/Avalonia.Visuals/ApiCompatBaseline.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Avalonia.Visuals/ApiCompatBaseline.txt b/src/Avalonia.Visuals/ApiCompatBaseline.txt index 6c7b3efaf8..805d1955ea 100644 --- a/src/Avalonia.Visuals/ApiCompatBaseline.txt +++ b/src/Avalonia.Visuals/ApiCompatBaseline.txt @@ -5,5 +5,5 @@ InterfacesShouldHaveSameMembers : Interface member 'public System.Double Avaloni InterfacesShouldHaveSameMembers : Interface member 'public System.Double Avalonia.Platform.IGeometryImpl.ContourLength.get()' is present in the implementation but not in the contract. InterfacesShouldHaveSameMembers : Interface member 'public System.Boolean Avalonia.Platform.IGeometryImpl.TryGetPointAndTangentAtDistance(System.Double, Avalonia.Point, Avalonia.Point)' is present in the implementation but not in the contract. InterfacesShouldHaveSameMembers : Interface member 'public System.Boolean Avalonia.Platform.IGeometryImpl.TryGetPointAtDistance(System.Double, Avalonia.Point)' is present in the implementation but not in the contract. -InterfacesShouldHaveSameMembers : Interface member 'public System.Boolean Avalonia.Platform.IGeometryImpl.TryGetSegment(System.Single, System.Single, System.Boolean, Avalonia.Platform.IGeometryImpl)' is present in the implementation but not in the contract. +InterfacesShouldHaveSameMembers : Interface member 'public System.Boolean Avalonia.Platform.IGeometryImpl.TryGetSegment(System.Double, System.Double, System.Boolean, Avalonia.Platform.IGeometryImpl)' is present in the implementation but not in the contract. Total Issues: 7 From 077b2cbfd15b93b10ae87e7be1f71de78f99a5b3 Mon Sep 17 00:00:00 2001 From: Jumar Macato Date: Sun, 4 Apr 2021 14:00:24 +0800 Subject: [PATCH 28/38] address review --- .../Rendering/SceneGraph/DeferredDrawingContextImpl.cs | 4 ++-- src/Windows/Avalonia.Direct2D1/Media/DrawingContextImpl.cs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Avalonia.Visuals/Rendering/SceneGraph/DeferredDrawingContextImpl.cs b/src/Avalonia.Visuals/Rendering/SceneGraph/DeferredDrawingContextImpl.cs index d4564c9f29..f4c350eead 100644 --- a/src/Avalonia.Visuals/Rendering/SceneGraph/DeferredDrawingContextImpl.cs +++ b/src/Avalonia.Visuals/Rendering/SceneGraph/DeferredDrawingContextImpl.cs @@ -257,7 +257,7 @@ namespace Avalonia.Rendering.SceneGraph /// public void PopBitmapBlendMode() { - var next = NextDrawAs(); + var next = NextDrawAs(); if (next == null || !next.Item.Equals(null)) { @@ -377,7 +377,7 @@ namespace Avalonia.Rendering.SceneGraph /// public void PushBitmapBlendMode(BitmapBlendingMode blendingMode) { - var next = NextDrawAs(); + var next = NextDrawAs(); if (next == null || !next.Item.Equals(blendingMode)) { diff --git a/src/Windows/Avalonia.Direct2D1/Media/DrawingContextImpl.cs b/src/Windows/Avalonia.Direct2D1/Media/DrawingContextImpl.cs index efe581ae0d..af35934785 100644 --- a/src/Windows/Avalonia.Direct2D1/Media/DrawingContextImpl.cs +++ b/src/Windows/Avalonia.Direct2D1/Media/DrawingContextImpl.cs @@ -559,12 +559,12 @@ namespace Avalonia.Direct2D1.Media public void PushBitmapBlendMode(BitmapBlendingMode blendingMode) { - // Stubs for now + // TODO: Stubs for now } public void PopBitmapBlendMode() { - // Stubs for now + // TODO: Stubs for now } public void PushOpacityMask(IBrush mask, Rect bounds) From ae034cf212583b97fa89c4350f281096fa2de8b6 Mon Sep 17 00:00:00 2001 From: Jumar Macato Date: Sun, 4 Apr 2021 14:03:32 +0800 Subject: [PATCH 29/38] address review --- src/Avalonia.Visuals/Platform/IDrawingContextImpl.cs | 3 +-- src/Avalonia.Visuals/Platform/IGeometryImpl.cs | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Avalonia.Visuals/Platform/IDrawingContextImpl.cs b/src/Avalonia.Visuals/Platform/IDrawingContextImpl.cs index e2fefffecc..8c9c17770e 100644 --- a/src/Avalonia.Visuals/Platform/IDrawingContextImpl.cs +++ b/src/Avalonia.Visuals/Platform/IDrawingContextImpl.cs @@ -30,7 +30,6 @@ namespace Avalonia.Platform /// The rect in the image to draw. /// The rect in the output to draw to. /// The bitmap interpolation mode. - /// The bitmap blending mode. void DrawBitmap(IRef source, double opacity, Rect sourceRect, Rect destRect, BitmapInterpolationMode bitmapInterpolationMode = BitmapInterpolationMode.Default); /// @@ -153,7 +152,7 @@ namespace Avalonia.Platform /// /// Pushes an bitmap blending value. /// - /// The opacity. + /// The bitmap blending mode. void PushBitmapBlendMode(BitmapBlendingMode blendingMode); /// diff --git a/src/Avalonia.Visuals/Platform/IGeometryImpl.cs b/src/Avalonia.Visuals/Platform/IGeometryImpl.cs index 9dfaab9575..2a79202e70 100644 --- a/src/Avalonia.Visuals/Platform/IGeometryImpl.cs +++ b/src/Avalonia.Visuals/Platform/IGeometryImpl.cs @@ -85,6 +85,6 @@ namespace Avalonia.Platform /// If ture, the resulting snipped path will start with a BeginFigure call. /// The resulting snipped path. /// If the snipping operation is successful. - bool TryGetSegment (double startDistance, double stopDistance, bool startOnBeginFigure, out IGeometryImpl segmentGeometry); + bool TryGetSegment(double startDistance, double stopDistance, bool startOnBeginFigure, out IGeometryImpl segmentGeometry); } } From 8652792563c8c0bd971f3e36c78ad43b66103d31 Mon Sep 17 00:00:00 2001 From: Jumar Macato Date: Sun, 4 Apr 2021 14:04:07 +0800 Subject: [PATCH 30/38] address review --- .../Rendering/SceneGraph/BitmapBlendModeNode.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Avalonia.Visuals/Rendering/SceneGraph/BitmapBlendModeNode.cs b/src/Avalonia.Visuals/Rendering/SceneGraph/BitmapBlendModeNode.cs index b486b5060a..346a5941d5 100644 --- a/src/Avalonia.Visuals/Rendering/SceneGraph/BitmapBlendModeNode.cs +++ b/src/Avalonia.Visuals/Rendering/SceneGraph/BitmapBlendModeNode.cs @@ -12,7 +12,7 @@ namespace Avalonia.Rendering.SceneGraph /// Initializes a new instance of the class that represents an /// push. /// - /// The to push. + /// The to push. public BitmapBlendModeNode(BitmapBlendingMode bitmapBlend) { BlendingMode = bitmapBlend; From cdb16d31611e0899c82434281f4978c27c9a29e6 Mon Sep 17 00:00:00 2001 From: Jumar Macato Date: Sun, 4 Apr 2021 14:04:26 +0800 Subject: [PATCH 31/38] address review --- .../Rendering/SceneGraph/DeferredDrawingContextImpl.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Avalonia.Visuals/Rendering/SceneGraph/DeferredDrawingContextImpl.cs b/src/Avalonia.Visuals/Rendering/SceneGraph/DeferredDrawingContextImpl.cs index f4c350eead..ec162e3e2a 100644 --- a/src/Avalonia.Visuals/Rendering/SceneGraph/DeferredDrawingContextImpl.cs +++ b/src/Avalonia.Visuals/Rendering/SceneGraph/DeferredDrawingContextImpl.cs @@ -178,8 +178,7 @@ namespace Avalonia.Rendering.SceneGraph ++_drawOperationindex; } } - - + public void Custom(ICustomDrawOperation custom) { var next = NextDrawAs(); From 7485b3a8cfa7ac628625875639548322874b5f8c Mon Sep 17 00:00:00 2001 From: Jumar Macato <16554748+jmacato@users.noreply.github.com> Date: Fri, 9 Apr 2021 17:42:02 +0800 Subject: [PATCH 32/38] Update src/Avalonia.Visuals/Rendering/SceneGraph/BitmapBlendModeNode.cs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Dariusz Komosiński --- .../Rendering/SceneGraph/BitmapBlendModeNode.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Avalonia.Visuals/Rendering/SceneGraph/BitmapBlendModeNode.cs b/src/Avalonia.Visuals/Rendering/SceneGraph/BitmapBlendModeNode.cs index 346a5941d5..0a5c1f8db6 100644 --- a/src/Avalonia.Visuals/Rendering/SceneGraph/BitmapBlendModeNode.cs +++ b/src/Avalonia.Visuals/Rendering/SceneGraph/BitmapBlendModeNode.cs @@ -4,7 +4,7 @@ using Avalonia.Visuals.Media.Imaging; namespace Avalonia.Rendering.SceneGraph { /// - /// A node in the scene graph which represents an opacity push or pop. + /// A node in the scene graph which represents an bitmap blending mode push or pop. /// internal class BitmapBlendModeNode : IDrawOperation { From ef903d7ed2f0afd368a08ff38ab8557636ef2efb Mon Sep 17 00:00:00 2001 From: Jumar Macato <16554748+jmacato@users.noreply.github.com> Date: Fri, 9 Apr 2021 17:42:26 +0800 Subject: [PATCH 33/38] Update src/Avalonia.Visuals/Platform/IDrawingContextImpl.cs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Dariusz Komosiński --- src/Avalonia.Visuals/Platform/IDrawingContextImpl.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Avalonia.Visuals/Platform/IDrawingContextImpl.cs b/src/Avalonia.Visuals/Platform/IDrawingContextImpl.cs index 8c9c17770e..39d4066e55 100644 --- a/src/Avalonia.Visuals/Platform/IDrawingContextImpl.cs +++ b/src/Avalonia.Visuals/Platform/IDrawingContextImpl.cs @@ -150,7 +150,7 @@ namespace Avalonia.Platform void PopGeometryClip(); /// - /// Pushes an bitmap blending value. + /// Pushes a bitmap blending value. /// /// The bitmap blending mode. void PushBitmapBlendMode(BitmapBlendingMode blendingMode); From e191d22d96c9e1cd2aaf56499287e2bc755a840d Mon Sep 17 00:00:00 2001 From: Jumar Macato <16554748+jmacato@users.noreply.github.com> Date: Fri, 9 Apr 2021 18:18:49 +0800 Subject: [PATCH 34/38] Update src/Avalonia.Visuals/Platform/IGeometryImpl.cs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Dariusz Komosiński --- src/Avalonia.Visuals/Platform/IGeometryImpl.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Avalonia.Visuals/Platform/IGeometryImpl.cs b/src/Avalonia.Visuals/Platform/IGeometryImpl.cs index 2a79202e70..79e125c5cb 100644 --- a/src/Avalonia.Visuals/Platform/IGeometryImpl.cs +++ b/src/Avalonia.Visuals/Platform/IGeometryImpl.cs @@ -55,7 +55,7 @@ namespace Avalonia.Platform ITransformedGeometryImpl WithTransform(Matrix transform); /// - /// Attempts to get the corresponding point from the + /// Attempts to get the corresponding point at the /// specified distance /// /// The contour distance to get from. From 1ab777c96188a1df486ea96bcbee192374772fbc Mon Sep 17 00:00:00 2001 From: Jumar Macato Date: Fri, 9 Apr 2021 18:22:19 +0800 Subject: [PATCH 35/38] address review --- .../RenderDemo/Pages/PathMeasurementPage.cs | 6 ++- .../SceneGraph/DeferredDrawingContextImpl.cs | 2 +- src/Skia/Avalonia.Skia/GeometryImpl.cs | 43 ++++++++----------- 3 files changed, 24 insertions(+), 27 deletions(-) diff --git a/samples/RenderDemo/Pages/PathMeasurementPage.cs b/samples/RenderDemo/Pages/PathMeasurementPage.cs index 6373e39ac5..212377deae 100644 --- a/samples/RenderDemo/Pages/PathMeasurementPage.cs +++ b/samples/RenderDemo/Pages/PathMeasurementPage.cs @@ -15,13 +15,17 @@ namespace RenderDemo.Pages { public class PathMeasurementPage : Control { + static PathMeasurementPage() + { + AffectsRender(BoundsProperty); + } + private RenderTargetBitmap _bitmap; protected override void OnAttachedToLogicalTree(LogicalTreeAttachmentEventArgs e) { _bitmap = new RenderTargetBitmap(new PixelSize(500, 500), new Vector(96, 96)); base.OnAttachedToLogicalTree(e); - AffectsRender(BoundsProperty); } protected override void OnDetachedFromLogicalTree(LogicalTreeAttachmentEventArgs e) diff --git a/src/Avalonia.Visuals/Rendering/SceneGraph/DeferredDrawingContextImpl.cs b/src/Avalonia.Visuals/Rendering/SceneGraph/DeferredDrawingContextImpl.cs index ec162e3e2a..e6092574c5 100644 --- a/src/Avalonia.Visuals/Rendering/SceneGraph/DeferredDrawingContextImpl.cs +++ b/src/Avalonia.Visuals/Rendering/SceneGraph/DeferredDrawingContextImpl.cs @@ -178,7 +178,7 @@ namespace Avalonia.Rendering.SceneGraph ++_drawOperationindex; } } - + public void Custom(ICustomDrawOperation custom) { var next = NextDrawAs(); diff --git a/src/Skia/Avalonia.Skia/GeometryImpl.cs b/src/Skia/Avalonia.Skia/GeometryImpl.cs index e5794c1a64..23fc5be20a 100644 --- a/src/Skia/Avalonia.Skia/GeometryImpl.cs +++ b/src/Skia/Avalonia.Skia/GeometryImpl.cs @@ -11,7 +11,20 @@ namespace Avalonia.Skia internal abstract class GeometryImpl : IGeometryImpl { private PathCache _pathCache; - private SKPathMeasure _pathMeasureCache; + private SKPathMeasure _pathMeasureCachex; + + private SKPathMeasure CachedPathMeasure + { + get + { + if (_pathMeasureCachex is null) + { + _pathMeasureCachex = new SKPathMeasure(EffectivePath); + } + + return _pathMeasureCachex; + } + } /// public abstract Rect Bounds { get; } @@ -24,12 +37,7 @@ namespace Avalonia.Skia if (EffectivePath is null) return 0; - if (_pathMeasureCache is null) - { - _pathMeasureCache = new SKPathMeasure(EffectivePath); - } - - return (double)_pathMeasureCache?.Length; + return (double)CachedPathMeasure?.Length; } } @@ -132,12 +140,7 @@ namespace Avalonia.Skia return false; } - if (_pathMeasureCache is null) - { - _pathMeasureCache = new SKPathMeasure(EffectivePath); - } - - var res = _pathMeasureCache.GetPosition((float)distance, out var skPoint); + var res = CachedPathMeasure.GetPosition((float)distance, out var skPoint); point = new Point(skPoint.X, skPoint.Y); return res; } @@ -152,12 +155,7 @@ namespace Avalonia.Skia return false; } - if (_pathMeasureCache is null) - { - _pathMeasureCache = new SKPathMeasure(EffectivePath); - } - - var res = _pathMeasureCache.GetPositionAndTangent((float)distance, out var skPoint, out var skTangent); + var res = CachedPathMeasure.GetPositionAndTangent((float)distance, out var skPoint, out var skTangent); point = new Point(skPoint.X, skPoint.Y); tangent = new Point(skTangent.X, skTangent.Y); return res; @@ -172,16 +170,11 @@ namespace Avalonia.Skia return false; } - if (_pathMeasureCache is null) - { - _pathMeasureCache = new SKPathMeasure(EffectivePath); - } - segmentGeometry = null; var _skPathSegment = new SKPath(); - var res = _pathMeasureCache.GetSegment((float)startDistance, (float)stopDistance, _skPathSegment, startOnBeginFigure); + var res = CachedPathMeasure.GetSegment((float)startDistance, (float)stopDistance, _skPathSegment, startOnBeginFigure); if (res) { From 2da760005ca9c452464f7926712fd1bc2e70a5e9 Mon Sep 17 00:00:00 2001 From: Jumar Macato Date: Sat, 10 Apr 2021 14:44:12 +0800 Subject: [PATCH 36/38] address review --- src/Skia/Avalonia.Skia/GeometryImpl.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Skia/Avalonia.Skia/GeometryImpl.cs b/src/Skia/Avalonia.Skia/GeometryImpl.cs index 23fc5be20a..19dbcf9713 100644 --- a/src/Skia/Avalonia.Skia/GeometryImpl.cs +++ b/src/Skia/Avalonia.Skia/GeometryImpl.cs @@ -11,18 +11,18 @@ namespace Avalonia.Skia internal abstract class GeometryImpl : IGeometryImpl { private PathCache _pathCache; - private SKPathMeasure _pathMeasureCachex; + private SKPathMeasure _pathMeasureCache; private SKPathMeasure CachedPathMeasure { get { - if (_pathMeasureCachex is null) + if (_pathMeasureCache is null) { - _pathMeasureCachex = new SKPathMeasure(EffectivePath); + _pathMeasureCache = new SKPathMeasure(EffectivePath); } - return _pathMeasureCachex; + return _pathMeasureCache; } } From bcca8d86161b0fc50f6c808b4d7f07d831a1fa93 Mon Sep 17 00:00:00 2001 From: Andrey Ermilkin Date: Sun, 11 Apr 2021 16:29:37 -0400 Subject: [PATCH 37/38] Scaling Vector is not commutative --- tests/Avalonia.Visuals.UnitTests/VectorTests.cs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/Avalonia.Visuals.UnitTests/VectorTests.cs b/tests/Avalonia.Visuals.UnitTests/VectorTests.cs index 0e72cd7c7f..f9a9e59436 100644 --- a/tests/Avalonia.Visuals.UnitTests/VectorTests.cs +++ b/tests/Avalonia.Visuals.UnitTests/VectorTests.cs @@ -105,5 +105,15 @@ namespace Avalonia.Visuals.UnitTests Assert.Equal(expected, Vector.Multiply(vector, 2)); } + + [Fact] + public void Scale_Vector_Should_Be_Commutative() + { + var vector = new Vector(10, 2); + + var expected = vector * 2; + + Assert.Equal(expected, 2 * vector); + } } } From 323d975dad07a59fa286e75cc4d5cd05f5352d82 Mon Sep 17 00:00:00 2001 From: Andrey Ermilkin Date: Sun, 11 Apr 2021 16:34:12 -0400 Subject: [PATCH 38/38] Fixes Vector test for scale commutativeness. --- src/Avalonia.Visuals/Vector.cs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/Avalonia.Visuals/Vector.cs b/src/Avalonia.Visuals/Vector.cs index 1b9f5c67d5..79c4202be4 100644 --- a/src/Avalonia.Visuals/Vector.cs +++ b/src/Avalonia.Visuals/Vector.cs @@ -82,6 +82,15 @@ namespace Avalonia public static Vector operator *(Vector vector, double scale) => Multiply(vector, scale); + /// + /// Scales a vector. + /// + /// The vector. + /// The scaling factor. + /// The scaled vector. + public static Vector operator *(double scale, Vector vector) + => Multiply(vector, scale); + /// /// Scales a vector. ///