diff --git a/src/ComputeSharp.D2D1.Uwp/ABI.Microsoft.Graphics.Canvas/Win2D.cs b/src/ComputeSharp.D2D1.Uwp/ABI.Microsoft.Graphics.Canvas/Win2D.cs new file mode 100644 index 00000000..9674c68d --- /dev/null +++ b/src/ComputeSharp.D2D1.Uwp/ABI.Microsoft.Graphics.Canvas/Win2D.cs @@ -0,0 +1,27 @@ +using System.Numerics; +using System.Runtime.InteropServices; +using TerraFX.Interop; +using Windows.Foundation; + +namespace ABI.Microsoft.Graphics.Canvas; + +/// +/// Native APIs from Microsoft.Graphics.Canvas (ie. Win2D). +/// +internal static unsafe class Win2D +{ + /// + /// Calculates the bounds for a given instance. + /// + /// The object in use. + /// The input instance to compute the bounds for. + /// The transform to use (optional). + /// The resulting bounds. + /// The for the operation. + [DllImport("Microsoft.Graphics.Canvas.dll", PreserveSig = true, ExactSpelling = true)] + public static extern int GetBoundsForICanvasImageInterop( + ICanvasResourceCreator* resourceCreator, + ICanvasImageInterop* image, + [NativeTypeName("Windows::Foundation::Numerics::Matrix3x2 const*")] Matrix3x2* transform, + Rect* rect); +} diff --git a/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.ICanvasImage.cs b/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.ICanvasImage.cs index 8c026cc7..62f2c3d9 100644 --- a/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.ICanvasImage.cs +++ b/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.ICanvasImage.cs @@ -1,22 +1,58 @@ -using System; using System.Numerics; +using System.Runtime.InteropServices; +using ABI.Microsoft.Graphics.Canvas; +using ComputeSharp.D2D1.Extensions; +using ComputeSharp.Interop; using Microsoft.Graphics.Canvas; +using TerraFX.Interop.Windows; using Windows.Foundation; +using ICanvasResourceCreator = Microsoft.Graphics.Canvas.ICanvasResourceCreator; namespace ComputeSharp.D2D1.Uwp; /// -partial class PixelShaderEffect +unsafe partial class PixelShaderEffect { /// Rect ICanvasImage.GetBounds(ICanvasResourceCreator resourceCreator) { - throw new NotSupportedException(); + return GetBounds(resourceCreator, null); } /// Rect ICanvasImage.GetBounds(ICanvasResourceCreator resourceCreator, Matrix3x2 transform) { - throw new NotSupportedException(); + return GetBounds(resourceCreator, &transform); + } + + /// + private Rect GetBounds(ICanvasResourceCreator resourceCreator, Matrix3x2* transform) + { + using ReferenceTracker.Lease _0 = GetReferenceTracker().GetLease(); + + using ComPtr canvasResourceCreatorUnknown = default; + using ComPtr canvasImageInteropUnknown = default; + + // Get the IUnknown* references for the ICanvasResourceCreator and ICanvasImageInterop objects + canvasResourceCreatorUnknown.Attach((IUnknown*)Marshal.GetIUnknownForObject(resourceCreator)); + canvasImageInteropUnknown.Attach((IUnknown*)Marshal.GetIUnknownForObject(this)); + + using ComPtr canvasResourceCreator = default; + using ComPtr canvasImageInterop = default; + + // Get the ICanvasResourceCreator* and ICanvasImageInterop* pointers + canvasResourceCreatorUnknown.CopyTo(canvasResourceCreator.GetAddressOf()).Assert(); + canvasImageInteropUnknown.CopyTo(canvasImageInterop.GetAddressOf()).Assert(); + + Rect bounds; + + // Forward the actual logic to Win2D to compute the image bounds (it needs the internal context) + Win2D.GetBoundsForICanvasImageInterop( + resourceCreator: canvasResourceCreator.Get(), + image: canvasImageInterop.Get(), + transform: transform, + rect: &bounds).Assert(); + + return bounds; } } \ No newline at end of file