From 805e1555fe815fa820194ec9d932b7d17cefedba Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 18 Feb 2023 23:23:52 +0100 Subject: [PATCH] Implement ICanvasImageInterop with CsWinRT --- .../ICanvasImageInterop.cs | 102 +++++++++++++++++- 1 file changed, 100 insertions(+), 2 deletions(-) diff --git a/src/ComputeSharp.D2D1.UI/ABI.Microsoft.Graphics.Canvas/ICanvasImageInterop.cs b/src/ComputeSharp.D2D1.UI/ABI.Microsoft.Graphics.Canvas/ICanvasImageInterop.cs index a4bb8cb9..b63aed7f 100644 --- a/src/ComputeSharp.D2D1.UI/ABI.Microsoft.Graphics.Canvas/ICanvasImageInterop.cs +++ b/src/ComputeSharp.D2D1.UI/ABI.Microsoft.Graphics.Canvas/ICanvasImageInterop.cs @@ -4,6 +4,10 @@ using System.Runtime.InteropServices; using TerraFX.Interop.DirectX; using TerraFX.Interop.Windows; using TerraFX.Interop; +#if !WINDOWS_UWP +using WinRT; +using WinRT.Interop; +#endif #pragma warning disable CS0649, IDE1006 @@ -79,20 +83,29 @@ internal unsafe struct ICanvasImageInterop } /// - /// The implementation of . + /// The managed implementation of . /// - [ComImport] [Guid("E042D1F7-F9AD-4479-A713-67627EA31863")] +#if WINDOWS_UWP + [ComImport] [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] +#else + [WindowsRuntimeType] + [WindowsRuntimeHelperType(typeof(Interface))] +#endif public interface Interface { /// +#if WINDOWS_UWP [PreserveSig] +#endif [return: NativeTypeName("HRESULT")] int GetDevice(ICanvasDevice** device, WIN2D_GET_DEVICE_ASSOCIATION_TYPE* type); /// +#if WINDOWS_UWP [PreserveSig] +#endif [return: NativeTypeName("HRESULT")] int GetD2DImage( ICanvasDevice* device, @@ -101,5 +114,90 @@ internal unsafe struct ICanvasImageInterop float targetDpi, float* realizeDpi, ID2D1Image** ppImage); + +#if !WINDOWS_UWP + /// + /// The vtable type for . + /// + [Guid("E042D1F7-F9AD-4479-A713-67627EA31863")] + public struct Vftbl + { + /// + /// Allows CsWinRT to retrieve a pointer to the projection vtable (the name is hardcoded by convention). + /// + public static readonly IntPtr AbiToProjectionVftablePtr = InitVtbl(); + + /// + /// Builds the custom method table pointer for . + /// + /// The method table pointer for . + private static IntPtr InitVtbl() + { + Vftbl* lpVtbl = (Vftbl*)ComWrappersSupport.AllocateVtableMemory(typeof(Vftbl), sizeof(Vftbl)); + + lpVtbl->IUnknownVftbl = IUnknownVftbl.AbiToProjectionVftbl; + lpVtbl->GetDevice = &GetDeviceFromAbi; + lpVtbl->GetD2DImage = &GetD2DImageFromAbi; + + return (IntPtr)lpVtbl; + } + + /// + /// The IUnknown vtable. + /// + internal IUnknownVftbl IUnknownVftbl; + + /// + /// Function pointer for . + /// + internal delegate* unmanaged[Stdcall] GetDevice; + + /// + /// Function pointer for . + /// + internal delegate* unmanaged[Stdcall] GetD2DImage; + + /// + [UnmanagedCallersOnly(CallConvs = new[] { typeof(CallConvStdcall) })] + [return: NativeTypeName("HRESULT")] + private static int GetDeviceFromAbi(IntPtr thisPtr, ICanvasDevice** device, WIN2D_GET_DEVICE_ASSOCIATION_TYPE* type) + { + try + { + return ComWrappersSupport.FindObject(thisPtr).GetDevice(device, type); + } + catch (Exception e) + { + ExceptionHelpers.SetErrorInfo(e); + + return Marshal.GetHRForException(e); + } + } + + /// + [UnmanagedCallersOnly(CallConvs = new[] { typeof(CallConvStdcall) })] + [return: NativeTypeName("HRESULT")] + private static int GetD2DImageFromAbi( + IntPtr thisPtr, + ICanvasDevice* device, + ID2D1DeviceContext* deviceContext, + WIN2D_GET_D2D_IMAGE_FLAGS flags, + float targetDpi, + float* realizeDpi, + ID2D1Image** ppImage) + { + try + { + return ComWrappersSupport.FindObject(thisPtr).GetD2DImage(device, deviceContext, flags, targetDpi, realizeDpi, ppImage); + } + catch (Exception e) + { + ExceptionHelpers.SetErrorInfo(e); + + return Marshal.GetHRForException(e); + } + } + } +#endif } } \ No newline at end of file