diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1ResourceTextureManager.cs b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1ResourceTextureManager.cs index e41642ac..6f9d2404 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1ResourceTextureManager.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1ResourceTextureManager.cs @@ -406,7 +406,7 @@ public sealed unsafe class D2D1ResourceTextureManager : ICustomQueryInterface fixed (Guid* pIid = &iid) fixed (IntPtr* pPpv = &ppv) { - int hresult = this.d2D1ResourceManagerImpl.Get()->QueryInterface(pIid, (void**)pPpv); + int hresult = this.d2D1ResourceManagerImpl.CopyTo(pIid, (void**)pPpv); GC.KeepAlive(this); diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/TransformMappers/ID2D1TransformMapperProxy.ID2D1TransformMapper.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/TransformMappers/ID2D1TransformMapperProxy.ID2D1TransformMapper.cs new file mode 100644 index 00000000..b14498eb --- /dev/null +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/TransformMappers/ID2D1TransformMapperProxy.ID2D1TransformMapper.cs @@ -0,0 +1,212 @@ +using System; +using System.Drawing; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using ComputeSharp.D2D1.Extensions; +using ComputeSharp.D2D1.Interop.Effects; +using TerraFX.Interop.Windows; +#if !NET6_0_OR_GREATER +using UnmanagedCallersOnlyAttribute = ComputeSharp.NetStandard.System.Runtime.InteropServices.UnmanagedCallersOnlyAttribute; +#endif + +namespace ComputeSharp.D2D1.Shaders.Interop.Effects.TransformMappers; + +/// +unsafe partial struct ID2D1TransformMapperProxy +{ +#if !NET6_0_OR_GREATER + /// + [UnmanagedFunctionPointer(CallingConvention.StdCall)] + private delegate int QueryInterfaceDelegate(ID2D1TransformMapperProxy* @this, Guid* riid, void** ppvObject); + + /// + [UnmanagedFunctionPointer(CallingConvention.StdCall)] + private delegate uint AddRefDelegate(ID2D1TransformMapperProxy* @this); + + /// + [UnmanagedFunctionPointer(CallingConvention.StdCall)] + private delegate uint ReleaseDelegate(ID2D1TransformMapperProxy* @this); + + /// + [UnmanagedFunctionPointer(CallingConvention.StdCall)] + private delegate int MapInputRectsToOutputRectDelegate( + ID2D1TransformMapperProxy* @this, + ID2D1DrawInfoUpdateContext* d2D1DrawInfoUpdateContext, + RECT* inputRects, + RECT* inputOpaqueSubRects, + uint inputRectCount, + RECT* outputRect, + RECT* outputOpaqueSubRect); + + /// + [UnmanagedFunctionPointer(CallingConvention.StdCall)] + private delegate int MapOutputRectToInputRectsDelegate( + ID2D1TransformMapperProxy* @this, + RECT* outputRect, + RECT* inputRects, + uint inputRectsCount); + + /// + [UnmanagedFunctionPointer(CallingConvention.StdCall)] + private delegate int MapInvalidRectDelegate( + ID2D1TransformMapperProxy* @this, + uint inputIndex, + RECT invalidInputRect, + RECT* invalidOutputRect); + + /// + /// A cached instance wrapping . + /// + private static readonly QueryInterfaceDelegate QueryInterfaceWrapper = QueryInterface; + + /// + /// A cached instance wrapping . + /// + private static readonly AddRefDelegate AddRefWrapper = AddRef; + + /// + /// A cached instance wrapping . + /// + private static readonly ReleaseDelegate ReleaseWrapper = Release; + + /// + /// A cached instance wrapping . + /// + private static readonly MapInputRectsToOutputRectDelegate MapInputRectsToOutputRectWrapper = MapInputRectsToOutputRect; + + /// + /// A cached instance wrapping . + /// + private static readonly MapOutputRectToInputRectsDelegate MapOutputRectToInputRectsWrapper = MapOutputRectToInputRects; + + /// + /// A cached instance wrapping . + /// + private static readonly MapInvalidRectDelegate MapInvalidRectWrapper = MapInvalidRect; +#endif + + /// + [UnmanagedCallersOnly] + private static int QueryInterface(ID2D1TransformMapperProxy* @this, Guid* riid, void** ppvObject) + { + return @this->QueryInterface(riid, ppvObject); + } + + /// + [UnmanagedCallersOnly] + private static uint AddRef(ID2D1TransformMapperProxy* @this) + { + return @this->AddRef(); + } + + /// + [UnmanagedCallersOnly] + private static uint Release(ID2D1TransformMapperProxy* @this) + { + return @this->Release(); + } + + /// + [UnmanagedCallersOnly] + private static int MapInputRectsToOutputRect( + ID2D1TransformMapperProxy* @this, + ID2D1DrawInfoUpdateContext* d2D1DrawInfoUpdateContext, + RECT* inputRects, + RECT* inputOpaqueSubRects, + uint inputRectCount, + RECT* outputRect, + RECT* outputOpaqueSubRect) + { + Span inputs = stackalloc Rectangle[8].Slice(0, (int)inputRectCount); + Span opaqueInputs = stackalloc Rectangle[8].Slice(0, (int)inputRectCount); + + for (int i = 0; i < (int)inputRectCount; i++) + { + inputs[i] = inputRects[i].ToRectangle(); + opaqueInputs[i] = inputOpaqueSubRects[i].ToRectangle(); + } + + Rectangle output; + Rectangle opaqueOutput; + + // Invoke MapInputsToOutput and handle exceptions so they don't cross the ABI boundary + try + { + Unsafe.As(@this->transformMapperHandle.Target!).MapInputsToOutput( + d2D1DrawInfoUpdateContext, + inputs, + opaqueInputs, + out output, + out opaqueOutput); + } + catch (Exception e) + { + return e.HResult; + } + + *outputRect = output.ToRECT(); + *outputOpaqueSubRect = opaqueOutput.ToRECT(); + + return S.S_OK; + } + + /// + [UnmanagedCallersOnly] + private static int MapOutputRectToInputRects( + ID2D1TransformMapperProxy* @this, + RECT* outputRect, + RECT* inputRects, + uint inputRectsCount) + { + Rectangle output = outputRect->ToRectangle(); + Span inputs = stackalloc Rectangle[8].Slice(0, (int)inputRectsCount); + + for (int i = 0; i < (int)inputRectsCount; i++) + { + inputs[i] = inputRects[i].ToRectangle(); + } + + // Handle exceptions, as mentioned above + try + { + Unsafe.As(@this->transformMapperHandle.Target!).MapOutputToInputs(in output, inputs); + } + catch (Exception e) + { + return e.HResult; + } + + for (int i = 0; i < (int)inputRectsCount; i++) + { + inputRects[i] = inputs[i].ToRECT(); + } + + return S.S_OK; + } + + /// + [UnmanagedCallersOnly] + private static int MapInvalidRect( + ID2D1TransformMapperProxy* @this, + uint inputIndex, + RECT invalidInputRect, + RECT* invalidOutputRect) + { + Rectangle invalidInput = invalidInputRect.ToRectangle(); + Rectangle invalidOutput; + + // Handle exceptions once again + try + { + Unsafe.As(@this->transformMapperHandle.Target!).MapInvalidOutput((int)inputIndex, invalidInput, out invalidOutput); + } + catch (Exception e) + { + return e.HResult; + } + + *invalidOutputRect = invalidOutput.ToRECT(); + + return S.S_OK; + } +} \ No newline at end of file diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/TransformMappers/ID2D1TransformMapperProxy.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/TransformMappers/ID2D1TransformMapperProxy.cs new file mode 100644 index 00000000..9ef4435c --- /dev/null +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/TransformMappers/ID2D1TransformMapperProxy.cs @@ -0,0 +1,128 @@ +using System; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using System.Threading; +using ComputeSharp.D2D1.Interop.Effects; +using TerraFX.Interop.Windows; +#if NET6_0_OR_GREATER +using RuntimeHelpers = System.Runtime.CompilerServices.RuntimeHelpers; +#else +using RuntimeHelpers = ComputeSharp.D2D1.NetStandard.System.Runtime.CompilerServices.RuntimeHelpers; +#endif + +namespace ComputeSharp.D2D1.Shaders.Interop.Effects.TransformMappers; + +/// +/// An implementation of the interface wrapping . +/// +internal unsafe partial struct ID2D1TransformMapperProxy +{ + /// + /// The shared method table pointer for all instances. + /// + private static readonly void** Vtbl = InitVtbl(); + + /// + /// Builds the custom method table pointer for . + /// + /// The method table pointer for . + private static void** InitVtbl() + { + void** lpVtbl = (void**)RuntimeHelpers.AllocateTypeAssociatedMemory(typeof(ID2D1TransformMapperProxy), sizeof(void*) * 6); + +#if NET6_0_OR_GREATER + lpVtbl[0] = (delegate* unmanaged)&QueryInterface; + lpVtbl[1] = (delegate* unmanaged)&AddRef; + lpVtbl[2] = (delegate* unmanaged)&Release; + lpVtbl[3] = (delegate* unmanaged)&MapInputRectsToOutputRect; + lpVtbl[4] = (delegate* unmanaged)&MapOutputRectToInputRects; + lpVtbl[5] = (delegate* unmanaged)&MapInvalidRect; +#else + lpVtbl[0] = (void*)Marshal.GetFunctionPointerForDelegate(QueryInterfaceWrapper); + lpVtbl[1] = (void*)Marshal.GetFunctionPointerForDelegate(AddRefWrapper); + lpVtbl[2] = (void*)Marshal.GetFunctionPointerForDelegate(ReleaseWrapper); + lpVtbl[3] = (void*)Marshal.GetFunctionPointerForDelegate(MapInputRectsToOutputRectWrapper); + lpVtbl[4] = (void*)Marshal.GetFunctionPointerForDelegate(MapOutputRectToInputRectsWrapper); + lpVtbl[5] = (void*)Marshal.GetFunctionPointerForDelegate(MapInvalidRectWrapper); +#endif + + return lpVtbl; + } + + /// + /// The vtable pointer for the current instance. + /// + private void** lpVtbl; + + /// + /// The current reference count for the object (from ). + /// + private volatile int referenceCount; + + /// + /// A to the wrapped instance. + /// + private GCHandle transformMapperHandle; + + /// + /// The factory method for instances. + /// + /// The input instance to wrap. + /// The resulting instance. + public static void Factory( + ID2D1TransformMapperInterop transformMapper, + ID2D1TransformMapperProxy** d2D1TransformMapperProxy) + { + ID2D1TransformMapperProxy* @this = (ID2D1TransformMapperProxy*)NativeMemory.Alloc((nuint)sizeof(ID2D1TransformMapperProxy)); + + @this->lpVtbl = Vtbl; + @this->referenceCount = 1; + @this->transformMapperHandle = GCHandle.Alloc(transformMapper); + + *d2D1TransformMapperProxy = @this; + } + + /// + public int QueryInterface(Guid* riid, void** ppvObject) + { + if (ppvObject is null) + { + return E.E_POINTER; + } + + if (riid->Equals(Windows.__uuidof()) || + riid->Equals(ID2D1TransformMapper.Guid)) + { + _ = Interlocked.Increment(ref this.referenceCount); + + *ppvObject = Unsafe.AsPointer(ref this); + + return S.S_OK; + } + + *ppvObject = null; + + return E.E_NOINTERFACE; + } + + /// + public uint AddRef() + { + return (uint)Interlocked.Increment(ref this.referenceCount); + } + + /// + public uint Release() + { + uint referenceCount = (uint)Interlocked.Decrement(ref this.referenceCount); + + if (referenceCount == 0) + { + this.transformMapperHandle.Free(); + + NativeMemory.Free(Unsafe.AsPointer(ref this)); + } + + return referenceCount; + } +} \ No newline at end of file