From f78d377ebf87e1531c1cc56c318f2d59af0ae047 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 17 Apr 2022 20:41:05 +0200 Subject: [PATCH] Improve Rectangle/RECT marshalling, add utility extensions --- .../ComputeSharp.D2D1.NetStandard.projitems | 1 + .../System/Math.cs | 46 ++++++++++++++++++ .../Extensions/RECTExtensions.cs | 47 ++++++++++++++++++- .../Extensions/RectangleExtensions.cs | 38 +++++++++------ 4 files changed, 116 insertions(+), 16 deletions(-) create mode 100644 src/ComputeSharp.D2D1.NetStandard/System/Math.cs diff --git a/src/ComputeSharp.D2D1.NetStandard/ComputeSharp.D2D1.NetStandard.projitems b/src/ComputeSharp.D2D1.NetStandard/ComputeSharp.D2D1.NetStandard.projitems index 897c1c96..501f2db0 100644 --- a/src/ComputeSharp.D2D1.NetStandard/ComputeSharp.D2D1.NetStandard.projitems +++ b/src/ComputeSharp.D2D1.NetStandard/ComputeSharp.D2D1.NetStandard.projitems @@ -10,6 +10,7 @@ + diff --git a/src/ComputeSharp.D2D1.NetStandard/System/Math.cs b/src/ComputeSharp.D2D1.NetStandard/System/Math.cs new file mode 100644 index 00000000..7885d997 --- /dev/null +++ b/src/ComputeSharp.D2D1.NetStandard/System/Math.cs @@ -0,0 +1,46 @@ +using System.Runtime.CompilerServices; + +namespace ComputeSharp.D2D1.NetStandard.System; + +/// +/// A polyfill type that mirrors some methods from on .NET 6. +/// +internal static class Math +{ + /// + /// Returns clamped to the inclusive range of and . + /// + /// The value to be clamped. + /// The lower bound of the result. + /// The upper bound of the result. + /// The input value clamped to the inclusive range of and . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static int Clamp(int value, int min, int max) + { + if (value < min) + { + return min; + } + + if (value > max) + { + return max; + } + + return value; + } + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static int Min(int val1, int val2) + { + return global::System.Math.Min(val1, val2); + } + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static int Max(int val1, int val2) + { + return global::System.Math.Max(val1, val2); + } +} \ No newline at end of file diff --git a/src/ComputeSharp.D2D1/Extensions/RECTExtensions.cs b/src/ComputeSharp.D2D1/Extensions/RECTExtensions.cs index 05afc06b..df8d053a 100644 --- a/src/ComputeSharp.D2D1/Extensions/RECTExtensions.cs +++ b/src/ComputeSharp.D2D1/Extensions/RECTExtensions.cs @@ -1,11 +1,16 @@ using System.Drawing; using System.Runtime.CompilerServices; using TerraFX.Interop.Windows; +#if NET6_0_OR_GREATER +using Math = System.Math; +#else +using Math = ComputeSharp.D2D1.NetStandard.System.Math; +#endif namespace ComputeSharp.D2D1.Extensions; /// -/// Helper methods for . +/// Helper methods for and . /// internal static class RECTExtensions { @@ -17,6 +22,44 @@ internal static class RECTExtensions [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Rectangle ToRectangle(this in RECT rect) { - return Rectangle.FromLTRB(rect.left, rect.top, rect.right, rect.bottom); + int left = Math.Clamp(rect.left, int.MinValue / 2, int.MaxValue / 2); + int top = Math.Clamp(rect.top, int.MinValue / 2, int.MaxValue / 2); + int right = Math.Clamp(rect.right, int.MinValue / 2, int.MaxValue / 2); + int bottom = Math.Clamp(rect.bottom, int.MinValue / 2, int.MaxValue / 2); + + int minX = Math.Min(left, right); + int minY = Math.Min(top, bottom); + int maxX = Math.Max(left, right); + int maxY = Math.Max(top, bottom); + + return new(minX, minY, maxX - minX, maxY - minY); + } + + /// + /// Creates a from a value. + /// + /// The input value. + /// The resulting value. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static RECT ToRECT(this in Rectangle rectangle) + { + if (rectangle.IsD2D1Infinite()) + { + return new() + { + left = int.MinValue, + top = int.MinValue, + right = int.MaxValue, + bottom = int.MaxValue + }; + } + + return new() + { + left = rectangle.Left, + top = rectangle.Top, + right = rectangle.Right, + bottom = rectangle.Bottom + }; } } diff --git a/src/ComputeSharp.D2D1/Extensions/RectangleExtensions.cs b/src/ComputeSharp.D2D1/Extensions/RectangleExtensions.cs index 260afe57..8826f625 100644 --- a/src/ComputeSharp.D2D1/Extensions/RectangleExtensions.cs +++ b/src/ComputeSharp.D2D1/Extensions/RectangleExtensions.cs @@ -1,29 +1,39 @@ using System.Drawing; using System.Runtime.CompilerServices; -using TerraFX.Interop.Windows; -namespace ComputeSharp.D2D1.Extensions; +namespace ComputeSharp.D2D1; /// /// Helper methods for . /// -internal static class RectangleExtensions +public static class RectangleExtensions { /// - /// Creates a from a value. + /// Modifies a target instance to be a D2D1 infinite rectangle. /// - /// The input value. - /// The resulting value. + /// The target instance to modify. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static RECT ToRECT(this in Rectangle rectangle) + public static void ToD2D1Infinite(this ref Rectangle rectangle) { - RECT result; + rectangle = new Rectangle( + int.MinValue / 2, + int.MinValue / 2, + int.MaxValue, + int.MaxValue); + } - result.left = rectangle.Left; - result.top = rectangle.Top; - result.right = rectangle.Right; - result.bottom = rectangle.Bottom; - - return result; + /// + /// Checks whether a given instance represents a D2D1 infinite rectangle. + /// + /// The input instance to check. + /// Whether or not represents a D2D1 infinite rectangle. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static bool IsD2D1Infinite(this in Rectangle rectangle) + { + return rectangle == new Rectangle( + int.MinValue / 2, + int.MinValue / 2, + int.MaxValue, + int.MaxValue); } }