Implement CCW retrieval logic from D2D1TransformMapper<T>
このコミットが含まれているのは:
1 親
edeb905442
コミット
557e3fec03
2ファイル変更
+172
-20
検索条件に一致するファイルはありません
@@ -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;
|
||||
/// <see cref="ICustomQueryInterface"/> interface, and this can then be passed to an existing D2D1 effect instance.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
public abstract unsafe class D2D1TransformMapper<T> : ID2D1TransformMapperInterop
|
||||
public abstract unsafe class D2D1TransformMapper<T> : ICustomQueryInterface, ID2D1TransformMapperInterop
|
||||
where T : unmanaged, ID2D1PixelShader
|
||||
{
|
||||
/// <summary>
|
||||
/// The <see cref="D2D1TransformMapperImpl"/> object wrapped by the current instance.
|
||||
/// </summary>
|
||||
private ComPtr<D2D1TransformMapperImpl> d2D1TransformMapperImpl;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="D2D1TransformMapper{T}"/> instance.
|
||||
/// </summary>
|
||||
public D2D1TransformMapper()
|
||||
{
|
||||
fixed (D2D1TransformMapperImpl** d2D1TransformMapperImpl = this.d2D1TransformMapperImpl)
|
||||
{
|
||||
D2D1TransformMapperImpl.Factory(this, d2D1TransformMapperImpl);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Releases the underlying <c>ID2D1TransformMapper</c> object.
|
||||
/// </summary>
|
||||
~D2D1TransformMapper()
|
||||
{
|
||||
this.d2D1TransformMapperImpl.Dispose();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <para>
|
||||
/// 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<T> : ID2D1TransformMapperIntero
|
||||
/// </remarks>
|
||||
public abstract void MapInvalidOutput(int inputIndex, in Rectangle invalidInput, out Rectangle invalidOutput);
|
||||
|
||||
/// <summary>
|
||||
/// Gets the underlying <see cref="ID2D1TransformMapper"/> object.
|
||||
/// </summary>
|
||||
/// <param name="transformMapper">The underlying <see cref="ID2D1TransformMapper"/> object.</param>
|
||||
[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);
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
void ID2D1TransformMapperInterop.MapInputsToOutput(
|
||||
ID2D1DrawInfoUpdateContext* d2D1DrawInfoUpdateContext,
|
||||
|
||||
+85
-19
@@ -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
|
||||
/// </summary>
|
||||
private GCHandle transformMapperHandle;
|
||||
|
||||
/// <summary>
|
||||
/// The <see cref="System.Threading.SpinLock"/> instance to synchronize accesses to this instance.
|
||||
/// </summary>
|
||||
private SpinLock spinLock;
|
||||
|
||||
/// <summary>
|
||||
/// The factory method for <see cref="D2D1TransformMapperImpl"/> instances.
|
||||
/// </summary>
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a reference to the <see cref="System.Threading.SpinLock"/> instance used to synchronize reference counts.
|
||||
/// </summary>
|
||||
[UnscopedRef]
|
||||
public ref SpinLock SpinLock => ref this.spinLock;
|
||||
|
||||
/// <summary>
|
||||
/// Ensures the target of the current CCW is tracked and is keeping it alive.
|
||||
/// </summary>
|
||||
/// <param name="target">The associated <see cref="ID2D1TransformMapperInterop"/> object to track.</param>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void EnsureTargetIsTracked(ID2D1TransformMapperInterop target)
|
||||
{
|
||||
this.transformMapperHandle.Target = target;
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IUnknown.QueryInterface"/>
|
||||
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<IUnknown>()) ||
|
||||
riid->Equals(ID2D1TransformMapper.Guid))
|
||||
bool lockTaken = false;
|
||||
|
||||
this.spinLock.Enter(ref lockTaken);
|
||||
|
||||
try
|
||||
{
|
||||
_ = Interlocked.Increment(ref this.referenceCount);
|
||||
if (riid->Equals(Windows.__uuidof<IUnknown>()) ||
|
||||
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;
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IUnknown.AddRef"/>
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IUnknown.Release"/>
|
||||
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<T> 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;
|
||||
}
|
||||
}
|
||||
新しいイシューから参照
ユーザーをブロックする