diff --git a/src/Avalonia.Visuals/Media/EllipseGeometry.cs b/src/Avalonia.Visuals/Media/EllipseGeometry.cs index ca84d4cc7b..c2df9db635 100644 --- a/src/Avalonia.Visuals/Media/EllipseGeometry.cs +++ b/src/Avalonia.Visuals/Media/EllipseGeometry.cs @@ -1,7 +1,6 @@ // Copyright (c) The Avalonia Project. All rights reserved. // Licensed under the MIT license. See licence.md file in the project root for full license information. -using System; using Avalonia.Platform; namespace Avalonia.Media @@ -57,36 +56,8 @@ namespace Avalonia.Media protected override IGeometryImpl CreateDefiningGeometry() { var factory = AvaloniaLocator.Current.GetService(); - var geometry = factory.CreateStreamGeometry(); - using (var ctx = geometry.Open()) - { - var rect = Rect; - double controlPointRatio = (Math.Sqrt(2) - 1) * 4 / 3; - var center = rect.Center; - var radius = new Vector(rect.Width / 2, rect.Height / 2); - - var x0 = center.X - radius.X; - var x1 = center.X - (radius.X * controlPointRatio); - var x2 = center.X; - var x3 = center.X + (radius.X * controlPointRatio); - var x4 = center.X + radius.X; - - var y0 = center.Y - radius.Y; - var y1 = center.Y - (radius.Y * controlPointRatio); - var y2 = center.Y; - var y3 = center.Y + (radius.Y * controlPointRatio); - var y4 = center.Y + radius.Y; - - ctx.BeginFigure(new Point(x2, y0), true); - ctx.CubicBezierTo(new Point(x3, y0), new Point(x4, y1), new Point(x4, y2)); - ctx.CubicBezierTo(new Point(x4, y3), new Point(x3, y4), new Point(x2, y4)); - ctx.CubicBezierTo(new Point(x1, y4), new Point(x0, y3), new Point(x0, y2)); - ctx.CubicBezierTo(new Point(x0, y1), new Point(x1, y0), new Point(x2, y0)); - ctx.EndFigure(true); - } - - return geometry; + return factory.CreateEllipseGeometry(Rect); } } } diff --git a/src/Avalonia.Visuals/Media/LineGeometry.cs b/src/Avalonia.Visuals/Media/LineGeometry.cs index f7ba4ccb0e..90577dabd8 100644 --- a/src/Avalonia.Visuals/Media/LineGeometry.cs +++ b/src/Avalonia.Visuals/Media/LineGeometry.cs @@ -73,16 +73,8 @@ namespace Avalonia.Media protected override IGeometryImpl CreateDefiningGeometry() { var factory = AvaloniaLocator.Current.GetService(); - var geometry = factory.CreateStreamGeometry(); - using (var context = geometry.Open()) - { - context.BeginFigure(StartPoint, false); - context.LineTo(EndPoint); - context.EndFigure(false); - } - - return geometry; + return factory.CreateLineGeometry(StartPoint, EndPoint); } } } diff --git a/src/Avalonia.Visuals/Media/RectangleGeometry.cs b/src/Avalonia.Visuals/Media/RectangleGeometry.cs index 3ccfd80f93..9250500644 100644 --- a/src/Avalonia.Visuals/Media/RectangleGeometry.cs +++ b/src/Avalonia.Visuals/Media/RectangleGeometry.cs @@ -16,12 +16,6 @@ namespace Avalonia.Media public static readonly StyledProperty RectProperty = AvaloniaProperty.Register(nameof(Rect)); - public Rect Rect - { - get => GetValue(RectProperty); - set => SetValue(RectProperty, value); - } - static RectangleGeometry() { AffectsGeometry(RectProperty); @@ -43,25 +37,23 @@ namespace Avalonia.Media Rect = rect; } + /// + /// Gets or sets the bounds of the rectangle. + /// + public Rect Rect + { + get => GetValue(RectProperty); + set => SetValue(RectProperty, value); + } + /// public override Geometry Clone() => new RectangleGeometry(Rect); protected override IGeometryImpl CreateDefiningGeometry() { var factory = AvaloniaLocator.Current.GetService(); - var geometry = factory.CreateStreamGeometry(); - - using (var context = geometry.Open()) - { - var rect = Rect; - context.BeginFigure(rect.TopLeft, true); - context.LineTo(rect.TopRight); - context.LineTo(rect.BottomRight); - context.LineTo(rect.BottomLeft); - context.EndFigure(true); - } - return geometry; + return factory.CreateRectangleGeometry(Rect); } } } diff --git a/src/Avalonia.Visuals/Platform/IPlatformRenderInterface.cs b/src/Avalonia.Visuals/Platform/IPlatformRenderInterface.cs index 3a1f79e32a..87db9251e1 100644 --- a/src/Avalonia.Visuals/Platform/IPlatformRenderInterface.cs +++ b/src/Avalonia.Visuals/Platform/IPlatformRenderInterface.cs @@ -36,6 +36,28 @@ namespace Avalonia.Platform Size constraint, IReadOnlyList spans); + /// + /// Creates an ellipse geometry implementation. + /// + /// The bounds of the ellipse. + /// An ellipse geometry.. + IGeometryImpl CreateEllipseGeometry(Rect rect); + + /// + /// Creates a line geometry implementation. + /// + /// The start of the line. + /// The end of the line. + /// A line geometry. + IGeometryImpl CreateLineGeometry(Point p1, Point p2); + + /// + /// Creates a rectangle geometry implementation. + /// + /// The bounds of the rectangle. + /// A rectangle. + IGeometryImpl CreateRectangleGeometry(Rect rect); + /// /// Creates a stream geometry implementation. /// diff --git a/src/Skia/Avalonia.Skia/EllipseGeometryImpl.cs b/src/Skia/Avalonia.Skia/EllipseGeometryImpl.cs new file mode 100644 index 0000000000..aae1dd8cef --- /dev/null +++ b/src/Skia/Avalonia.Skia/EllipseGeometryImpl.cs @@ -0,0 +1,25 @@ +// Copyright (c) The Avalonia Project. All rights reserved. +// Licensed under the MIT license. See licence.md file in the project root for full license information. + +using SkiaSharp; + +namespace Avalonia.Skia +{ + /// + /// A Skia implementation of a . + /// + internal class EllipseGeometryImpl : GeometryImpl + { + public override Rect Bounds { get; } + public override SKPath EffectivePath { get; } + + public EllipseGeometryImpl(Rect rect) + { + var path = new SKPath(); + path.AddOval(rect.ToSKRect()); + + EffectivePath = path; + Bounds = rect; + } + } +} diff --git a/src/Skia/Avalonia.Skia/LineGeometryImpl.cs b/src/Skia/Avalonia.Skia/LineGeometryImpl.cs new file mode 100644 index 0000000000..e929e153d1 --- /dev/null +++ b/src/Skia/Avalonia.Skia/LineGeometryImpl.cs @@ -0,0 +1,29 @@ +// Copyright (c) The Avalonia Project. All rights reserved. +// Licensed under the MIT license. See licence.md file in the project root for full license information. + +using System; +using SkiaSharp; + +namespace Avalonia.Skia +{ + /// + /// A Skia implementation of a . + /// + internal class LineGeometryImpl : GeometryImpl + { + public override Rect Bounds { get; } + public override SKPath EffectivePath { get; } + + public LineGeometryImpl(Point p1, Point p2) + { + var path = new SKPath(); + path.MoveTo(p1.ToSKPoint()); + path.LineTo(p2.ToSKPoint()); + + EffectivePath = path; + Bounds = new Rect( + new Point(Math.Min(p1.X, p2.X), Math.Min(p1.Y, p2.Y)), + new Point(Math.Max(p1.X, p2.X), Math.Max(p1.Y, p2.Y))); + } + } +} diff --git a/src/Skia/Avalonia.Skia/PlatformRenderInterface.cs b/src/Skia/Avalonia.Skia/PlatformRenderInterface.cs index ec162f9767..362fd028cf 100644 --- a/src/Skia/Avalonia.Skia/PlatformRenderInterface.cs +++ b/src/Skia/Avalonia.Skia/PlatformRenderInterface.cs @@ -60,6 +60,12 @@ namespace Avalonia.Skia return new FormattedTextImpl(text, typeface, textAlignment, wrapping, constraint, spans); } + public IGeometryImpl CreateEllipseGeometry(Rect rect) => new EllipseGeometryImpl(rect); + + public IGeometryImpl CreateLineGeometry(Point p1, Point p2) => new LineGeometryImpl(p1, p2); + + public IGeometryImpl CreateRectangleGeometry(Rect rect) => new RectangleGeometryImpl(rect); + /// public IStreamGeometryImpl CreateStreamGeometry() { diff --git a/src/Skia/Avalonia.Skia/RectangleGeometryImpl.cs b/src/Skia/Avalonia.Skia/RectangleGeometryImpl.cs new file mode 100644 index 0000000000..a873e8e2df --- /dev/null +++ b/src/Skia/Avalonia.Skia/RectangleGeometryImpl.cs @@ -0,0 +1,25 @@ +// Copyright (c) The Avalonia Project. All rights reserved. +// Licensed under the MIT license. See licence.md file in the project root for full license information. + +using SkiaSharp; + +namespace Avalonia.Skia +{ + /// + /// A Skia implementation of a . + /// + internal class RectangleGeometryImpl : GeometryImpl + { + public override Rect Bounds { get; } + public override SKPath EffectivePath { get; } + + public RectangleGeometryImpl(Rect rect) + { + var path = new SKPath(); + path.AddRect(rect.ToSKRect()); + + EffectivePath = path; + Bounds = rect; + } + } +} diff --git a/src/Windows/Avalonia.Direct2D1/Direct2D1Platform.cs b/src/Windows/Avalonia.Direct2D1/Direct2D1Platform.cs index 8412a65e23..5ab9a8f74d 100644 --- a/src/Windows/Avalonia.Direct2D1/Direct2D1Platform.cs +++ b/src/Windows/Avalonia.Direct2D1/Direct2D1Platform.cs @@ -182,10 +182,10 @@ namespace Avalonia.Direct2D1 return new WriteableWicBitmapImpl(size, dpi, format); } - public IStreamGeometryImpl CreateStreamGeometry() - { - return new StreamGeometryImpl(); - } + public IGeometryImpl CreateEllipseGeometry(Rect rect) => new EllipseGeometryImpl(rect); + public IGeometryImpl CreateLineGeometry(Point p1, Point p2) => new LineGeometryImpl(p1, p2); + public IGeometryImpl CreateRectangleGeometry(Rect rect) => new RectangleGeometryImpl(rect); + public IStreamGeometryImpl CreateStreamGeometry() => new StreamGeometryImpl(); public IBitmapImpl LoadBitmap(string fileName) { diff --git a/src/Windows/Avalonia.Direct2D1/Media/EllipseGeometryImpl.cs b/src/Windows/Avalonia.Direct2D1/Media/EllipseGeometryImpl.cs new file mode 100644 index 0000000000..9440966406 --- /dev/null +++ b/src/Windows/Avalonia.Direct2D1/Media/EllipseGeometryImpl.cs @@ -0,0 +1,27 @@ +// Copyright (c) The Avalonia Project. All rights reserved. +// Licensed under the MIT license. See licence.md file in the project root for full license information. + +using SharpDX.Direct2D1; + +namespace Avalonia.Direct2D1.Media +{ + /// + /// A Direct2D implementation of a . + /// + internal class EllipseGeometryImpl : GeometryImpl + { + /// + /// Initializes a new instance of the class. + /// + public EllipseGeometryImpl(Rect rect) + : base(CreateGeometry(rect)) + { + } + + private static Geometry CreateGeometry(Rect rect) + { + var ellipse = new Ellipse(rect.Center.ToSharpDX(), (float)rect.Width / 2, (float)rect.Height / 2); + return new EllipseGeometry(Direct2D1Platform.Direct2D1Factory, ellipse); + } + } +} diff --git a/src/Windows/Avalonia.Direct2D1/Media/LineGeometryImpl.cs b/src/Windows/Avalonia.Direct2D1/Media/LineGeometryImpl.cs new file mode 100644 index 0000000000..6b73fce309 --- /dev/null +++ b/src/Windows/Avalonia.Direct2D1/Media/LineGeometryImpl.cs @@ -0,0 +1,27 @@ +// Copyright (c) The Avalonia Project. All rights reserved. +// Licensed under the MIT license. See licence.md file in the project root for full license information. + +using SharpDX.Direct2D1; + +namespace Avalonia.Direct2D1.Media +{ + /// + /// A Direct2D implementation of a . + /// + internal class LineGeometryImpl : StreamGeometryImpl + { + /// + /// Initializes a new instance of the class. + /// + public LineGeometryImpl(Point p1, Point p2) + { + using (var sink = ((PathGeometry)Geometry).Open()) + { + sink.BeginFigure(p1.ToSharpDX(), FigureBegin.Hollow); + sink.AddLine(p2.ToSharpDX()); + sink.EndFigure(FigureEnd.Open); + sink.Close(); + } + } + } +} diff --git a/src/Windows/Avalonia.Direct2D1/Media/RectangleGeometryImpl.cs b/src/Windows/Avalonia.Direct2D1/Media/RectangleGeometryImpl.cs new file mode 100644 index 0000000000..194de4dd14 --- /dev/null +++ b/src/Windows/Avalonia.Direct2D1/Media/RectangleGeometryImpl.cs @@ -0,0 +1,26 @@ +// Copyright (c) The Avalonia Project. All rights reserved. +// Licensed under the MIT license. See licence.md file in the project root for full license information. + +using SharpDX.Direct2D1; + +namespace Avalonia.Direct2D1.Media +{ + /// + /// A Direct2D implementation of a . + /// + internal class RectangleGeometryImpl : GeometryImpl + { + /// + /// Initializes a new instance of the class. + /// + public RectangleGeometryImpl(Rect rect) + : base(CreateGeometry(rect)) + { + } + + private static Geometry CreateGeometry(Rect rect) + { + return new RectangleGeometry(Direct2D1Platform.Direct2D1Factory, rect.ToDirect2D()); + } + } +} diff --git a/src/Windows/Avalonia.Win32.Interop/Wpf/Direct2DImageSurface.cs b/src/Windows/Avalonia.Win32.Interop/Wpf/Direct2DImageSurface.cs index 400dd59ea9..5b04c5d7ff 100644 --- a/src/Windows/Avalonia.Win32.Interop/Wpf/Direct2DImageSurface.cs +++ b/src/Windows/Avalonia.Win32.Interop/Wpf/Direct2DImageSurface.cs @@ -50,7 +50,7 @@ namespace Avalonia.Win32.Interop.Wpf { _resource = texture.QueryInterface(); - Target = new RenderTarget(AvaloniaLocator.Current.GetService(), surface, + Target = new RenderTarget(Direct2D1Platform.Direct2D1Factory, surface, new RenderTargetProperties { DpiX = (float) dpi.X, diff --git a/tests/Avalonia.UnitTests/MockPlatformRenderInterface.cs b/tests/Avalonia.UnitTests/MockPlatformRenderInterface.cs index 0e2abb314d..a3cc3dec17 100644 --- a/tests/Avalonia.UnitTests/MockPlatformRenderInterface.cs +++ b/tests/Avalonia.UnitTests/MockPlatformRenderInterface.cs @@ -22,6 +22,21 @@ namespace Avalonia.UnitTests return Mock.Of(); } + public IGeometryImpl CreateEllipseGeometry(Rect rect) + { + return Mock.Of(); + } + + public IGeometryImpl CreateLineGeometry(Point p1, Point p2) + { + return Mock.Of(); + } + + public IGeometryImpl CreateRectangleGeometry(Rect rect) + { + return Mock.Of(); + } + public IRenderTarget CreateRenderTarget(IEnumerable surfaces) { return Mock.Of(); diff --git a/tests/Avalonia.Visuals.UnitTests/VisualTree/MockRenderInterface.cs b/tests/Avalonia.Visuals.UnitTests/VisualTree/MockRenderInterface.cs index fec0f0831a..03470670d2 100644 --- a/tests/Avalonia.Visuals.UnitTests/VisualTree/MockRenderInterface.cs +++ b/tests/Avalonia.Visuals.UnitTests/VisualTree/MockRenderInterface.cs @@ -56,6 +56,21 @@ namespace Avalonia.Visuals.UnitTests.VisualTree throw new NotImplementedException(); } + public IGeometryImpl CreateEllipseGeometry(Rect rect) + { + throw new NotImplementedException(); + } + + public IGeometryImpl CreateLineGeometry(Point p1, Point p2) + { + throw new NotImplementedException(); + } + + public IGeometryImpl CreateRectangleGeometry(Rect rect) + { + throw new NotImplementedException(); + } + class MockStreamGeometry : IStreamGeometryImpl { private MockStreamGeometryContext _impl = new MockStreamGeometryContext();