diff --git a/src/ComputeSharp.D2D1.UI/ABI.Microsoft.Graphics.Canvas/ICanvasEffectFactoryNative.cs b/src/ComputeSharp.D2D1.UI/ABI.Microsoft.Graphics.Canvas/ICanvasEffectFactoryNative.cs index 69201dfb..59a3919c 100644 --- a/src/ComputeSharp.D2D1.UI/ABI.Microsoft.Graphics.Canvas/ICanvasEffectFactoryNative.cs +++ b/src/ComputeSharp.D2D1.UI/ABI.Microsoft.Graphics.Canvas/ICanvasEffectFactoryNative.cs @@ -47,7 +47,7 @@ internal unsafe struct ICanvasEffectFactoryNative } /// - /// Creates a new inspectable wrapper for an inpute D2D effect previosly registered through . + /// Creates a new inspectable wrapper for an input D2D effect previosly registered through . /// /// The input canvas device. /// The input native effect to create a wrapper for. diff --git a/src/ComputeSharp.D2D1.UI/ComputeSharp.D2D1.UI.projitems b/src/ComputeSharp.D2D1.UI/ComputeSharp.D2D1.UI.projitems index ebda8706..2bdd3aaf 100644 --- a/src/ComputeSharp.D2D1.UI/ComputeSharp.D2D1.UI.projitems +++ b/src/ComputeSharp.D2D1.UI/ComputeSharp.D2D1.UI.projitems @@ -38,6 +38,7 @@ + diff --git a/src/ComputeSharp.D2D1.UI/PixelShaderEffect{T}.ICanvasEffectFactoryNative.cs b/src/ComputeSharp.D2D1.UI/PixelShaderEffect{T}.ICanvasEffectFactoryNative.cs new file mode 100644 index 00000000..b80457a1 --- /dev/null +++ b/src/ComputeSharp.D2D1.UI/PixelShaderEffect{T}.ICanvasEffectFactoryNative.cs @@ -0,0 +1,72 @@ +using System; +using System.Runtime.InteropServices; +using ABI.Microsoft.Graphics.Canvas; +using ComputeSharp.D2D1.Extensions; +#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 TerraFX.Interop; +using TerraFX.Interop.DirectX; +using TerraFX.Interop.Windows; +using TerraFX.Interop.WinRT; + +#if WINDOWS_UWP +namespace ComputeSharp.D2D1.Uwp; +#else +namespace ComputeSharp.D2D1.WinUI; +#endif + +/// +unsafe partial class PixelShaderEffect +{ + /// + [UnmanagedFunctionPointer(CallingConvention.StdCall)] + [return: NativeTypeName("HRESULT")] + private delegate int CreateWrapperDelegate(ICanvasDevice* device, ID2D1Effect* resource, float dpi, IInspectable** wrapper); + + /// + /// A cached instance wrapping . + /// + private static readonly CreateWrapperDelegate CreateWrapperWrapper = CreateWrapper; + + /// + /// Creates a new instance for an input D2D effect and marshals it as an wrapper. + /// + /// + /// + /// + /// + /// This method always returns (any exceptions will be marshalled by the delegate stub). + private static int CreateWrapper(ICanvasDevice* device, ID2D1Effect* resource, float dpi, IInspectable** wrapper) + { + PixelShaderEffect @this = new(); + + @this.InitializeFromNativeResource(device, resource); + + RcwMarshaller.GetNativeObject(@this, wrapper); + + return S.S_OK; + } + + /// + /// Initializes a instance from an external realized D2D1 effect. + /// + /// + /// + private void InitializeFromNativeResource(ICanvasDevice* device, ID2D1Effect* resource) + { + using ComPtr d2D1Device1 = default; + + // Get the underlying ID2D1Device1 instance for the input device + device->GetD2DDevice(d2D1Device1.GetAddressOf()).Assert(); + + // Initialize the effect state + device->CopyTo(ref this.canvasDevice).Assert(); + d2D1Device1.CopyTo(ref this.d2D1RealizationDevice).Assert(); + resource->CopyTo(ref this.d2D1Effect).Assert(); + } +} \ No newline at end of file