diff --git a/src/ComputeSharp.D2D1.UI/CanvasEffect.Interop.cs b/src/ComputeSharp.D2D1.UI/CanvasEffect.Interop.cs index 58399b07..e3a6ea74 100644 --- a/src/ComputeSharp.D2D1.UI/CanvasEffect.Interop.cs +++ b/src/ComputeSharp.D2D1.UI/CanvasEffect.Interop.cs @@ -40,7 +40,7 @@ unsafe partial class CanvasEffect { using ComPtr canvasImageInterop = default; - RcwMarshaller.GetNativeObject(GetCanvasImage(), canvasImageInterop.GetAddressOf()).Assert(); + RcwMarshaller.GetNativeInterface(GetCanvasImage(), canvasImageInterop.GetAddressOf()).Assert(); return canvasImageInterop.Get()->GetDevice(device, type); } @@ -56,7 +56,7 @@ unsafe partial class CanvasEffect { using ComPtr canvasImageInterop = default; - RcwMarshaller.GetNativeObject(GetCanvasImage(), canvasImageInterop.GetAddressOf()).Assert(); + RcwMarshaller.GetNativeInterface(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 f5fc2b5e..3adfae3c 100644 --- a/src/ComputeSharp.D2D1.UI/Helpers/RcwMarshaller.cs +++ b/src/ComputeSharp.D2D1.UI/Helpers/RcwMarshaller.cs @@ -2,11 +2,13 @@ using System; using System.Diagnostics.CodeAnalysis; #if WINDOWS_UWP using System.Runtime.InteropServices; +using ComputeSharp.D2D1.Extensions; #endif using TerraFX.Interop.Windows; #if !WINDOWS_UWP using WinRT; #endif +using IInspectable = TerraFX.Interop.WinRT.IInspectable; #if WINDOWS_UWP namespace ComputeSharp.D2D1.Uwp.Helpers; @@ -17,7 +19,7 @@ namespace ComputeSharp.D2D1.WinUI.Helpers; /// /// A helper type to handle marshalling of RCW instances. /// -internal static class RcwMarshaller +internal static unsafe class RcwMarshaller { /// /// Gets or creates a managed object of a specified type for an input native object. @@ -25,14 +27,15 @@ internal static class RcwMarshaller /// 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<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.NonPublicConstructors | DynamicallyAccessedMemberTypes.Interfaces)] T>(IUnknown* nativeObject) + /// This method should only be called with being a projected interface type. + public static T GetOrCreateManagedInterface(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); + return MarshalInterface.FromAbi((IntPtr)nativeObject); #endif } @@ -42,15 +45,21 @@ internal static class RcwMarshaller /// The type of managed object to unwrap. /// The input RCW instance to unwrap. /// A pointer to the resulting native object to retrieve. - public static unsafe void GetNativeObject<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.NonPublicConstructors | DynamicallyAccessedMemberTypes.Interfaces)] T>(T managedObject, IUnknown** nativeObject) + /// This method should only be called with being a concrete projected type. + public static void GetNativeObject<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.NonPublicConstructors | DynamicallyAccessedMemberTypes.Interfaces)] T>(T managedObject, IInspectable** nativeObject) where T : class { #if WINDOWS_UWP - // On UWP, due to built-in COM/WinRT support, Marshal.GetIUnknownForObject can handle all the logic - *nativeObject = (IUnknown*)Marshal.GetIUnknownForObject(managedObject); + using ComPtr unknownObject = default; + + // On UWP, due to built-in COM/WinRT support, Marshal.GetIUnknownForObject can handle all the logic. + // We get back an IUnknown* pointer, so we need to QueryInterface for IInspectable* ourselves. + unknownObject.Attach((IUnknown*)Marshal.GetIUnknownForObject(managedObject)); + + unknownObject.CopyTo(nativeObject).Assert(); #else // On WinUI 3, delegate the RCW unwrapping or CCW creation logic to CsWinRT's APIs - *nativeObject = (IUnknown*)MarshalInspectable.FromManaged(managedObject); + *nativeObject = (IInspectable*)MarshalInspectable.FromManaged(managedObject); #endif } @@ -62,13 +71,22 @@ internal static class RcwMarshaller /// The input RCW instance to unwrap. /// A pointer to the resulting native object to retrieve. /// The for the operation. - public static unsafe HRESULT GetNativeObject<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.NonPublicConstructors | DynamicallyAccessedMemberTypes.Interfaces)] TFrom, TTo>(TFrom managedObject, TTo** nativeObject) + /// This method should only be called with being a projected interface type. + public static HRESULT GetNativeInterface(TFrom managedObject, TTo** nativeObject) where TFrom : class where TTo : unmanaged // IUnknown { using ComPtr unknownObject = default; - GetNativeObject(managedObject, unknownObject.GetAddressOf()); +#if WINDOWS_UWP + unknownObject.Attach((IUnknown*)Marshal.GetIUnknownForObject(managedObject)); + + unknownObject.CopyTo(nativeObject).Assert(); +#else + // Here we only want to get an IUnknown* pointer for a given interface, and then we'll do + // QueryInterface ourselves to get the target COM type. So MarshalInterface is fine. + unknownObject.Attach((IUnknown*)MarshalInterface.FromManaged(managedObject)); +#endif return unknownObject.CopyTo(nativeObject); } diff --git a/src/ComputeSharp.D2D1.UI/Helpers/ResourceManager.cs b/src/ComputeSharp.D2D1.UI/Helpers/ResourceManager.cs index 2e9b9fe1..0a671c36 100644 --- a/src/ComputeSharp.D2D1.UI/Helpers/ResourceManager.cs +++ b/src/ComputeSharp.D2D1.UI/Helpers/ResourceManager.cs @@ -52,7 +52,7 @@ internal static unsafe class ResourceManager wrapper: wrapperInspectable.GetAddressOf()).Assert(); // Get the runtime-provided RCW for the resulting WinRT wrapper - return RcwMarshaller.GetOrCreateManagedObject((IUnknown*)wrapperInspectable.Get()); + return RcwMarshaller.GetOrCreateManagedInterface((IUnknown*)wrapperInspectable.Get()); } /// @@ -70,7 +70,7 @@ internal static unsafe class ResourceManager using ComPtr wrapperInspectable = default; // Unwrap the input wrapper and get an IInspectable* object - RcwMarshaller.GetNativeObject(wrapper, wrapperInspectable.GetAddressOf()).Assert(); + RcwMarshaller.GetNativeObject(wrapper, wrapperInspectable.GetAddressOf()); // Register this pair of native resource and inspectable wrapper canvasFactoryNative.Get()->RegisterWrapper(resource, wrapperInspectable.Get()).Assert(); @@ -128,7 +128,7 @@ internal static unsafe class ResourceManager // For instance, this will ensure the following call will work fine in unpackaged apps. ICanvasFactoryNative.Interface canvasDeviceActivationFactory = CanvasDevice.As(); - *factoryNative = (ICanvasFactoryNative*)MarshalInspectable.FromManaged(canvasDeviceActivationFactory); + *factoryNative = (ICanvasFactoryNative*)MarshalInterface.FromManaged(canvasDeviceActivationFactory); #endif } } diff --git a/src/ComputeSharp.D2D1.UI/PixelShaderEffect{T}.ICanvasEffect.cs b/src/ComputeSharp.D2D1.UI/PixelShaderEffect{T}.ICanvasEffect.cs index ac6a5cc2..a55b2506 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.GetNativeObject(sourceEffect, canvasEffectAbi.GetAddressOf()).Assert(); + RcwMarshaller.GetNativeInterface(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.GetNativeObject(sourceEffects[i], canvasEffects[i].GetAddressOf()).Assert(); + RcwMarshaller.GetNativeInterface(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.GetNativeObject(resourceCreator, resourceCreatorWithDpi).Assert(); + RcwMarshaller.GetNativeInterface(resourceCreator, resourceCreatorWithDpi).Assert(); // Get the ICanvasImageInterop object from the current instance - RcwMarshaller.GetNativeObject(this, canvasImageInterop).Assert(); + RcwMarshaller.GetNativeInterface(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 be000836..140d13eb 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.GetNativeObject(resourceCreator, resourceCreatorAbi.GetAddressOf()).Assert(); + RcwMarshaller.GetNativeInterface(resourceCreator, resourceCreatorAbi.GetAddressOf()).Assert(); using ComPtr canvasImageInterop = default; // Get the ICanvasImageInterop object from the current instance - RcwMarshaller.GetNativeObject(this, canvasImageInterop.GetAddressOf()).Assert(); + RcwMarshaller.GetNativeInterface(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 9e9c1ffa..ddee4f0d 100644 --- a/src/ComputeSharp.D2D1.UI/PixelShaderEffect{T}.ICanvasImageInterop.cs +++ b/src/ComputeSharp.D2D1.UI/PixelShaderEffect{T}.ICanvasImageInterop.cs @@ -373,7 +373,7 @@ unsafe partial class PixelShaderEffect using ComPtr canvasImageInterop = default; // Convert to ICanvasImageInterop (this must always succeed, and throws if it doesn't) - RcwMarshaller.GetNativeObject(source, canvasImageInterop.GetAddressOf()).Assert(); + RcwMarshaller.GetNativeInterface(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 264b610a..a24bdd4d 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.GetNativeObject(value, canvasImageInterop.GetAddressOf()); + HRESULT hresult = RcwMarshaller.GetNativeInterface(value, canvasImageInterop.GetAddressOf()); if (!Win32.SUCCEEDED(hresult)) {