diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1TransformMapper{T}.cs b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1TransformMapper{T}.cs
index c9442077..0178b570 100644
--- a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1TransformMapper{T}.cs
+++ b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1TransformMapper{T}.cs
@@ -1,8 +1,13 @@
using System;
using System.Drawing;
+using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
+using ComputeSharp.D2D1.Extensions;
using ComputeSharp.D2D1.Interop.Effects;
using ComputeSharp.D2D1.Shaders.Interop.Effects.TransformMappers;
+using TerraFX.Interop.Windows;
+
+#pragma warning disable CA1033
namespace ComputeSharp.D2D1.Interop;
@@ -89,9 +94,33 @@ namespace ComputeSharp.D2D1.Interop;
/// interface, and this can then be passed to an existing D2D1 effect instance.
///
///
-public abstract unsafe class D2D1TransformMapper : ID2D1TransformMapperInterop
+public abstract unsafe class D2D1TransformMapper : ICustomQueryInterface, ID2D1TransformMapperInterop
where T : unmanaged, ID2D1PixelShader
{
+ ///
+ /// The object wrapped by the current instance.
+ ///
+ private ComPtr d2D1TransformMapperImpl;
+
+ ///
+ /// Creates a new instance.
+ ///
+ public D2D1TransformMapper()
+ {
+ fixed (D2D1TransformMapperImpl** d2D1TransformMapperImpl = this.d2D1TransformMapperImpl)
+ {
+ D2D1TransformMapperImpl.Factory(this, d2D1TransformMapperImpl);
+ }
+ }
+
+ ///
+ /// Releases the underlying ID2D1TransformMapper object.
+ ///
+ ~D2D1TransformMapper()
+ {
+ this.d2D1TransformMapperImpl.Dispose();
+ }
+
///
///
/// Allows a transform to state how it would map a a set of sample rectangles on its input to an output rectangle.
@@ -159,6 +188,63 @@ public abstract unsafe class D2D1TransformMapper : ID2D1TransformMapperIntero
///
public abstract void MapInvalidOutput(int inputIndex, in Rectangle invalidInput, out Rectangle invalidOutput);
+ ///
+ /// Gets the underlying object.
+ ///
+ /// The underlying object.
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ internal void GetD2D1TransformMapper(ID2D1TransformMapper** transformMapper)
+ {
+ bool lockTaken = false;
+
+ this.d2D1TransformMapperImpl.Get()->SpinLock.Enter(ref lockTaken);
+
+ // Whenever the CCW is requested from this object, we also make sure that the current instance is tracked
+ // in the GC handle stored in the CCW. This could've been reset in case there were no other references to
+ // the CCW (see comments in D2D1TransformMapperImpl about this), so here we're assigning a reference to the
+ // current object again to ensure the returned CCW object will keep the instance alive if needed.
+ try
+ {
+ this.d2D1TransformMapperImpl.Get()->EnsureTargetIsTracked(this);
+ this.d2D1TransformMapperImpl.CopyTo(transformMapper).Assert();
+ }
+ finally
+ {
+ this.d2D1TransformMapperImpl.Get()->SpinLock.Exit();
+
+ GC.KeepAlive(this);
+ }
+ }
+
+ ///
+ CustomQueryInterfaceResult ICustomQueryInterface.GetInterface(ref Guid iid, out IntPtr ppv)
+ {
+ fixed (Guid* pIid = &iid)
+ fixed (IntPtr* pPpv = &ppv)
+ {
+ bool lockTaken = false;
+
+ this.d2D1TransformMapperImpl.Get()->SpinLock.Enter(ref lockTaken);
+
+ try
+ {
+ this.d2D1TransformMapperImpl.Get()->EnsureTargetIsTracked(this);
+
+ return (int)this.d2D1TransformMapperImpl.CopyTo(pIid, (void**)pPpv) switch
+ {
+ S.S_OK => CustomQueryInterfaceResult.Handled,
+ _ => CustomQueryInterfaceResult.Failed
+ };
+ }
+ finally
+ {
+ this.d2D1TransformMapperImpl.Get()->SpinLock.Exit();
+
+ GC.KeepAlive(this);
+ }
+ }
+ }
+
///
void ID2D1TransformMapperInterop.MapInputsToOutput(
ID2D1DrawInfoUpdateContext* d2D1DrawInfoUpdateContext,
diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/TransformMappers/D2D1TransformMapperImpl.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/TransformMappers/D2D1TransformMapperImpl.cs
index 00f34f1d..82ec4def 100644
--- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/TransformMappers/D2D1TransformMapperImpl.cs
+++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/TransformMappers/D2D1TransformMapperImpl.cs
@@ -1,4 +1,5 @@
using System;
+using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Threading;
@@ -64,6 +65,11 @@ internal unsafe partial struct D2D1TransformMapperImpl
///
private GCHandle transformMapperHandle;
+ ///
+ /// The instance to synchronize accesses to this instance.
+ ///
+ private SpinLock spinLock;
+
///
/// The factory method for instances.
///
@@ -78,51 +84,111 @@ internal unsafe partial struct D2D1TransformMapperImpl
@this->lpVtbl = Vtbl;
@this->referenceCount = 1;
@this->transformMapperHandle = GCHandle.Alloc(transformMapper);
+ @this->spinLock = new SpinLock(enableThreadOwnerTracking: false);
*d2D1TransformMapperProxy = @this;
}
+ ///
+ /// Gets a reference to the instance used to synchronize reference counts.
+ ///
+ [UnscopedRef]
+ public ref SpinLock SpinLock => ref this.spinLock;
+
+ ///
+ /// Ensures the target of the current CCW is tracked and is keeping it alive.
+ ///
+ /// The associated object to track.
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public void EnsureTargetIsTracked(ID2D1TransformMapperInterop target)
+ {
+ this.transformMapperHandle.Target = target;
+ }
+
///
- public int QueryInterface(Guid* riid, void** ppvObject)
+ private int QueryInterface(Guid* riid, void** ppvObject)
{
if (ppvObject is null)
{
return E.E_POINTER;
}
- if (riid->Equals(Windows.__uuidof()) ||
- riid->Equals(ID2D1TransformMapper.Guid))
+ bool lockTaken = false;
+
+ this.spinLock.Enter(ref lockTaken);
+
+ try
{
- _ = Interlocked.Increment(ref this.referenceCount);
+ if (riid->Equals(Windows.__uuidof()) ||
+ riid->Equals(ID2D1TransformMapper.Guid))
+ {
+ this.referenceCount++;
- *ppvObject = Unsafe.AsPointer(ref this);
+ *ppvObject = Unsafe.AsPointer(ref this);
- return S.S_OK;
+ return S.S_OK;
+ }
+
+ *ppvObject = null;
+
+ return E.E_NOINTERFACE;
+ }
+ finally
+ {
+ this.spinLock.Exit();
}
-
- *ppvObject = null;
-
- return E.E_NOINTERFACE;
}
///
- public uint AddRef()
+ private uint AddRef()
{
- return (uint)Interlocked.Increment(ref this.referenceCount);
+ bool lockTaken = false;
+
+ this.spinLock.Enter(ref lockTaken);
+
+ try
+ {
+ return (uint)++this.referenceCount;
+ }
+ finally
+ {
+ this.spinLock.Exit();
+ }
}
///
- public uint Release()
+ private uint Release()
{
- uint referenceCount = (uint)Interlocked.Decrement(ref this.referenceCount);
+ bool lockTaken = false;
- if (referenceCount == 0)
+ this.spinLock.Enter(ref lockTaken);
+
+ try
{
- this.transformMapperHandle.Free();
+ uint referenceCount = (uint)--this.referenceCount;
- NativeMemory.Free(Unsafe.AsPointer(ref this));
+ // There are two special cases to consider here:
+ // - If the reference count is 1, it means that the D2D1TransformMapper object is the only
+ // thing keeping this CCW alive. In that case, we can reset the GC handle pointing back to it,
+ // so that it can be collected if nobody else is referencing it. This breaks the reference cycle
+ // between the two object, which would have otherwise caused them to remain alive forever.
+ // - If the reference count is 0, the object is simply being destroyed.
+ if (referenceCount == 1)
+ {
+ this.transformMapperHandle.Target = null;
+ }
+ else if (referenceCount == 0)
+ {
+ this.transformMapperHandle.Free();
+
+ NativeMemory.Free(Unsafe.AsPointer(ref this));
+ }
+
+ return referenceCount;
+ }
+ finally
+ {
+ this.spinLock.Exit();
}
-
- return referenceCount;
}
}
\ No newline at end of file