From df8a4c250b9635bb25fcaa52077198c0bce9f6d4 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 26 Feb 2023 18:17:46 +0100 Subject: [PATCH] Support CsWinRT RCW marshalling in WinUI project --- .../CanvasEffect.Interop.cs | 23 +++------- .../ComputeSharp.D2D1.UI.projitems | 1 + .../Helpers/RcwMarshaller.cs | 45 +++++++++++++++++++ .../PixelShaderEffect{T}.ICanvasEffect.cs | 24 ++++------ .../PixelShaderEffect{T}.ICanvasImage.cs | 14 +++--- ...ixelShaderEffect{T}.ICanvasImageInterop.cs | 8 ++-- ...derEffect{T}.Properties.Interop.Sources.cs | 9 ++-- 7 files changed, 75 insertions(+), 49 deletions(-) create mode 100644 src/ComputeSharp.D2D1.UI/Helpers/RcwMarshaller.cs diff --git a/src/ComputeSharp.D2D1.UI/CanvasEffect.Interop.cs b/src/ComputeSharp.D2D1.UI/CanvasEffect.Interop.cs index 155c4081..e8cdf977 100644 --- a/src/ComputeSharp.D2D1.UI/CanvasEffect.Interop.cs +++ b/src/ComputeSharp.D2D1.UI/CanvasEffect.Interop.cs @@ -1,8 +1,12 @@ using System; using System.Numerics; -using System.Runtime.InteropServices; using ABI.Microsoft.Graphics.Canvas; using ComputeSharp.D2D1.Extensions; +#if WINDOWS_UWP +using ComputeSharp.D2D1.Uwp.Helpers; +#else +using ComputeSharp.D2D1.WinUI.Helpers; +#endif using Microsoft.Graphics.Canvas; using TerraFX.Interop.DirectX; using TerraFX.Interop.Windows; @@ -35,7 +39,7 @@ unsafe partial class CanvasEffect { using ComPtr canvasImageInterop = default; - GetCanvasImageInterop(canvasImageInterop.GetAddressOf()); + RcwMarshaller.QueryInterface(GetCanvasImage(), canvasImageInterop.GetAddressOf()).Assert(); return canvasImageInterop.Get()->GetDevice(device, type); } @@ -51,7 +55,7 @@ unsafe partial class CanvasEffect { using ComPtr canvasImageInterop = default; - GetCanvasImageInterop(canvasImageInterop.GetAddressOf()); + RcwMarshaller.QueryInterface(GetCanvasImage(), canvasImageInterop.GetAddressOf()).Assert(); return canvasImageInterop.Get()->GetD2DImage(device, deviceContext, flags, targetDpi, realizeDpi, ppImage); } @@ -79,17 +83,4 @@ unsafe partial class CanvasEffect return this.canvasImage; } } - - /// - /// Gets the object currently in use. - /// - /// A pointer to the object currently in use. - private void GetCanvasImageInterop(ICanvasImageInterop** canvasImageInterop) - { - using ComPtr canvasImageUnknown = default; - - canvasImageUnknown.Attach((IUnknown*)Marshal.GetIUnknownForObject(GetCanvasImage())); - - canvasImageUnknown.CopyTo(canvasImageInterop).Assert(); - } } \ No newline at end of file diff --git a/src/ComputeSharp.D2D1.UI/ComputeSharp.D2D1.UI.projitems b/src/ComputeSharp.D2D1.UI/ComputeSharp.D2D1.UI.projitems index cc372c6a..086fc849 100644 --- a/src/ComputeSharp.D2D1.UI/ComputeSharp.D2D1.UI.projitems +++ b/src/ComputeSharp.D2D1.UI/ComputeSharp.D2D1.UI.projitems @@ -32,6 +32,7 @@ + diff --git a/src/ComputeSharp.D2D1.UI/Helpers/RcwMarshaller.cs b/src/ComputeSharp.D2D1.UI/Helpers/RcwMarshaller.cs new file mode 100644 index 00000000..1fff576c --- /dev/null +++ b/src/ComputeSharp.D2D1.UI/Helpers/RcwMarshaller.cs @@ -0,0 +1,45 @@ +#if WINDOWS_UWP +using System.Runtime.InteropServices; +#endif +using TerraFX.Interop.Windows; +#if !WINDOWS_UWP +using WinRT; +#endif + +#if WINDOWS_UWP +namespace ComputeSharp.D2D1.Uwp.Helpers; +#else +namespace ComputeSharp.D2D1.WinUI.Helpers; +#endif + +/// +/// A helper type to handle marshalling of RCW instances. +/// +internal static class RcwMarshaller +{ + /// + /// Retrieves the underlying native object for an input RCW and casts it to the specified type. + /// + /// The interface type to retrieve an instance of. + /// The input RCW instance to unwrap. + /// A pointer to the resulting native object to retrieve. + /// The for the operation. + public static unsafe HRESULT QueryInterface(object managedObject, T** nativeObject) + where T : unmanaged // IUnknown + { + using ComPtr unknownObject = default; + +#if WINDOWS_UWP + // On UWP, due to built-in COM/WinRT support, Marshal.GetIUnknownForObject can handle all the logic + unknownObject.Attach((IUnknown*)Marshal.GetIUnknownForObject(managedObject)); +#else + // On WinUI 3, delegate the RCW unwrapping or CCW creation logic to CsWinRT's APIs + ObjectReferenceValue objectReference = MarshalInspectable.CreateMarshaler2(managedObject); + + unknownObject.Attach((IUnknown*)objectReference.Detach()); +#endif + + // QueryInterface to the specific interface we need + return unknownObject.CopyTo(nativeObject); + } +} diff --git a/src/ComputeSharp.D2D1.UI/PixelShaderEffect{T}.ICanvasEffect.cs b/src/ComputeSharp.D2D1.UI/PixelShaderEffect{T}.ICanvasEffect.cs index 6e5e63d0..c3f7d459 100644 --- a/src/ComputeSharp.D2D1.UI/PixelShaderEffect{T}.ICanvasEffect.cs +++ b/src/ComputeSharp.D2D1.UI/PixelShaderEffect{T}.ICanvasEffect.cs @@ -3,6 +3,11 @@ using System.Buffers; using System.Runtime.InteropServices; using ABI.Microsoft.Graphics.Canvas; using ComputeSharp.D2D1.Extensions; +#if WINDOWS_UWP +using ComputeSharp.D2D1.Uwp.Helpers; +#else +using ComputeSharp.D2D1.WinUI.Helpers; +#endif using ComputeSharp.Interop; using TerraFX.Interop.Windows; using Windows.Foundation; @@ -93,12 +98,10 @@ unsafe partial class PixelShaderEffect Rect result; - using ComPtr canvasEffectUnknown = default; using ComPtr canvasEffectAbi = default; // Get the ABI.Microsoft.Graphics.Canvas.Effects.ICanvasEffect object from the input interface - canvasEffectUnknown.Attach((IUnknown*)Marshal.GetIUnknownForObject(sourceEffect)); - canvasEffectUnknown.CopyTo(canvasEffectAbi.GetAddressOf()).Assert(); + RcwMarshaller.QueryInterface(sourceEffect, canvasEffectAbi.GetAddressOf()).Assert(); Win2D.GetRequiredSourceRectanglesForICanvasImageInterop( resourceCreator: resourceCreatorWithDpi.Get(), @@ -152,10 +155,7 @@ unsafe partial class PixelShaderEffect // Get the underlying ABI.Microsoft.Graphics.Canvas.Effects.ICanvasEffect object for each input effect for (int i = 0; i < sourceEffects.Length; i++) { - using ComPtr canvasEffectUnknown = default; - - canvasEffectUnknown.Attach((IUnknown*)Marshal.GetIUnknownForObject(sourceEffects[i])); - canvasEffectUnknown.CopyTo(canvasEffects[i].GetAddressOf()).Assert(); + RcwMarshaller.QueryInterface(sourceEffects[i], canvasEffects[i].GetAddressOf()).Assert(); } Rect[] result = new Rect[sourceEffects.Length]; @@ -209,16 +209,10 @@ unsafe partial class PixelShaderEffect ABI.Microsoft.Graphics.Canvas.ICanvasResourceCreatorWithDpi** resourceCreatorWithDpi, ICanvasImageInterop** canvasImageInterop) { - using ComPtr resourceCreatorUnknown = default; - // Get the ABI.Microsoft.Graphics.Canvas.ICanvasResourceCreatorWithDpi object from the input interface - resourceCreatorUnknown.Attach((IUnknown*)Marshal.GetIUnknownForObject(resourceCreator)); - resourceCreatorUnknown.CopyTo(resourceCreatorWithDpi).Assert(); - - using ComPtr canvasImageInteropUnknown = default; + RcwMarshaller.QueryInterface(resourceCreator, resourceCreatorWithDpi).Assert(); // Get the ICanvasImageInterop object from the current instance - canvasImageInteropUnknown.Attach((IUnknown*)Marshal.GetIUnknownForObject(this)); - canvasImageInteropUnknown.CopyTo(canvasImageInterop).Assert(); + RcwMarshaller.QueryInterface(this, canvasImageInterop).Assert(); } } \ No newline at end of file diff --git a/src/ComputeSharp.D2D1.UI/PixelShaderEffect{T}.ICanvasImage.cs b/src/ComputeSharp.D2D1.UI/PixelShaderEffect{T}.ICanvasImage.cs index 26b33d5b..19f26b60 100644 --- a/src/ComputeSharp.D2D1.UI/PixelShaderEffect{T}.ICanvasImage.cs +++ b/src/ComputeSharp.D2D1.UI/PixelShaderEffect{T}.ICanvasImage.cs @@ -1,7 +1,11 @@ using System.Numerics; -using System.Runtime.InteropServices; using ABI.Microsoft.Graphics.Canvas; using ComputeSharp.D2D1.Extensions; +#if WINDOWS_UWP +using ComputeSharp.D2D1.Uwp.Helpers; +#else +using ComputeSharp.D2D1.WinUI.Helpers; +#endif using ComputeSharp.Interop; using TerraFX.Interop.Windows; using Windows.Foundation; @@ -33,19 +37,15 @@ unsafe partial class PixelShaderEffect { using ReferenceTracker.Lease _0 = GetReferenceTracker().GetLease(); - using ComPtr resourceCreatorUnknown = default; using ComPtr resourceCreatorAbi = default; // Get the ABI.Microsoft.Graphics.Canvas.ICanvasResourceCreator object from the input interface - resourceCreatorUnknown.Attach((IUnknown*)Marshal.GetIUnknownForObject(resourceCreator)); - resourceCreatorUnknown.CopyTo(resourceCreatorAbi.GetAddressOf()).Assert(); + RcwMarshaller.QueryInterface(resourceCreator, resourceCreatorAbi.GetAddressOf()).Assert(); - using ComPtr canvasImageInteropUnknown = default; using ComPtr canvasImageInterop = default; // Get the ICanvasImageInterop object from the current instance - canvasImageInteropUnknown.Attach((IUnknown*)Marshal.GetIUnknownForObject(this)); - canvasImageInteropUnknown.CopyTo(canvasImageInterop.GetAddressOf()).Assert(); + RcwMarshaller.QueryInterface(this, canvasImageInterop.GetAddressOf()).Assert(); Rect bounds; diff --git a/src/ComputeSharp.D2D1.UI/PixelShaderEffect{T}.ICanvasImageInterop.cs b/src/ComputeSharp.D2D1.UI/PixelShaderEffect{T}.ICanvasImageInterop.cs index d68da8ab..a425de9e 100644 --- a/src/ComputeSharp.D2D1.UI/PixelShaderEffect{T}.ICanvasImageInterop.cs +++ b/src/ComputeSharp.D2D1.UI/PixelShaderEffect{T}.ICanvasImageInterop.cs @@ -7,8 +7,10 @@ using ComputeSharp.D2D1.Interop; using ComputeSharp.D2D1.Interop.Effects; #if WINDOWS_UWP using ComputeSharp.D2D1.Uwp.Extensions; +using ComputeSharp.D2D1.Uwp.Helpers; #else using ComputeSharp.D2D1.WinUI.Extensions; +using ComputeSharp.D2D1.WinUI.Helpers; #endif using ComputeSharp.Interop; using TerraFX.Interop.DirectX; @@ -361,14 +363,10 @@ unsafe partial class PixelShaderEffect } else { - using ComPtr canvasImageInteropUnknown = default; using ComPtr canvasImageInterop = default; - // Get the underlying IUnknown object for the current source - canvasImageInteropUnknown.Attach((IUnknown*)Marshal.GetIUnknownForObject(source)); - // Convert to ICanvasImageInterop (this must always succeed, and throws if it doesn't) - canvasImageInteropUnknown.CopyTo(canvasImageInterop.GetAddressOf()).Assert(); + RcwMarshaller.QueryInterface(source, canvasImageInterop.GetAddressOf()).Assert(); using ComPtr d2D1Image = default; diff --git a/src/ComputeSharp.D2D1.UI/PixelShaderEffect{T}.Properties.Interop.Sources.cs b/src/ComputeSharp.D2D1.UI/PixelShaderEffect{T}.Properties.Interop.Sources.cs index 0f622d59..87198f0e 100644 --- a/src/ComputeSharp.D2D1.UI/PixelShaderEffect{T}.Properties.Interop.Sources.cs +++ b/src/ComputeSharp.D2D1.UI/PixelShaderEffect{T}.Properties.Interop.Sources.cs @@ -6,9 +6,11 @@ using ComputeSharp.D2D1.Extensions; #if WINDOWS_UWP using ComputeSharp.D2D1.Uwp.Buffers; using ComputeSharp.D2D1.Uwp.Extensions; +using ComputeSharp.D2D1.Uwp.Helpers; #else using ComputeSharp.D2D1.WinUI.Buffers; using ComputeSharp.D2D1.WinUI.Extensions; +using ComputeSharp.D2D1.WinUI.Helpers; #endif using ComputeSharp.Interop; using TerraFX.Interop.DirectX; @@ -136,15 +138,10 @@ unsafe partial class PixelShaderEffect if (value is not null) { - using ComPtr canvasImageUnknown = default; - - // Get the underlying IUnknown object for the input source - canvasImageUnknown.Attach((IUnknown*)Marshal.GetIUnknownForObject(value)); - using ComPtr canvasImageInterop = default; // Try to get the ICanvasImageInterop interface from the input source - HRESULT hresult = canvasImageUnknown.CopyTo(canvasImageInterop.GetAddressOf()); + HRESULT hresult = RcwMarshaller.QueryInterface(value, canvasImageInterop.GetAddressOf()); if (!Win32.SUCCEEDED(hresult)) {