using System; using System.Runtime.InteropServices; using System.Runtime.InteropServices.WindowsRuntime; using Microsoft.Graphics.Canvas; using TerraFX.Interop.DirectX; using TerraFX.Interop.Windows; #nullable enable namespace ComputeSharp.D2D1.UI.Tests.Helpers; /// /// A helper type to marshal managed objects in unit tests. /// internal static class Win2DHelper { /// /// Gets the managed wrapper for a given object. /// /// The input object to get a wrapper for. /// The realization instance to use. /// The resulting managed wrapper for . public static unsafe object GetOrCreate(ID2D1Image* d2D1Image, CanvasDevice? canvasDevice = null) { IActivationFactory activationFactory = WindowsRuntimeMarshal.GetActivationFactory(typeof(CanvasDevice)); using ComPtr activationFactoryUnknown = default; activationFactoryUnknown.Attach((IUnknown*)Marshal.GetIUnknownForObject(activationFactory)); using ComPtr canvasFactoryNativeUnknown = default; Guid uuidOfCanvasFactoryNativeUnknown = new("695C440D-04B3-4EDD-BFD9-63E51E9F7202"); //Get the ICanvasFactoryNative object int hresult = activationFactoryUnknown.CopyTo(&uuidOfCanvasFactoryNativeUnknown, (void**)canvasFactoryNativeUnknown.GetAddressOf()); Marshal.ThrowExceptionForHR(hresult); using ComPtr canvasDeviceUnknown = default; if (canvasDevice is not null) { canvasDeviceUnknown.Attach((IUnknown*)Marshal.GetIUnknownForObject(canvasDevice)); } using ComPtr canvasDeviceInterfaceUnknown = default; if (canvasDevice is not null) { Guid uuidOfCanvasDeviceInterface = new("A27F0B5D-EC2C-4D4F-948F-0AA1E95E33E6"); // Get the ICanvasDevice object (as an IUnknown* as well) hresult = canvasDeviceUnknown.CopyTo(&uuidOfCanvasDeviceInterface, (void**)canvasDeviceInterfaceUnknown.GetAddressOf()); } using ComPtr wrapperUnknown = default; // Invoke GetOrCreate to get the inspectable wrapper hresult = ((delegate* unmanaged[Stdcall])(*(void***)canvasFactoryNativeUnknown.Get())[6])( canvasFactoryNativeUnknown.Get(), canvasDeviceInterfaceUnknown.Get(), (IUnknown*)d2D1Image, 0, wrapperUnknown.GetAddressOf()); Marshal.ThrowExceptionForHR(hresult); // Get or create a managed object for the native object return Marshal.GetObjectForIUnknown((IntPtr)wrapperUnknown.Get()); } /// /// Gets the D2D image from a given object. /// /// The input object. /// The realization instance to use. /// The resulting object. public static unsafe void GetD2DImage(ICanvasImage canvasImage, CanvasDevice canvasDevice, ID2D1Image** d2D1Image) { using ComPtr effectUnknown = default; effectUnknown.Attach((IUnknown*)Marshal.GetIUnknownForObject(canvasImage)); using ComPtr canvasImageInteropUnknown = default; Guid uuidOfCanvasImageInterop = new("E042D1F7-F9AD-4479-A713-67627EA31863"); // Get the ICanvasImageInterop object (as an IUnknown*) int hresult = effectUnknown.CopyTo(&uuidOfCanvasImageInterop, (void**)canvasImageInteropUnknown.GetAddressOf()); Marshal.ThrowExceptionForHR(hresult); using ComPtr canvasDeviceUnknown = default; canvasDeviceUnknown.Attach((IUnknown*)Marshal.GetIUnknownForObject(canvasDevice)); using ComPtr canvasDeviceInterfaceUnknown = default; Guid uuidOfCanvasDeviceInterface = new("A27F0B5D-EC2C-4D4F-948F-0AA1E95E33E6"); // Get the ICanvasDevice object (as an IUnknown* as well) hresult = canvasDeviceUnknown.CopyTo(&uuidOfCanvasDeviceInterface, (void**)canvasDeviceInterfaceUnknown.GetAddressOf()); Marshal.ThrowExceptionForHR(hresult); // Invoke GetD2DImage (passing NEVER_INSERT_DPI_COMPENSATION | MINIMAL_REALIZATION | ALLOW_NULL_EFFECT_INPUTS) hresult = ((delegate* unmanaged[Stdcall])(*(void***)canvasImageInteropUnknown.Get())[4])( canvasImageInteropUnknown.Get(), canvasDeviceInterfaceUnknown.Get(), null, 4 | 8 | 16, 0, null, d2D1Image); Marshal.ThrowExceptionForHR(hresult); } }