From 2578cf30288bc561e1a07de1053ec9f68200d034 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 25 Mar 2023 21:46:23 +0100 Subject: [PATCH] Support CsWinRT CCW marshalling in WinUI project --- .../CanvasEffect.Interop.cs | 4 +-- .../Helpers/RcwMarshaller.cs | 30 +++++++++++++++---- .../Helpers/ResourceManager.cs | 5 +--- .../PixelShaderEffect{T}.ICanvasEffect.cs | 8 ++--- .../PixelShaderEffect{T}.ICanvasImage.cs | 4 +-- ...ixelShaderEffect{T}.ICanvasImageInterop.cs | 2 +- ...derEffect{T}.Properties.Interop.Sources.cs | 2 +- 7 files changed, 35 insertions(+), 20 deletions(-) diff --git a/src/ComputeSharp.D2D1.UI/CanvasEffect.Interop.cs b/src/ComputeSharp.D2D1.UI/CanvasEffect.Interop.cs index e8cdf977..0da3a77e 100644 --- a/src/ComputeSharp.D2D1.UI/CanvasEffect.Interop.cs +++ b/src/ComputeSharp.D2D1.UI/CanvasEffect.Interop.cs @@ -39,7 +39,7 @@ unsafe partial class CanvasEffect { using ComPtr canvasImageInterop = default; - RcwMarshaller.QueryInterface(GetCanvasImage(), canvasImageInterop.GetAddressOf()).Assert(); + RcwMarshaller.GetNativeObject(GetCanvasImage(), canvasImageInterop.GetAddressOf()).Assert(); return canvasImageInterop.Get()->GetDevice(device, type); } @@ -55,7 +55,7 @@ unsafe partial class CanvasEffect { using ComPtr canvasImageInterop = default; - RcwMarshaller.QueryInterface(GetCanvasImage(), canvasImageInterop.GetAddressOf()).Assert(); + RcwMarshaller.GetNativeObject(GetCanvasImage(), canvasImageInterop.GetAddressOf()).Assert(); return canvasImageInterop.Get()->GetD2DImage(device, deviceContext, flags, targetDpi, realizeDpi, ppImage); } diff --git a/src/ComputeSharp.D2D1.UI/Helpers/RcwMarshaller.cs b/src/ComputeSharp.D2D1.UI/Helpers/RcwMarshaller.cs index 1fff576c..93d99753 100644 --- a/src/ComputeSharp.D2D1.UI/Helpers/RcwMarshaller.cs +++ b/src/ComputeSharp.D2D1.UI/Helpers/RcwMarshaller.cs @@ -1,3 +1,4 @@ +using System; #if WINDOWS_UWP using System.Runtime.InteropServices; #endif @@ -18,14 +19,33 @@ namespace ComputeSharp.D2D1.WinUI.Helpers; internal static class RcwMarshaller { /// - /// Retrieves the underlying native object for an input RCW and casts it to the specified type. + /// Gets or creates a managed object of a specified type for an input native object. /// /// The interface type to retrieve an instance of. + /// A pointer to the native object to get a managed wrapper for. + /// The resulting managed object wrapping . + public static unsafe T GetOrCreateManagedObject(IUnknown* nativeObject) + where T : class + { +#if WINDOWS_UWP + // On UWP, Marshal.GetObjectForIUnknown handles all the marshalling/wrapping logic + return (T)Marshal.GetObjectForIUnknown((IntPtr)nativeObject); +#else + return MarshalInspectable.FromAbi((IntPtr)nativeObject); +#endif + } + + /// + /// Retrieves the underlying native object for an input RCW and casts it to the specified type. + /// + /// The type of managed object to unwrap. + /// 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 + public static unsafe HRESULT GetNativeObject(TFrom managedObject, TTo** nativeObject) + where TFrom : class + where TTo : unmanaged // IUnknown { using ComPtr unknownObject = default; @@ -34,9 +54,7 @@ internal static class RcwMarshaller 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()); + unknownObject.Attach((IUnknown*)MarshalInspectable.FromManaged(managedObject)); #endif // QueryInterface to the specific interface we need diff --git a/src/ComputeSharp.D2D1.UI/Helpers/ResourceManager.cs b/src/ComputeSharp.D2D1.UI/Helpers/ResourceManager.cs index 0795aac8..798354a8 100644 --- a/src/ComputeSharp.D2D1.UI/Helpers/ResourceManager.cs +++ b/src/ComputeSharp.D2D1.UI/Helpers/ResourceManager.cs @@ -1,5 +1,4 @@ using System; -using System.Runtime.InteropServices; using ABI.Microsoft.Graphics.Canvas; using ComputeSharp.D2D1.Extensions; using TerraFX.Interop.Windows; @@ -67,8 +66,6 @@ internal static class ResourceManager wrapper: wrapperUnknown.GetVoidAddressOf()).Assert(); // Get the runtime-provided RCW for the resulting WinRT wrapper - object wrapper = Marshal.GetObjectForIUnknown((IntPtr)wrapperUnknown.Get()); - - return (IGraphicsEffectSource)wrapper; + return RcwMarshaller.GetOrCreateManagedObject(wrapperUnknown.Get()); } } diff --git a/src/ComputeSharp.D2D1.UI/PixelShaderEffect{T}.ICanvasEffect.cs b/src/ComputeSharp.D2D1.UI/PixelShaderEffect{T}.ICanvasEffect.cs index c3f7d459..ac6a5cc2 100644 --- a/src/ComputeSharp.D2D1.UI/PixelShaderEffect{T}.ICanvasEffect.cs +++ b/src/ComputeSharp.D2D1.UI/PixelShaderEffect{T}.ICanvasEffect.cs @@ -101,7 +101,7 @@ unsafe partial class PixelShaderEffect using ComPtr canvasEffectAbi = default; // Get the ABI.Microsoft.Graphics.Canvas.Effects.ICanvasEffect object from the input interface - RcwMarshaller.QueryInterface(sourceEffect, canvasEffectAbi.GetAddressOf()).Assert(); + RcwMarshaller.GetNativeObject(sourceEffect, canvasEffectAbi.GetAddressOf()).Assert(); Win2D.GetRequiredSourceRectanglesForICanvasImageInterop( resourceCreator: resourceCreatorWithDpi.Get(), @@ -155,7 +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++) { - RcwMarshaller.QueryInterface(sourceEffects[i], canvasEffects[i].GetAddressOf()).Assert(); + RcwMarshaller.GetNativeObject(sourceEffects[i], canvasEffects[i].GetAddressOf()).Assert(); } Rect[] result = new Rect[sourceEffects.Length]; @@ -210,9 +210,9 @@ unsafe partial class PixelShaderEffect ICanvasImageInterop** canvasImageInterop) { // Get the ABI.Microsoft.Graphics.Canvas.ICanvasResourceCreatorWithDpi object from the input interface - RcwMarshaller.QueryInterface(resourceCreator, resourceCreatorWithDpi).Assert(); + RcwMarshaller.GetNativeObject(resourceCreator, resourceCreatorWithDpi).Assert(); // Get the ICanvasImageInterop object from the current instance - RcwMarshaller.QueryInterface(this, canvasImageInterop).Assert(); + RcwMarshaller.GetNativeObject(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 19f26b60..be000836 100644 --- a/src/ComputeSharp.D2D1.UI/PixelShaderEffect{T}.ICanvasImage.cs +++ b/src/ComputeSharp.D2D1.UI/PixelShaderEffect{T}.ICanvasImage.cs @@ -40,12 +40,12 @@ unsafe partial class PixelShaderEffect using ComPtr resourceCreatorAbi = default; // Get the ABI.Microsoft.Graphics.Canvas.ICanvasResourceCreator object from the input interface - RcwMarshaller.QueryInterface(resourceCreator, resourceCreatorAbi.GetAddressOf()).Assert(); + RcwMarshaller.GetNativeObject(resourceCreator, resourceCreatorAbi.GetAddressOf()).Assert(); using ComPtr canvasImageInterop = default; // Get the ICanvasImageInterop object from the current instance - RcwMarshaller.QueryInterface(this, canvasImageInterop.GetAddressOf()).Assert(); + RcwMarshaller.GetNativeObject(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 a425de9e..5f376fdf 100644 --- a/src/ComputeSharp.D2D1.UI/PixelShaderEffect{T}.ICanvasImageInterop.cs +++ b/src/ComputeSharp.D2D1.UI/PixelShaderEffect{T}.ICanvasImageInterop.cs @@ -366,7 +366,7 @@ unsafe partial class PixelShaderEffect using ComPtr canvasImageInterop = default; // Convert to ICanvasImageInterop (this must always succeed, and throws if it doesn't) - RcwMarshaller.QueryInterface(source, canvasImageInterop.GetAddressOf()).Assert(); + RcwMarshaller.GetNativeObject(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 87198f0e..264b610a 100644 --- a/src/ComputeSharp.D2D1.UI/PixelShaderEffect{T}.Properties.Interop.Sources.cs +++ b/src/ComputeSharp.D2D1.UI/PixelShaderEffect{T}.Properties.Interop.Sources.cs @@ -141,7 +141,7 @@ unsafe partial class PixelShaderEffect using ComPtr canvasImageInterop = default; // Try to get the ICanvasImageInterop interface from the input source - HRESULT hresult = RcwMarshaller.QueryInterface(value, canvasImageInterop.GetAddressOf()); + HRESULT hresult = RcwMarshaller.GetNativeObject(value, canvasImageInterop.GetAddressOf()); if (!Win32.SUCCEEDED(hresult)) {