Move 'ComputeSharp.D2D1.WinUI' files to shared project
このコミットが含まれているのは:
1 親
c106c0fd50
コミット
a44b585927
46ファイル変更
+49
-6
検索条件に一致するファイルはありません
@@ -1,43 +0,0 @@
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
using ComputeSharp.Win32;
|
||||
|
||||
#pragma warning disable CS0649, IDE1006
|
||||
|
||||
namespace ABI.Microsoft.Graphics.Canvas;
|
||||
|
||||
/// <summary>
|
||||
/// The native WinRT interface for <see cref="global::Microsoft.Graphics.Canvas.CanvasDevice"/> objects.
|
||||
/// </summary>
|
||||
[NativeTypeName("class ICanvasDevice : IInspectable")]
|
||||
[NativeInheritance("IInspectable")]
|
||||
internal unsafe struct ICanvasDevice
|
||||
{
|
||||
public void** lpVtbl;
|
||||
|
||||
/// <inheritdoc cref="IUnknown.QueryInterface"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
[VtblIndex(0)]
|
||||
public HRESULT QueryInterface([NativeTypeName("const IID &")] Guid* riid, void** ppvObject)
|
||||
{
|
||||
return ((delegate* unmanaged[MemberFunction]<ICanvasDevice*, Guid*, void**, int>)this.lpVtbl[0])((ICanvasDevice*)Unsafe.AsPointer(ref this), riid, ppvObject);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IUnknown.AddRef"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
[VtblIndex(1)]
|
||||
[return: NativeTypeName("ULONG")]
|
||||
public uint AddRef()
|
||||
{
|
||||
return ((delegate* unmanaged[MemberFunction]<ICanvasDevice*, uint>)this.lpVtbl[1])((ICanvasDevice*)Unsafe.AsPointer(ref this));
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IUnknown.Release"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
[VtblIndex(2)]
|
||||
[return: NativeTypeName("ULONG")]
|
||||
public uint Release()
|
||||
{
|
||||
return ((delegate* unmanaged[MemberFunction]<ICanvasDevice*, uint>)this.lpVtbl[2])((ICanvasDevice*)Unsafe.AsPointer(ref this));
|
||||
}
|
||||
}
|
||||
@@ -1,69 +0,0 @@
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using ComputeSharp.Win32;
|
||||
|
||||
#pragma warning disable CS0649, IDE0055, IDE1006
|
||||
|
||||
namespace ABI.Microsoft.Graphics.Canvas.Effects;
|
||||
|
||||
/// <summary>
|
||||
/// The native WinRT interface for <see cref="global::Microsoft.Graphics.Canvas.Effects.ICanvasEffect"/> objects.
|
||||
/// </summary>
|
||||
[NativeTypeName("class ICanvasEffect : IInspectable")]
|
||||
[NativeInheritance("IInspectable")]
|
||||
internal unsafe struct ICanvasEffect : IComObject
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
static Guid* IComObject.IID
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get
|
||||
{
|
||||
ReadOnlySpan<byte> data =
|
||||
[
|
||||
0x8C, 0x6F, 0xF9, 0x0E,
|
||||
0x5E, 0x9B,
|
||||
0xF0, 0x4B,
|
||||
0x99,
|
||||
0xA3,
|
||||
0xAA,
|
||||
0xD8,
|
||||
0xCE,
|
||||
0x53,
|
||||
0xDB,
|
||||
0x55
|
||||
];
|
||||
|
||||
return (Guid*)Unsafe.AsPointer(ref MemoryMarshal.GetReference(data));
|
||||
}
|
||||
}
|
||||
|
||||
public void** lpVtbl;
|
||||
|
||||
/// <inheritdoc cref="IUnknown.QueryInterface"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
[VtblIndex(0)]
|
||||
public HRESULT QueryInterface([NativeTypeName("const IID &")] Guid* riid, void** ppvObject)
|
||||
{
|
||||
return ((delegate* unmanaged[MemberFunction]<ICanvasEffect*, Guid*, void**, int>)this.lpVtbl[0])((ICanvasEffect*)Unsafe.AsPointer(ref this), riid, ppvObject);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IUnknown.AddRef"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
[VtblIndex(1)]
|
||||
[return: NativeTypeName("ULONG")]
|
||||
public uint AddRef()
|
||||
{
|
||||
return ((delegate* unmanaged[MemberFunction]<ICanvasEffect*, uint>)this.lpVtbl[1])((ICanvasEffect*)Unsafe.AsPointer(ref this));
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IUnknown.Release"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
[VtblIndex(2)]
|
||||
[return: NativeTypeName("ULONG")]
|
||||
public uint Release()
|
||||
{
|
||||
return ((delegate* unmanaged[MemberFunction]<ICanvasEffect*, uint>)this.lpVtbl[2])((ICanvasEffect*)Unsafe.AsPointer(ref this));
|
||||
}
|
||||
}
|
||||
-178
@@ -1,178 +0,0 @@
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using ComputeSharp.Win32;
|
||||
using WinRT;
|
||||
using WinRT.Interop;
|
||||
using IInspectable = ComputeSharp.Win32.IInspectable;
|
||||
|
||||
#pragma warning disable CS0649, IDE0055, IDE1006
|
||||
|
||||
namespace ABI.Microsoft.Graphics.Canvas
|
||||
{
|
||||
/// <summary>
|
||||
/// The interop Win2D interface for factories of external effects.
|
||||
/// </summary>
|
||||
[NativeTypeName("class ICanvasEffectFactoryNative : IUnknown")]
|
||||
[NativeInheritance("IUnknown")]
|
||||
internal unsafe struct ICanvasEffectFactoryNative : IComObject
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
public static Guid* IID
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get
|
||||
{
|
||||
ReadOnlySpan<byte> data =
|
||||
[
|
||||
0x1F, 0x1A, 0xBA, 0x29,
|
||||
0xFE, 0x1C,
|
||||
0xC3, 0x44,
|
||||
0x98, 0x4D,
|
||||
0x42,
|
||||
0x6D,
|
||||
0x61,
|
||||
0xB5,
|
||||
0x14,
|
||||
0x27
|
||||
];
|
||||
|
||||
return (Guid*)Unsafe.AsPointer(ref MemoryMarshal.GetReference(data));
|
||||
}
|
||||
}
|
||||
|
||||
public void** lpVtbl;
|
||||
|
||||
/// <inheritdoc cref="IUnknown.QueryInterface"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
[VtblIndex(0)]
|
||||
public HRESULT QueryInterface([NativeTypeName("const IID &")] Guid* riid, void** ppvObject)
|
||||
{
|
||||
return ((delegate* unmanaged[MemberFunction]<ICanvasEffectFactoryNative*, Guid*, void**, int>)this.lpVtbl[0])((ICanvasEffectFactoryNative*)Unsafe.AsPointer(ref this), riid, ppvObject);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IUnknown.AddRef"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
[VtblIndex(1)]
|
||||
[return: NativeTypeName("ULONG")]
|
||||
public uint AddRef()
|
||||
{
|
||||
return ((delegate* unmanaged[MemberFunction]<ICanvasEffectFactoryNative*, uint>)this.lpVtbl[1])((ICanvasEffectFactoryNative*)Unsafe.AsPointer(ref this));
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IUnknown.Release"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
[VtblIndex(2)]
|
||||
[return: NativeTypeName("ULONG")]
|
||||
public uint Release()
|
||||
{
|
||||
return ((delegate* unmanaged[MemberFunction]<ICanvasEffectFactoryNative*, uint>)this.lpVtbl[2])((ICanvasEffectFactoryNative*)Unsafe.AsPointer(ref this));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new inspectable wrapper for an input D2D effect previosly registered through <see cref="ICanvasFactoryNative.RegisterEffectFactory"/>.
|
||||
/// </summary>
|
||||
/// <param name="device">The input canvas device.</param>
|
||||
/// <param name="resource">The input native effect to create a wrapper for.</param>
|
||||
/// <param name="dpi">The realization DPIs for <paramref name="resource"/>.</param>
|
||||
/// <param name="wrapper">The resulting wrapper for <paramref name="resource"/>.</param>
|
||||
/// <returns>The <see cref="HRESULT"/> for the operation.</returns>
|
||||
/// <remarks>
|
||||
/// All parameters are directly forwarded from the ones the caller passed to <see cref="ICanvasFactoryNative.GetOrCreate"/>. The returned
|
||||
/// wrapper should implement <see cref="global::Microsoft.Graphics.Canvas.ICanvasImage"/> to be returned correctly from Win2D after this call.
|
||||
/// </remarks>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
[VtblIndex(3)]
|
||||
public HRESULT CreateWrapper(ICanvasDevice* device, ID2D1Effect* resource, float dpi, IInspectable** wrapper)
|
||||
{
|
||||
return ((delegate* unmanaged[MemberFunction]<ICanvasEffectFactoryNative*, ICanvasDevice*, ID2D1Effect*, float, IInspectable**, int>)this.lpVtbl[3])(
|
||||
(ICanvasEffectFactoryNative*)Unsafe.AsPointer(ref this),
|
||||
device,
|
||||
resource,
|
||||
dpi,
|
||||
wrapper);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The ABI methods for <see cref="ICanvasEffectFactoryNative"/>.
|
||||
/// </summary>
|
||||
internal static unsafe class ICanvasEffectFactoryNativeMethods
|
||||
{
|
||||
/// <inheritdoc cref="ICanvasEffectFactoryNative.IID"/>
|
||||
public static Guid IID => *ICanvasEffectFactoryNative.IID;
|
||||
|
||||
/// <inheritdoc cref="global::Microsoft.Graphics.Canvas.ICanvasEffectFactoryNative.Vftbl.AbiToProjectionVftablePtr"/>
|
||||
public static IntPtr AbiToProjectionVftablePtr => global::Microsoft.Graphics.Canvas.ICanvasEffectFactoryNative.Vftbl.AbiToProjectionVftablePtr;
|
||||
}
|
||||
}
|
||||
|
||||
namespace Microsoft.Graphics.Canvas
|
||||
{
|
||||
using ABI.Microsoft.Graphics.Canvas;
|
||||
|
||||
/// <summary>
|
||||
/// The managed implementation of <see cref="ABI.Microsoft.Graphics.Canvas.ICanvasEffectFactoryNative"/>.
|
||||
/// </summary>
|
||||
[Guid("29BA1A1F-1CFE-44C3-984D-426D61B51427")]
|
||||
[WindowsRuntimeType]
|
||||
[WindowsRuntimeHelperType(typeof(ICanvasEffectFactoryNative))]
|
||||
internal unsafe interface ICanvasEffectFactoryNative
|
||||
{
|
||||
/// <inheritdoc cref="ABI.Microsoft.Graphics.Canvas.ICanvasEffectFactoryNative.CreateWrapper"/>
|
||||
[return: NativeTypeName("HRESULT")]
|
||||
int CreateWrapper(ICanvasDevice* device, ID2D1Effect* resource, float dpi, IInspectable** wrapper);
|
||||
|
||||
/// <summary>
|
||||
/// The vtable type for <see cref="ICanvasEffectFactoryNative"/>.
|
||||
/// </summary>
|
||||
public struct Vftbl
|
||||
{
|
||||
/// <summary>
|
||||
/// Allows CsWinRT to retrieve a pointer to the projection vtable (the name is hardcoded by convention).
|
||||
/// </summary>
|
||||
public static readonly IntPtr AbiToProjectionVftablePtr = InitVtbl();
|
||||
|
||||
/// <summary>
|
||||
/// Builds the custom method table pointer for <see cref="ICanvasEffectFactoryNative"/>.
|
||||
/// </summary>
|
||||
/// <returns>The method table pointer for <see cref="ICanvasEffectFactoryNative"/>.</returns>
|
||||
private static IntPtr InitVtbl()
|
||||
{
|
||||
Vftbl* lpVtbl = (Vftbl*)ComWrappersSupport.AllocateVtableMemory(typeof(Vftbl), sizeof(Vftbl));
|
||||
|
||||
lpVtbl->IUnknownVftbl = IUnknownVftbl.AbiToProjectionVftbl;
|
||||
lpVtbl->CreateWrapper = &CreateWrapperFromAbi;
|
||||
|
||||
return (IntPtr)lpVtbl;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The IUnknown vtable.
|
||||
/// </summary>
|
||||
private IUnknownVftbl IUnknownVftbl;
|
||||
|
||||
/// <summary>
|
||||
/// Function pointer for <see cref="CreateWrapperFromAbi"/>.
|
||||
/// </summary>
|
||||
private delegate* unmanaged[MemberFunction]<IntPtr, ICanvasDevice*, ID2D1Effect*, float, IInspectable**, int> CreateWrapper;
|
||||
|
||||
/// <inheritdoc cref="ICanvasEffectFactoryNative.CreateWrapper"/>
|
||||
[UnmanagedCallersOnly(CallConvs = [typeof(CallConvMemberFunction)])]
|
||||
[return: NativeTypeName("HRESULT")]
|
||||
private static int CreateWrapperFromAbi(IntPtr thisPtr, ICanvasDevice* device, ID2D1Effect* resource, float dpi, IInspectable** wrapper)
|
||||
{
|
||||
try
|
||||
{
|
||||
return ComWrappersSupport.FindObject<ICanvasEffectFactoryNative>(thisPtr).CreateWrapper(device, resource, dpi, wrapper);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
ExceptionHelpers.SetErrorInfo(e);
|
||||
|
||||
return Marshal.GetHRForException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,168 +0,0 @@
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using ComputeSharp.Win32;
|
||||
|
||||
#pragma warning disable CS0649, IDE0055, IDE1006
|
||||
|
||||
namespace ABI.Microsoft.Graphics.Canvas;
|
||||
|
||||
/// <summary>
|
||||
/// The activation factory for <see cref="global::Microsoft.Graphics.Canvas.CanvasDevice"/>.
|
||||
/// </summary>
|
||||
[NativeTypeName("class ICanvasFactoryNative : public IInspectable")]
|
||||
[NativeInheritance("IInspectable")]
|
||||
internal unsafe struct ICanvasFactoryNative : IComObject
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
public static Guid* IID
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get
|
||||
{
|
||||
ReadOnlySpan<byte> data =
|
||||
[
|
||||
0x0D, 0x44, 0x5C, 0x69,
|
||||
0xB3, 0x04,
|
||||
0xDD, 0x4E,
|
||||
0xBF, 0xD9,
|
||||
0x63,
|
||||
0xE5,
|
||||
0x1E,
|
||||
0x9F,
|
||||
0x72,
|
||||
0x02
|
||||
];
|
||||
|
||||
return (Guid*)Unsafe.AsPointer(ref MemoryMarshal.GetReference(data));
|
||||
}
|
||||
}
|
||||
|
||||
public void** lpVtbl;
|
||||
|
||||
/// <inheritdoc cref="IUnknown.QueryInterface"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
[VtblIndex(0)]
|
||||
public HRESULT QueryInterface([NativeTypeName("const IID &")] Guid* riid, void** ppvObject)
|
||||
{
|
||||
return ((delegate* unmanaged[MemberFunction]<ICanvasFactoryNative*, Guid*, void**, int>)this.lpVtbl[0])((ICanvasFactoryNative*)Unsafe.AsPointer(ref this), riid, ppvObject);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IUnknown.AddRef"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
[VtblIndex(1)]
|
||||
[return: NativeTypeName("ULONG")]
|
||||
public uint AddRef()
|
||||
{
|
||||
return ((delegate* unmanaged[MemberFunction]<ICanvasFactoryNative*, uint>)this.lpVtbl[1])((ICanvasFactoryNative*)Unsafe.AsPointer(ref this));
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IUnknown.Release"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
[VtblIndex(2)]
|
||||
[return: NativeTypeName("ULONG")]
|
||||
public uint Release()
|
||||
{
|
||||
return ((delegate* unmanaged[MemberFunction]<ICanvasFactoryNative*, uint>)this.lpVtbl[2])((ICanvasFactoryNative*)Unsafe.AsPointer(ref this));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or creates an <c>IInspectable</c> Win2D wrapper for a given native D2D resource.
|
||||
/// </summary>
|
||||
/// <param name="device">The input canvas device (as a marshalled <see cref="global::Microsoft.Graphics.Canvas.CanvasDevice"/>).</param>
|
||||
/// <param name="resource">The input native resource to create a wrapper for.</param>
|
||||
/// <param name="dpi">The realization DPIs for <paramref name="resource"/>.</param>
|
||||
/// <param name="wrapper">The resulting wrapper for <paramref name="resource"/>.</param>
|
||||
/// <returns>The <see cref="HRESULT"/> for the operation.</returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
[VtblIndex(6)]
|
||||
public HRESULT GetOrCreate(ICanvasDevice* device, IUnknown* resource, float dpi, IInspectable** wrapper)
|
||||
{
|
||||
return ((delegate* unmanaged[MemberFunction]<ICanvasFactoryNative*, ICanvasDevice*, IUnknown*, float, IInspectable**, int>)this.lpVtbl[6])(
|
||||
(ICanvasFactoryNative*)Unsafe.AsPointer(ref this),
|
||||
device,
|
||||
resource,
|
||||
dpi,
|
||||
wrapper);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Registers an <c>IInspectable</c> wrapper for a given native D2D resource.
|
||||
/// </summary>
|
||||
/// <param name="resource">The input native resource to register a wrapper for.</param>
|
||||
/// <param name="wrapper">The wrapper to register for <paramref name="resource"/>.</param>
|
||||
/// <returns>
|
||||
/// The <see cref="HRESULT"/> for the operation. In case of success, it will be <see cref="S.S_OK"/> if the wrapper
|
||||
/// was not previously registered, or <see cref="S.S_FALSE"/> if <paramref name="resource"/> was already registered.
|
||||
/// </returns>
|
||||
/// <remarks>
|
||||
/// The method validates at runtime that <paramref name="resource"/> is an <see cref="ID2D1Image"/>
|
||||
/// and that <paramref name="wrapper"/> is an <see cref="global::Microsoft.Graphics.Canvas.ICanvasImage"/>. Additionally,
|
||||
/// <paramref name="wrapper"/> has to implement <c>IWeakReferenceSource</c> for this method to be successful.
|
||||
/// </remarks>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
[VtblIndex(7)]
|
||||
public HRESULT RegisterWrapper(IUnknown* resource, IInspectable* wrapper)
|
||||
{
|
||||
return ((delegate* unmanaged[MemberFunction]<ICanvasFactoryNative*, IUnknown*, IInspectable*, int>)this.lpVtbl[7])(
|
||||
(ICanvasFactoryNative*)Unsafe.AsPointer(ref this),
|
||||
resource,
|
||||
wrapper);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Unregisters a native D2D resource previously registered with <see cref="RegisterWrapper"/>.
|
||||
/// </summary>
|
||||
/// <param name="resource">The input native resource to unregister.</param>
|
||||
/// <returns>
|
||||
/// The <see cref="HRESULT"/> for the operation. In case of success, it will be <see cref="S.S_OK"/> if the
|
||||
/// resource was previously registered and could be correctly unregistered, <see cref="S.S_FALSE"/> otherwise.
|
||||
/// </returns>
|
||||
/// <remarks>
|
||||
/// The method validates at runtime that <paramref name="resource"/> is an <see cref="ID2D1Image"/>.
|
||||
/// </remarks>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
[VtblIndex(8)]
|
||||
public HRESULT UnregisterWrapper(IUnknown* resource)
|
||||
{
|
||||
return ((delegate* unmanaged[MemberFunction]<ICanvasFactoryNative*, IUnknown*, int>)this.lpVtbl[8])(
|
||||
(ICanvasFactoryNative*)Unsafe.AsPointer(ref this),
|
||||
resource);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Registers a factory of wrappers for custom effects.
|
||||
/// </summary>
|
||||
/// <param name="effectId">The id of the effects to register a factory for.</param>
|
||||
/// <param name="factory">The input factory to create wrappers of native effects.</param>
|
||||
/// <returns>
|
||||
/// The <see cref="HRESULT"/> for the operation. In case of success, it will be <see cref="S.S_OK"/> if the factory
|
||||
/// was not previously registered, or <see cref="S.S_FALSE"/> if <paramref name="effectId"/> was already registered.
|
||||
/// </returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
[VtblIndex(9)]
|
||||
public HRESULT RegisterEffectFactory([NativeTypeName("const IID &")] Guid* effectId, ICanvasEffectFactoryNative* factory)
|
||||
{
|
||||
return ((delegate* unmanaged[MemberFunction]<ICanvasFactoryNative*, Guid*, ICanvasEffectFactoryNative*, int>)this.lpVtbl[9])(
|
||||
(ICanvasFactoryNative*)Unsafe.AsPointer(ref this),
|
||||
effectId,
|
||||
factory);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Unregisters an effect factory that was previosuly registered with <see cref="RegisterEffectFactory"/>.
|
||||
/// </summary>
|
||||
/// <param name="effectId">The id of the effects to unregister the factory for.</param>
|
||||
/// <returns>
|
||||
/// The <see cref="HRESULT"/> for the operation. In case of success, it will be <see cref="S.S_OK"/> if the
|
||||
/// factory was previously registered and could be correctly unregistered, <see cref="S.S_FALSE"/> otherwise.
|
||||
/// </returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
[VtblIndex(10)]
|
||||
public HRESULT UnregisterEffectFactory([NativeTypeName("const IID &")] Guid* effectId)
|
||||
{
|
||||
return ((delegate* unmanaged[MemberFunction]<ICanvasFactoryNative*, Guid*, int>)this.lpVtbl[10])(
|
||||
(ICanvasFactoryNative*)Unsafe.AsPointer(ref this),
|
||||
effectId);
|
||||
}
|
||||
}
|
||||
@@ -1,233 +0,0 @@
|
||||
using System;
|
||||
using System.ComponentModel;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using ComputeSharp.Win32;
|
||||
using WinRT;
|
||||
using WinRT.Interop;
|
||||
|
||||
#pragma warning disable CS0649, IDE0055, IDE1006
|
||||
|
||||
namespace ABI.Microsoft.Graphics.Canvas
|
||||
{
|
||||
/// <summary>
|
||||
/// An interop Win2D interface for all images and effects that can be drawn.
|
||||
/// </summary>
|
||||
[NativeTypeName("class ICanvasImageInterop : IUnknown")]
|
||||
[NativeInheritance("IUnknown")]
|
||||
internal unsafe struct ICanvasImageInterop : IComObject
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
public static Guid* IID
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get
|
||||
{
|
||||
ReadOnlySpan<byte> data =
|
||||
[
|
||||
0xF7, 0xD1, 0x42, 0xE0,
|
||||
0xAD, 0xF9,
|
||||
0x79, 0x44,
|
||||
0xA7, 0x13,
|
||||
0x67,
|
||||
0x62,
|
||||
0x7E,
|
||||
0xA3,
|
||||
0x18,
|
||||
0x63
|
||||
];
|
||||
|
||||
return (Guid*)Unsafe.AsPointer(ref MemoryMarshal.GetReference(data));
|
||||
}
|
||||
}
|
||||
|
||||
public void** lpVtbl;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the device that the effect is currently realized on, if any.
|
||||
/// </summary>
|
||||
/// <param name="device">The resulting device, if available (as a marshalled <see cref="global::Microsoft.Graphics.Canvas.CanvasDevice"/>).</param>
|
||||
/// <param name="type">The <see cref="WIN2D_GET_DEVICE_ASSOCIATION_TYPE"/> value describing the returned instance.</param>
|
||||
/// <returns>The <see cref="HRESULT"/> for the operation.</returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
[VtblIndex(3)]
|
||||
public HRESULT GetDevice(ICanvasDevice** device, WIN2D_GET_DEVICE_ASSOCIATION_TYPE* type)
|
||||
{
|
||||
return ((delegate* unmanaged[MemberFunction]<ICanvasImageInterop*, ICanvasDevice**, WIN2D_GET_DEVICE_ASSOCIATION_TYPE*, int>)this.lpVtbl[3])((ICanvasImageInterop*)Unsafe.AsPointer(ref this), device, type);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets an <see cref="ID2D1Image"/> from an <see cref="ICanvasImageInterop"/> instance.
|
||||
/// </summary>
|
||||
/// <param name="device">The input canvas device (as a marshalled <see cref="global::Microsoft.Graphics.Canvas.CanvasDevice"/>).</param>
|
||||
/// <param name="deviceContext">
|
||||
/// The device context in use. This value is is optional (but recommended), except when the
|
||||
/// <see cref="WIN2D_GET_D2D_IMAGE_FLAGS.WIN2D_GET_D2D_IMAGE_FLAGS_READ_DPI_FROM_DEVICE_CONTEXT"/> flag is specified. This is because
|
||||
/// not all callers of <see cref="GetD2DImage"/> have easy access to a context. It is always
|
||||
/// possible to get a resource creation context from the device, but the context is only
|
||||
/// actually necessary if a new effect realization needs to be created, so it is more efficient
|
||||
/// to have the implementation do this lookup only if/when it turns out to be needed.
|
||||
/// </param>
|
||||
/// <param name="flags">The flags to use to get the image.</param>
|
||||
/// <param name="targetDpi">
|
||||
/// The DPI of the target device context. This is used to determine when a <c>D2D1DpiCompensation</c>
|
||||
/// effect needs to be inserted. Behavior of this parameter can be overridden by the flag values
|
||||
/// <see cref="WIN2D_GET_D2D_IMAGE_FLAGS.WIN2D_GET_D2D_IMAGE_FLAGS_READ_DPI_FROM_DEVICE_CONTEXT"/>,
|
||||
/// <see cref="WIN2D_GET_D2D_IMAGE_FLAGS.WIN2D_GET_D2D_IMAGE_FLAGS_ALWAYS_INSERT_DPI_COMPENSATION"/>
|
||||
/// or <see cref="WIN2D_GET_D2D_IMAGE_FLAGS.WIN2D_GET_D2D_IMAGE_FLAGS_NEVER_INSERT_DPI_COMPENSATION"/>
|
||||
/// </param>
|
||||
/// <param name="realizeDpi">
|
||||
/// The DPI of a source bitmap, or zero if the image does not have a fixed DPI. A <c>D2D1DpiCompensation</c> effect
|
||||
/// will be inserted if <paramref name="targetDpi"/> and <paramref name="realizeDpi"/> are different (flags permitting).
|
||||
/// </param>
|
||||
/// <param name="ppImage">The resulting <see cref="ID2D1Image"/> for the effect.</param>
|
||||
/// <returns>The <see cref="HRESULT"/> for the operation.</returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
[VtblIndex(4)]
|
||||
public HRESULT GetD2DImage(
|
||||
ICanvasDevice* device,
|
||||
ID2D1DeviceContext* deviceContext,
|
||||
WIN2D_GET_D2D_IMAGE_FLAGS flags,
|
||||
float targetDpi,
|
||||
float* realizeDpi,
|
||||
ID2D1Image** ppImage)
|
||||
{
|
||||
return ((delegate* unmanaged[MemberFunction]<ICanvasImageInterop*, ICanvasDevice*, ID2D1DeviceContext*, WIN2D_GET_D2D_IMAGE_FLAGS, float, float*, ID2D1Image**, int>)this.lpVtbl[4])(
|
||||
(ICanvasImageInterop*)Unsafe.AsPointer(ref this),
|
||||
device,
|
||||
deviceContext,
|
||||
flags,
|
||||
targetDpi,
|
||||
realizeDpi,
|
||||
ppImage);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The ABI methods for <see cref="ICanvasImageInterop"/>.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This type has public accessibility to allow the AOT generator in CsWinRT to reference
|
||||
/// the <see cref="IID"/> and <see cref="AbiToProjectionVftablePtr"/> properties to make
|
||||
/// marshalling of CCW types AOT-safe. It is not meant to be used directly by developers.
|
||||
/// </remarks>
|
||||
[EditorBrowsable(EditorBrowsableState.Never)]
|
||||
public static unsafe class ICanvasImageInteropMethods
|
||||
{
|
||||
/// <inheritdoc cref="ICanvasImageInterop.IID"/>
|
||||
public static Guid IID => *ICanvasImageInterop.IID;
|
||||
|
||||
/// <inheritdoc cref="global::Microsoft.Graphics.Canvas.ICanvasImageInterop.Vftbl.AbiToProjectionVftablePtr"/>
|
||||
public static IntPtr AbiToProjectionVftablePtr => global::Microsoft.Graphics.Canvas.ICanvasImageInterop.Vftbl.AbiToProjectionVftablePtr;
|
||||
}
|
||||
}
|
||||
|
||||
namespace Microsoft.Graphics.Canvas
|
||||
{
|
||||
using ABI.Microsoft.Graphics.Canvas;
|
||||
|
||||
/// <summary>
|
||||
/// The managed implementation of <see cref="ABI.Microsoft.Graphics.Canvas.ICanvasImageInterop"/>.
|
||||
/// </summary>
|
||||
[Guid("E042D1F7-F9AD-4479-A713-67627EA31863")]
|
||||
[WindowsRuntimeType]
|
||||
[WindowsRuntimeHelperType(typeof(ICanvasImageInterop))]
|
||||
internal unsafe interface ICanvasImageInterop
|
||||
{
|
||||
/// <inheritdoc cref="ABI.Microsoft.Graphics.Canvas.ICanvasImageInterop.GetDevice"/>
|
||||
[return: NativeTypeName("HRESULT")]
|
||||
int GetDevice(ICanvasDevice** device, WIN2D_GET_DEVICE_ASSOCIATION_TYPE* type);
|
||||
|
||||
/// <inheritdoc cref="ABI.Microsoft.Graphics.Canvas.ICanvasImageInterop.GetD2DImage"/>
|
||||
[return: NativeTypeName("HRESULT")]
|
||||
int GetD2DImage(
|
||||
ICanvasDevice* device,
|
||||
ID2D1DeviceContext* deviceContext,
|
||||
WIN2D_GET_D2D_IMAGE_FLAGS flags,
|
||||
float targetDpi,
|
||||
float* realizeDpi,
|
||||
ID2D1Image** ppImage);
|
||||
|
||||
/// <summary>
|
||||
/// The vtable type for <see cref="ICanvasImageInterop"/>.
|
||||
/// </summary>
|
||||
public struct Vftbl
|
||||
{
|
||||
/// <summary>
|
||||
/// Allows CsWinRT to retrieve a pointer to the projection vtable (the name is hardcoded by convention).
|
||||
/// </summary>
|
||||
public static readonly IntPtr AbiToProjectionVftablePtr = InitVtbl();
|
||||
|
||||
/// <summary>
|
||||
/// Builds the custom method table pointer for <see cref="ICanvasImageInterop"/>.
|
||||
/// </summary>
|
||||
/// <returns>The method table pointer for <see cref="ICanvasImageInterop"/>.</returns>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The IUnknown vtable.
|
||||
/// </summary>
|
||||
private IUnknownVftbl IUnknownVftbl;
|
||||
|
||||
/// <summary>
|
||||
/// Function pointer for <see cref="GetDeviceFromAbi"/>.
|
||||
/// </summary>
|
||||
private delegate* unmanaged[MemberFunction]<IntPtr, ICanvasDevice**, WIN2D_GET_DEVICE_ASSOCIATION_TYPE*, int> GetDevice;
|
||||
|
||||
/// <summary>
|
||||
/// Function pointer for <see cref="GetD2DImageFromAbi"/>.
|
||||
/// </summary>
|
||||
private delegate* unmanaged[MemberFunction]<IntPtr, ICanvasDevice*, ID2D1DeviceContext*, WIN2D_GET_D2D_IMAGE_FLAGS, float, float*, ID2D1Image**, int> GetD2DImage;
|
||||
|
||||
/// <inheritdoc cref="ICanvasImageInterop.GetDevice"/>
|
||||
[UnmanagedCallersOnly(CallConvs = [typeof(CallConvMemberFunction)])]
|
||||
[return: NativeTypeName("HRESULT")]
|
||||
private static int GetDeviceFromAbi(IntPtr thisPtr, ICanvasDevice** device, WIN2D_GET_DEVICE_ASSOCIATION_TYPE* type)
|
||||
{
|
||||
try
|
||||
{
|
||||
return ComWrappersSupport.FindObject<ICanvasImageInterop>(thisPtr).GetDevice(device, type);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
ExceptionHelpers.SetErrorInfo(e);
|
||||
|
||||
return Marshal.GetHRForException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="ICanvasImageInterop.GetDevice"/>
|
||||
[UnmanagedCallersOnly(CallConvs = [typeof(CallConvMemberFunction)])]
|
||||
[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<ICanvasImageInterop>(thisPtr).GetD2DImage(device, deviceContext, flags, targetDpi, realizeDpi, ppImage);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
ExceptionHelpers.SetErrorInfo(e);
|
||||
|
||||
return Marshal.GetHRForException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,69 +0,0 @@
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using ComputeSharp.Win32;
|
||||
|
||||
#pragma warning disable CS0649, IDE0055, IDE1006
|
||||
|
||||
namespace ABI.Microsoft.Graphics.Canvas;
|
||||
|
||||
/// <summary>
|
||||
/// The native WinRT interface for <see cref="global::Microsoft.Graphics.Canvas.ICanvasResourceCreator"/> objects.
|
||||
/// </summary>
|
||||
[NativeTypeName("class ICanvasResourceCreator : IInspectable")]
|
||||
[NativeInheritance("IInspectable")]
|
||||
internal unsafe struct ICanvasResourceCreator : IComObject
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
static Guid* IComObject.IID
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get
|
||||
{
|
||||
ReadOnlySpan<byte> data =
|
||||
[
|
||||
0xA8, 0x8A, 0x6D, 0x8F,
|
||||
0x2F, 0x49,
|
||||
0xC6, 0x4B,
|
||||
0xB3,
|
||||
0xD0,
|
||||
0xE7,
|
||||
0xF5,
|
||||
0xEA,
|
||||
0xE8,
|
||||
0x4B,
|
||||
0x11
|
||||
];
|
||||
|
||||
return (Guid*)Unsafe.AsPointer(ref MemoryMarshal.GetReference(data));
|
||||
}
|
||||
}
|
||||
|
||||
public void** lpVtbl;
|
||||
|
||||
/// <inheritdoc cref="IUnknown.QueryInterface"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
[VtblIndex(0)]
|
||||
public HRESULT QueryInterface([NativeTypeName("const IID &")] Guid* riid, void** ppvObject)
|
||||
{
|
||||
return ((delegate* unmanaged[MemberFunction]<ICanvasResourceCreator*, Guid*, void**, int>)this.lpVtbl[0])((ICanvasResourceCreator*)Unsafe.AsPointer(ref this), riid, ppvObject);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IUnknown.AddRef"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
[VtblIndex(1)]
|
||||
[return: NativeTypeName("ULONG")]
|
||||
public uint AddRef()
|
||||
{
|
||||
return ((delegate* unmanaged[MemberFunction]<ICanvasResourceCreator*, uint>)this.lpVtbl[1])((ICanvasResourceCreator*)Unsafe.AsPointer(ref this));
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IUnknown.Release"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
[VtblIndex(2)]
|
||||
[return: NativeTypeName("ULONG")]
|
||||
public uint Release()
|
||||
{
|
||||
return ((delegate* unmanaged[MemberFunction]<ICanvasResourceCreator*, uint>)this.lpVtbl[2])((ICanvasResourceCreator*)Unsafe.AsPointer(ref this));
|
||||
}
|
||||
}
|
||||
-69
@@ -1,69 +0,0 @@
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using ComputeSharp.Win32;
|
||||
|
||||
#pragma warning disable CS0649, IDE0055, IDE1006
|
||||
|
||||
namespace ABI.Microsoft.Graphics.Canvas;
|
||||
|
||||
/// <summary>
|
||||
/// The native WinRT interface for <see cref="global::Microsoft.Graphics.Canvas.ICanvasResourceCreatorWithDpi"/> objects.
|
||||
/// </summary>
|
||||
[NativeTypeName("class ICanvasResourceCreatorWithDpi : IInspectable")]
|
||||
[NativeInheritance("IInspectable")]
|
||||
internal unsafe struct ICanvasResourceCreatorWithDpi : IComObject
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
static Guid* IComObject.IID
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get
|
||||
{
|
||||
ReadOnlySpan<byte> data =
|
||||
[
|
||||
0x12, 0xB5, 0x75, 0x1A,
|
||||
0xFA, 0xE9,
|
||||
0xE6, 0x49,
|
||||
0xA8,
|
||||
0x76,
|
||||
0x38,
|
||||
0xCA,
|
||||
0xE1,
|
||||
0x94,
|
||||
0x01,
|
||||
0x3E
|
||||
];
|
||||
|
||||
return (Guid*)Unsafe.AsPointer(ref MemoryMarshal.GetReference(data));
|
||||
}
|
||||
}
|
||||
|
||||
public void** lpVtbl;
|
||||
|
||||
/// <inheritdoc cref="IUnknown.QueryInterface"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
[VtblIndex(0)]
|
||||
public HRESULT QueryInterface([NativeTypeName("const IID &")] Guid* riid, void** ppvObject)
|
||||
{
|
||||
return ((delegate* unmanaged[MemberFunction]<ICanvasResourceCreatorWithDpi*, Guid*, void**, int>)this.lpVtbl[0])((ICanvasResourceCreatorWithDpi*)Unsafe.AsPointer(ref this), riid, ppvObject);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IUnknown.AddRef"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
[VtblIndex(1)]
|
||||
[return: NativeTypeName("ULONG")]
|
||||
public uint AddRef()
|
||||
{
|
||||
return ((delegate* unmanaged[MemberFunction]<ICanvasResourceCreatorWithDpi*, uint>)this.lpVtbl[1])((ICanvasResourceCreatorWithDpi*)Unsafe.AsPointer(ref this));
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IUnknown.Release"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
[VtblIndex(2)]
|
||||
[return: NativeTypeName("ULONG")]
|
||||
public uint Release()
|
||||
{
|
||||
return ((delegate* unmanaged[MemberFunction]<ICanvasResourceCreatorWithDpi*, uint>)this.lpVtbl[2])((ICanvasResourceCreatorWithDpi*)Unsafe.AsPointer(ref this));
|
||||
}
|
||||
}
|
||||
-79
@@ -1,79 +0,0 @@
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using ComputeSharp.Win32;
|
||||
|
||||
#pragma warning disable CS0649, IDE0055, IDE1006
|
||||
|
||||
namespace ABI.Microsoft.Graphics.Canvas;
|
||||
|
||||
/// <summary>
|
||||
/// An interop wrapper type for Win2D objects (see <see href="https://microsoft.github.io/Win2D/WinUI3/html/Interop.htm"/>).
|
||||
/// </summary>
|
||||
[NativeTypeName("class ICanvasResourceWrapperNative : IUnknown")]
|
||||
[NativeInheritance("IUnknown")]
|
||||
internal unsafe struct ICanvasResourceWrapperNative : IComObject
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
static Guid* IComObject.IID
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get
|
||||
{
|
||||
ReadOnlySpan<byte> data =
|
||||
[
|
||||
0x8D, 0x68, 0x10, 0x5F,
|
||||
0x55, 0xEA,
|
||||
0x55, 0x4D,
|
||||
0xA3,
|
||||
0xB0,
|
||||
0x4D,
|
||||
0xDB,
|
||||
0x55,
|
||||
0xC0,
|
||||
0xC2,
|
||||
0x0A
|
||||
];
|
||||
|
||||
return (Guid*)Unsafe.AsPointer(ref MemoryMarshal.GetReference(data));
|
||||
}
|
||||
}
|
||||
|
||||
public void** lpVtbl;
|
||||
|
||||
/// <inheritdoc cref="IUnknown.QueryInterface"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
[VtblIndex(0)]
|
||||
public HRESULT QueryInterface([NativeTypeName("const IID &")] Guid* riid, void** ppvObject)
|
||||
{
|
||||
return ((delegate* unmanaged[MemberFunction]<ICanvasResourceWrapperNative*, Guid*, void**, int>)this.lpVtbl[0])((ICanvasResourceWrapperNative*)Unsafe.AsPointer(ref this), riid, ppvObject);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IUnknown.AddRef"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
[VtblIndex(1)]
|
||||
[return: NativeTypeName("ULONG")]
|
||||
public uint AddRef()
|
||||
{
|
||||
return ((delegate* unmanaged[MemberFunction]<ICanvasResourceWrapperNative*, uint>)this.lpVtbl[1])((ICanvasResourceWrapperNative*)Unsafe.AsPointer(ref this));
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IUnknown.Release"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
[VtblIndex(2)]
|
||||
[return: NativeTypeName("ULONG")]
|
||||
public uint Release()
|
||||
{
|
||||
return ((delegate* unmanaged[MemberFunction]<ICanvasResourceWrapperNative*, uint>)this.lpVtbl[2])((ICanvasResourceWrapperNative*)Unsafe.AsPointer(ref this));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Interface provided by various Win2D objects that is able to retrieve the wrapped resource.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
[VtblIndex(3)]
|
||||
public HRESULT GetNativeResource(ICanvasDevice* device, float dpi, [NativeTypeName("REFIID")] Guid* iid, void** resource)
|
||||
{
|
||||
return ((delegate* unmanaged[MemberFunction]<ICanvasResourceWrapperNative*, ICanvasDevice*, float, Guid*, void**, int>)this.lpVtbl[3])((ICanvasResourceWrapperNative*)Unsafe.AsPointer(ref this), device, dpi, iid, resource);
|
||||
}
|
||||
}
|
||||
@@ -1,56 +0,0 @@
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
using ComputeSharp.Win32;
|
||||
|
||||
#pragma warning disable CS0649, IDE1006
|
||||
|
||||
namespace ABI.Microsoft.Graphics.Canvas;
|
||||
|
||||
/// <summary>
|
||||
/// An interface for a COM object that provides access to a pooled <see cref="ID2D1DeviceContext"/> object.
|
||||
/// </summary>
|
||||
[NativeTypeName("class ID2D1DeviceContextLease : IUnknown")]
|
||||
[NativeInheritance("IUnknown")]
|
||||
internal unsafe struct ID2D1DeviceContextLease
|
||||
{
|
||||
public void** lpVtbl;
|
||||
|
||||
/// <inheritdoc cref="IUnknown.QueryInterface"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
[VtblIndex(0)]
|
||||
public HRESULT QueryInterface([NativeTypeName("const IID &")] Guid* riid, void** ppvObject)
|
||||
{
|
||||
return ((delegate* unmanaged[MemberFunction]<ID2D1DeviceContextLease*, Guid*, void**, int>)this.lpVtbl[0])((ID2D1DeviceContextLease*)Unsafe.AsPointer(ref this), riid, ppvObject);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IUnknown.AddRef"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
[VtblIndex(1)]
|
||||
[return: NativeTypeName("ULONG")]
|
||||
public uint AddRef()
|
||||
{
|
||||
return ((delegate* unmanaged[MemberFunction]<ID2D1DeviceContextLease*, uint>)this.lpVtbl[1])((ID2D1DeviceContextLease*)Unsafe.AsPointer(ref this));
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IUnknown.Release"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
[VtblIndex(2)]
|
||||
[return: NativeTypeName("ULONG")]
|
||||
public uint Release()
|
||||
{
|
||||
return ((delegate* unmanaged[MemberFunction]<ID2D1DeviceContextLease*, uint>)this.lpVtbl[2])((ID2D1DeviceContextLease*)Unsafe.AsPointer(ref this));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Provides access to the <see cref="ID2D1DeviceContext"/> object wrapped by the current instance.
|
||||
/// </summary>
|
||||
/// <param name="deviceContext">The resulting <see cref="ID2D1DeviceContext"/> object wrapped by the current instance.</param>
|
||||
/// <returns>The <see cref="HRESULT"/> for the operation.</returns>
|
||||
/// <remarks>The returned <see cref="ID2D1DeviceContext"/> cannot be used once the owning lease has been released.</remarks>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
[VtblIndex(3)]
|
||||
public HRESULT GetD2DDeviceContext(ID2D1DeviceContext** deviceContext)
|
||||
{
|
||||
return ((delegate* unmanaged[MemberFunction]<ID2D1DeviceContextLease*, ID2D1DeviceContext**, int>)this.lpVtbl[3])((ID2D1DeviceContextLease*)Unsafe.AsPointer(ref this), deviceContext);
|
||||
}
|
||||
}
|
||||
@@ -1,81 +0,0 @@
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using ComputeSharp.Win32;
|
||||
|
||||
#pragma warning disable CS0649, IDE0055, IDE1006
|
||||
|
||||
namespace ABI.Microsoft.Graphics.Canvas;
|
||||
|
||||
/// <summary>
|
||||
/// An interface for an object that provides access to pooled <see cref="ID2D1DeviceContext"/> objects.
|
||||
/// </summary>
|
||||
[NativeTypeName("class ID2D1DeviceContextPool : IUnknown")]
|
||||
[NativeInheritance("IUnknown")]
|
||||
internal unsafe struct ID2D1DeviceContextPool : IComObject
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
static Guid* IComObject.IID
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get
|
||||
{
|
||||
ReadOnlySpan<byte> data =
|
||||
[
|
||||
0xA1, 0x82, 0x4A, 0x45,
|
||||
0x24, 0xF0,
|
||||
0xDB, 0x40,
|
||||
0xBD,
|
||||
0x5B,
|
||||
0x8F,
|
||||
0x52,
|
||||
0x7F,
|
||||
0xD5,
|
||||
0x8A,
|
||||
0xD0
|
||||
];
|
||||
|
||||
return (Guid*)Unsafe.AsPointer(ref MemoryMarshal.GetReference(data));
|
||||
}
|
||||
}
|
||||
|
||||
public void** lpVtbl;
|
||||
|
||||
/// <inheritdoc cref="IUnknown.QueryInterface"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
[VtblIndex(0)]
|
||||
public HRESULT QueryInterface([NativeTypeName("const IID &")] Guid* riid, void** ppvObject)
|
||||
{
|
||||
return ((delegate* unmanaged[MemberFunction]<ID2D1DeviceContextPool*, Guid*, void**, int>)this.lpVtbl[0])((ID2D1DeviceContextPool*)Unsafe.AsPointer(ref this), riid, ppvObject);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IUnknown.AddRef"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
[VtblIndex(1)]
|
||||
[return: NativeTypeName("ULONG")]
|
||||
public uint AddRef()
|
||||
{
|
||||
return ((delegate* unmanaged[MemberFunction]<ID2D1DeviceContextPool*, uint>)this.lpVtbl[1])((ID2D1DeviceContextPool*)Unsafe.AsPointer(ref this));
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IUnknown.Release"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
[VtblIndex(2)]
|
||||
[return: NativeTypeName("ULONG")]
|
||||
public uint Release()
|
||||
{
|
||||
return ((delegate* unmanaged[MemberFunction]<ID2D1DeviceContextPool*, uint>)this.lpVtbl[2])((ID2D1DeviceContextPool*)Unsafe.AsPointer(ref this));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieves a new <see cref="ID2D1DeviceContextLease"/> object from the current instance.
|
||||
/// </summary>
|
||||
/// <param name="lease">The resulting <see cref="ID2D1DeviceContextLease"/> object to use.</param>
|
||||
/// <returns>The <see cref="HRESULT"/> for the operation.</returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
[VtblIndex(3)]
|
||||
public HRESULT GetDeviceContextLease(ID2D1DeviceContextLease** lease)
|
||||
{
|
||||
return ((delegate* unmanaged[MemberFunction]<ID2D1DeviceContextPool*, ID2D1DeviceContextLease**, int>)this.lpVtbl[3])((ID2D1DeviceContextPool*)Unsafe.AsPointer(ref this), lease);
|
||||
}
|
||||
}
|
||||
@@ -1,47 +0,0 @@
|
||||
// Copyright © Tanner Gooding and Contributors. Licensed under the MIT License (MIT). See License.md in the repository root for more information.
|
||||
|
||||
// Ported from winrt/inspectable.h in the Windows SDK for Windows 10.0.22621.0
|
||||
// Original source is Copyright © Microsoft. All rights reserved.
|
||||
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
#pragma warning disable CS0649, IDE1006
|
||||
|
||||
namespace ComputeSharp.Win32;
|
||||
|
||||
/// <summary>
|
||||
/// The native WinRT interface for <see href="https://learn.microsoft.com/windows/win32/api/inspectable/nn-inspectable-iinspectable"><c>IInspectable</c></see> objects.
|
||||
/// </summary>
|
||||
[NativeTypeName("struct IInspectable : IUnknown")]
|
||||
[NativeInheritance("IUnknown")]
|
||||
internal unsafe struct IInspectable
|
||||
{
|
||||
public void** lpVtbl;
|
||||
|
||||
/// <inheritdoc cref="IUnknown.QueryInterface" />
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
[VtblIndex(0)]
|
||||
public HRESULT QueryInterface([NativeTypeName("const IID &")] Guid* riid, void** ppvObject)
|
||||
{
|
||||
return ((delegate* unmanaged[MemberFunction]<IInspectable*, Guid*, void**, int>)this.lpVtbl[0])((IInspectable*)Unsafe.AsPointer(ref this), riid, ppvObject);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IUnknown.AddRef" />
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
[VtblIndex(1)]
|
||||
[return: NativeTypeName("ULONG")]
|
||||
public uint AddRef()
|
||||
{
|
||||
return ((delegate* unmanaged[MemberFunction]<IInspectable*, uint>)this.lpVtbl[1])((IInspectable*)Unsafe.AsPointer(ref this));
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IUnknown.Release" />
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
[VtblIndex(2)]
|
||||
[return: NativeTypeName("ULONG")]
|
||||
public uint Release()
|
||||
{
|
||||
return ((delegate* unmanaged[MemberFunction]<IInspectable*, uint>)this.lpVtbl[2])((IInspectable*)Unsafe.AsPointer(ref this));
|
||||
}
|
||||
}
|
||||
@@ -1,50 +0,0 @@
|
||||
using System;
|
||||
|
||||
namespace ABI.Microsoft.Graphics.Canvas;
|
||||
|
||||
/// <summary>
|
||||
/// Options for fine-tuning the behavior of <see cref="ICanvasImageInterop.GetD2DImage"/>.
|
||||
/// </summary>
|
||||
[Flags]
|
||||
internal enum WIN2D_GET_D2D_IMAGE_FLAGS : uint
|
||||
{
|
||||
/// <summary>
|
||||
/// No options specified.
|
||||
/// </summary>
|
||||
WIN2D_GET_D2D_IMAGE_FLAGS_NONE = 0,
|
||||
|
||||
/// <summary>
|
||||
/// Ignore the <c>targetDpi</c> parameter, read DPI from <c>deviceContext</c> instead.
|
||||
/// </summary>
|
||||
WIN2D_GET_D2D_IMAGE_FLAGS_READ_DPI_FROM_DEVICE_CONTEXT = 1,
|
||||
|
||||
/// <summary>
|
||||
/// Ignore the <c>targetDpi</c> parameter, always insert DPI compensation.
|
||||
/// </summary>
|
||||
WIN2D_GET_D2D_IMAGE_FLAGS_ALWAYS_INSERT_DPI_COMPENSATION = 2,
|
||||
|
||||
/// <summary>
|
||||
/// Ignore the <c>targetDpi</c> parameter, never insert DPI compensation.
|
||||
/// </summary>
|
||||
WIN2D_GET_D2D_IMAGE_FLAGS_NEVER_INSERT_DPI_COMPENSATION = 4,
|
||||
|
||||
/// <summary>
|
||||
/// Do the bare minimum to get back an <c>ID2D1Image</c>, no validation or recursive realization.
|
||||
/// </summary>
|
||||
WIN2D_GET_D2D_IMAGE_FLAGS_MINIMAL_REALIZATION = 8,
|
||||
|
||||
/// <summary>
|
||||
/// Allow partially configured effect graphs where some inputs are <see langword="null"/>.
|
||||
/// </summary>
|
||||
WIN2D_GET_D2D_IMAGE_FLAGS_ALLOW_NULL_EFFECT_INPUTS = 16,
|
||||
|
||||
/// <summary>
|
||||
/// If an input is invalid, unrealize the effect and set the output image to <see langword="null"/>.
|
||||
/// </summary>
|
||||
WIN2D_GET_D2D_IMAGE_FLAGS_UNREALIZE_ON_FAILURE = 32,
|
||||
|
||||
/// <summary>
|
||||
/// Ignored.
|
||||
/// </summary>
|
||||
WIN2D_GET_D2D_IMAGE_FLAGS_FORCE_DWORD = 0xFFFFFFFF
|
||||
}
|
||||
-30
@@ -1,30 +0,0 @@
|
||||
using System;
|
||||
|
||||
namespace ABI.Microsoft.Graphics.Canvas;
|
||||
|
||||
/// <summary>
|
||||
/// Options for fine-tuning the behavior of <see cref="ICanvasImageInterop.GetDevice"/>.
|
||||
/// </summary>
|
||||
[Flags]
|
||||
internal enum WIN2D_GET_DEVICE_ASSOCIATION_TYPE : uint
|
||||
{
|
||||
/// <summary>
|
||||
/// No device info is available, callers will handle the returned device with their own logic.
|
||||
/// </summary>
|
||||
WIN2D_GET_DEVICE_ASSOCIATION_TYPE_UNSPECIFIED = 0,
|
||||
|
||||
/// <summary>
|
||||
/// The returned device is the one the image is currently realized on, if any.
|
||||
/// </summary>
|
||||
WIN2D_GET_DEVICE_ASSOCIATION_TYPE_REALIZATION_DEVICE = 1,
|
||||
|
||||
/// <summary>
|
||||
/// The returned device is the one that created the image resource, and cannot change.
|
||||
/// </summary>
|
||||
WIN2D_GET_DEVICE_ASSOCIATION_TYPE_CREATION_DEVICE = 2,
|
||||
|
||||
/// <summary>
|
||||
/// Ignored.
|
||||
/// </summary>
|
||||
WIN2D_GET_DEVICE_ASSOCIATION_TYPE_FORCE_DWORD = 0xFFFFFFFF
|
||||
}
|
||||
@@ -1,92 +0,0 @@
|
||||
using System.Numerics;
|
||||
using System.Runtime.InteropServices;
|
||||
using ABI.Microsoft.Graphics.Canvas.Effects;
|
||||
using ComputeSharp.Win32;
|
||||
using Windows.Foundation;
|
||||
|
||||
namespace ABI.Microsoft.Graphics.Canvas;
|
||||
|
||||
/// <summary>
|
||||
/// Native APIs from <c>Microsoft.Graphics.Canvas</c> (ie. Win2D).
|
||||
/// </summary>
|
||||
internal static unsafe class Win2D
|
||||
{
|
||||
/// <summary>
|
||||
/// Calculates the bounds for a given <see cref="ICanvasImageInterop"/> instance.
|
||||
/// </summary>
|
||||
/// <param name="resourceCreator">The <see cref="ICanvasResourceCreator"/> object in use.</param>
|
||||
/// <param name="image">The input <see cref="ICanvasImageInterop"/> instance to compute the bounds for.</param>
|
||||
/// <param name="transform">The transform to use (optional).</param>
|
||||
/// <param name="rect">The resulting bounds.</param>
|
||||
/// <returns>The <see cref="HRESULT"/> for the operation.</returns>
|
||||
[DllImport("Microsoft.Graphics.Canvas.dll", PreserveSig = true, ExactSpelling = true)]
|
||||
[return: NativeTypeName("HRESULT")]
|
||||
public static extern int GetBoundsForICanvasImageInterop(
|
||||
ICanvasResourceCreator* resourceCreator,
|
||||
ICanvasImageInterop* image,
|
||||
[NativeTypeName("Windows::Foundation::Numerics::Matrix3x2 const*")] Matrix3x2* transform,
|
||||
Rect* rect);
|
||||
|
||||
/// <summary>
|
||||
/// Implements support for <see cref="global::Microsoft.Graphics.Canvas.Effects.ICanvasEffect.InvalidateSourceRectangle"/> for an external <see cref="ICanvasImageInterop"/> instance.
|
||||
/// </summary>
|
||||
/// <param name="resourceCreator">The input <see cref="ICanvasResourceCreatorWithDpi"/> instance to use to perform the operation.</param>
|
||||
/// <param name="image">The target <see cref="ICanvasImageInterop"/> instance to execute the operation upon.</param>
|
||||
/// <param name="sourceIndex">The input index to invalidate.</param>
|
||||
/// <param name="invalidRectangle">The rectangle to invalidate.</param>
|
||||
/// <returns>The <see cref="HRESULT"/> for the operation.</returns>
|
||||
[DllImport("Microsoft.Graphics.Canvas.dll", PreserveSig = true, ExactSpelling = true)]
|
||||
[return: NativeTypeName("HRESULT")]
|
||||
public static extern int InvalidateSourceRectangleForICanvasImageInterop(
|
||||
ICanvasResourceCreatorWithDpi* resourceCreator,
|
||||
ICanvasImageInterop* image,
|
||||
uint sourceIndex,
|
||||
[NativeTypeName("Windows::Foundation::Rect const*")] Rect* invalidRectangle);
|
||||
|
||||
/// <summary>
|
||||
/// Implements support for <see cref="global::Microsoft.Graphics.Canvas.Effects.ICanvasEffect.GetInvalidRectangles"/> for an external <see cref="ICanvasImageInterop"/> instance.
|
||||
/// </summary>
|
||||
/// <param name="resourceCreator">The input <see cref="ICanvasResourceCreatorWithDpi"/> instance to use to perform the operation.</param>
|
||||
/// <param name="image">The target <see cref="ICanvasImageInterop"/> instance to execute the operation upon.</param>
|
||||
/// <param name="valueCount">The resulting length of the COM array returned via <paramref name="valueElements"/>.</param>
|
||||
/// <param name="valueElements">The COM array with the sequence of invalid rectangles for the effect.</param>
|
||||
/// <returns>The <see cref="HRESULT"/> for the operation.</returns>
|
||||
[DllImport("Microsoft.Graphics.Canvas.dll", PreserveSig = true, ExactSpelling = true)]
|
||||
[return: NativeTypeName("HRESULT")]
|
||||
public static extern int GetInvalidRectanglesForICanvasImageInterop(
|
||||
ICanvasResourceCreatorWithDpi* resourceCreator,
|
||||
ICanvasImageInterop* image,
|
||||
uint* valueCount,
|
||||
Rect** valueElements);
|
||||
|
||||
/// <summary>
|
||||
/// Implements support for <see cref="global::Microsoft.Graphics.Canvas.Effects.ICanvasEffect.GetRequiredSourceRectangle"/> and
|
||||
/// <see cref="global::Microsoft.Graphics.Canvas.Effects.ICanvasEffect.GetRequiredSourceRectangles"/> for an external <see cref="ICanvasImageInterop"/> instance.
|
||||
/// </summary>
|
||||
/// <param name="resourceCreator">The input <see cref="ICanvasResourceCreatorWithDpi"/> instance to use to perform the operation.</param>
|
||||
/// <param name="image">The target <see cref="ICanvasImageInterop"/> instance to execute the operation upon.</param>
|
||||
/// <param name="outputRectangle">The portion of the output image whose inputs are being inspected.</param>
|
||||
/// <param name="sourceEffectCount">The length of the sequence of input effects.</param>
|
||||
/// <param name="sourceEffects">The sequence of input effects whose rectangles are being queries.</param>
|
||||
/// <param name="sourceIndexCount">The length of the sequence of indices for the input effects.</param>
|
||||
/// <param name="sourceIndices">The sequence of indices for the input effects.</param>
|
||||
/// <param name="sourceBoundsCount">The length of the sequence of bounds to consider.</param>
|
||||
/// <param name="sourceBounds">The sequence of bounds to consider in the resulting output.</param>
|
||||
/// <param name="valueCount">The number of elements provided in <paramref name="valueElements"/>.</param>
|
||||
/// <param name="valueElements">The sequence of resulting required rectangles to compute.</param>
|
||||
/// <returns>The <see cref="HRESULT"/> for the operation.</returns>
|
||||
[DllImport("Microsoft.Graphics.Canvas.dll", PreserveSig = true, ExactSpelling = true)]
|
||||
[return: NativeTypeName("HRESULT")]
|
||||
public static extern int GetRequiredSourceRectanglesForICanvasImageInterop(
|
||||
ICanvasResourceCreatorWithDpi* resourceCreator,
|
||||
ICanvasImageInterop* image,
|
||||
[NativeTypeName("Windows::Foundation::Rect const*")] Rect* outputRectangle,
|
||||
uint sourceEffectCount,
|
||||
[NativeTypeName("ABI::Microsoft::Graphics::Canvas::Effects::ICanvasEffect* const*")] ICanvasEffect** sourceEffects,
|
||||
uint sourceIndexCount,
|
||||
[NativeTypeName("uint32_t const*")] uint* sourceIndices,
|
||||
uint sourceBoundsCount,
|
||||
[NativeTypeName("Windows::Foundation::Rect const*")] Rect* sourceBounds,
|
||||
uint valueCount,
|
||||
Rect* valueElements);
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
using System.Runtime.CompilerServices;
|
||||
using Windows.Graphics.Effects;
|
||||
|
||||
#pragma warning disable IDE0051
|
||||
|
||||
namespace ComputeSharp.D2D1.WinUI.Buffers;
|
||||
|
||||
/// <summary>
|
||||
/// A fixed buffer type containing 16 <see cref="IGraphicsEffectSource"/> fields.
|
||||
/// </summary>
|
||||
[InlineArray(16)]
|
||||
internal struct GraphicsEffectSourceBuffer
|
||||
{
|
||||
/// <summary>
|
||||
/// The <see cref="SourceReference"/> instance at index 0.
|
||||
/// </summary>
|
||||
private SourceReference source0;
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
using System.Runtime.CompilerServices;
|
||||
using ComputeSharp.D2D1.Interop;
|
||||
|
||||
#pragma warning disable IDE0044, IDE0051
|
||||
|
||||
namespace ComputeSharp.D2D1.WinUI.Buffers;
|
||||
|
||||
/// <summary>
|
||||
/// A fixed buffer type containing 16 <see cref="D2D1ResourceTextureManager"/> fields.
|
||||
/// </summary>
|
||||
[InlineArray(16)]
|
||||
internal struct ResourceTextureManagerBuffer
|
||||
{
|
||||
/// <summary>
|
||||
/// The <see cref="D2D1ResourceTextureManager"/> instance at index 0.
|
||||
/// </summary>
|
||||
private D2D1ResourceTextureManager? resourceTextureManager0;
|
||||
}
|
||||
@@ -1,185 +0,0 @@
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
using ABI.Microsoft.Graphics.Canvas;
|
||||
using ComputeSharp.D2D1.WinUI.Extensions;
|
||||
using ComputeSharp.D2D1.WinUI.Helpers;
|
||||
using ComputeSharp.Win32;
|
||||
using Windows.Graphics.Effects;
|
||||
|
||||
namespace ComputeSharp.D2D1.WinUI.Buffers;
|
||||
|
||||
/// <summary>
|
||||
/// A wrapper type for an input <see cref="IGraphicsEffectSource"/> and its underlying native D2D images.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// This type is meant to support these two common operations:
|
||||
/// <list type="bullet">
|
||||
/// <item>Look up a D2D resource from a property of some other D2D interface.</item>
|
||||
/// <item>Get a managed wrapper from a D2D interface.</item>
|
||||
/// </list>
|
||||
/// With this, the generated D2D objects can be associated with each original managed wrapper. Similarly,
|
||||
/// when the other way around is used, this type can also store the generated wrapper for future use.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// This gives the following improvements:
|
||||
/// <list type="bullet">
|
||||
/// <item>Improved performance for the common case of repeated calls using the same D2D instance.</item>
|
||||
/// <item>Maintaining a reference to the wrapper avoids it being repeatedly released and recreated.</item>
|
||||
/// </list>
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// Just like with all other members of an effect, values in this object are considered authoritative when the
|
||||
/// effect is not realized, otherwise they just act as a cache to speedup the reverse managed wrapper lookups.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// For realized effects, <see cref="SourceReference"/> tracks both the <see cref="ID2D1Image"/> resource and its
|
||||
/// <see cref="IGraphicsEffectSource"/> wrapper (and optional <see cref="ID2D1Effect"/> being the DPI compensation effect,
|
||||
/// if one is needed. For unrealized effects, the resource is <see langword="null"/> and only the wrapper part is used.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
internal unsafe struct SourceReference : IDisposable
|
||||
{
|
||||
/// <summary>
|
||||
/// The <see cref="ID2D1Image"/> produced by calling <see cref="ICanvasImageInterop.GetD2DImage"/>
|
||||
/// on the input managed wrapper used as source for the effect (ie. <see cref="graphicsEffectSourceWrapper"/>).
|
||||
/// </summary>
|
||||
private ComPtr<ID2D1Image> d2D1ImageSource;
|
||||
|
||||
/// <summary>
|
||||
/// The <see cref="ID2D1Effect"/> for DPI compensation wrapping <see cref="d2D1ImageSource"/>, if needed.
|
||||
/// </summary>
|
||||
private ComPtr<ID2D1Effect> d2D1EffectDpiCompensation;
|
||||
|
||||
/// <summary>
|
||||
/// The <see cref="IGraphicsEffectSource"/> instance being a managed wrapper for the current effect input.
|
||||
/// </summary>
|
||||
private IGraphicsEffectSource? graphicsEffectSourceWrapper;
|
||||
|
||||
/// <summary>
|
||||
/// Gets whether or not this source ference has a DPI compensation effect.
|
||||
/// </summary>
|
||||
public readonly bool HasDpiCompensationEffect
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => this.d2D1EffectDpiCompensation.Get() is not null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the <see cref="ID2D1Effect"/> object for the DPI compensation effect used, if any.
|
||||
/// </summary>
|
||||
/// <returns>The <see cref="ID2D1Effect"/> object for the DPI compensation effect used, if any.</returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public readonly ID2D1Effect* GetDpiCompensationEffect()
|
||||
{
|
||||
return this.d2D1EffectDpiCompensation.Get();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the DPI compensation effet being used,
|
||||
/// </summary>
|
||||
/// <param name="d2D1Effect"></param>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void SetDpiCompensationEffect(ID2D1Effect* d2D1Effect)
|
||||
{
|
||||
this.d2D1EffectDpiCompensation.Dispose();
|
||||
|
||||
if (d2D1Effect is not null)
|
||||
{
|
||||
this.d2D1EffectDpiCompensation = new ComPtr<ID2D1Effect>(d2D1Effect);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the <see cref="IGraphicsEffectSource"/> instance being a managed wrapper for the current effect input.
|
||||
/// </summary>
|
||||
/// <returns>The <see cref="IGraphicsEffectSource"/> instance being a managed wrapper for the current effect input.</returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public readonly IGraphicsEffectSource? GetWrapper()
|
||||
{
|
||||
return this.graphicsEffectSourceWrapper;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the <see cref="IGraphicsEffectSource"/> instance being a managed wrapper for the current effect input.
|
||||
/// </summary>
|
||||
/// <param name="wrapper">The <see cref="IGraphicsEffectSource"/> instance being a managed wrapper for the current effect input.</param>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void SetWrapper(IGraphicsEffectSource? wrapper)
|
||||
{
|
||||
this.d2D1ImageSource.Dispose();
|
||||
|
||||
this.graphicsEffectSourceWrapper = wrapper;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the wrapper and underlying realized resource for the current source reference.
|
||||
/// </summary>
|
||||
/// <param name="wrapper">The <see cref="IGraphicsEffectSource"/> instance being a managed wrapper for the current effect input.</param>
|
||||
/// <param name="d2D1Image">The underlying relized <see cref="ID2D1Image"/> instance for <paramref name="wrapper"/>.</param>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void SetWrapperAndResource(IGraphicsEffectSource? wrapper, ID2D1Image* d2D1Image)
|
||||
{
|
||||
this.d2D1ImageSource.Dispose();
|
||||
|
||||
this.d2D1ImageSource = new ComPtr<ID2D1Image>(d2D1Image);
|
||||
this.graphicsEffectSourceWrapper = wrapper;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or creates an <see cref="IGraphicsEffectSource"/> wrapper for a given <see cref="ID2D1Image"/> object.
|
||||
/// </summary>
|
||||
/// <param name="canvasDevice">The input <see cref="ICanvasDevice"/> object the source effect is realized on.</param>
|
||||
/// <param name="d2D1Image">The input <see cref="ID2D1Image"/> object to get or create a wrapper for.</param>
|
||||
/// <returns>An <see cref="IGraphicsEffectSource"/> wrapper for a given <see cref="ID2D1Image"/> object.</returns>
|
||||
public IGraphicsEffectSource? GetOrCreateWrapper(ICanvasDevice* canvasDevice, ID2D1Image* d2D1Image)
|
||||
{
|
||||
// Check if there already is a matching managed wrapper for the input image
|
||||
if (!this.d2D1ImageSource.Get()->IsSameInstance(d2D1Image))
|
||||
{
|
||||
// If that's not the case, create a new wrapper if the input image is not null
|
||||
if (d2D1Image is not null)
|
||||
{
|
||||
this.graphicsEffectSourceWrapper = ResourceManager.GetOrCreate(
|
||||
device: canvasDevice,
|
||||
resource: (IUnknown*)d2D1Image,
|
||||
dpi: 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
this.graphicsEffectSourceWrapper = null;
|
||||
}
|
||||
|
||||
this.d2D1ImageSource = new ComPtr<ID2D1Image>(d2D1Image);
|
||||
}
|
||||
|
||||
return this.graphicsEffectSourceWrapper;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Updates the underlying <see cref="ID2D1Image"/> resource, if needed.
|
||||
/// This is used after a potential re-realization of a lazily bound effect.
|
||||
/// </summary>
|
||||
/// <param name="d2D1Image">The input <see cref="ID2D1Image"/> object.</param>
|
||||
/// <returns>Whether the resource has changed or not.</returns>
|
||||
public bool UpdateResource(ID2D1Image* d2D1Image)
|
||||
{
|
||||
if (d2D1Image->IsSameInstance(this.d2D1ImageSource.Get()))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
this.d2D1ImageSource.Dispose();
|
||||
this.d2D1ImageSource = new ComPtr<ID2D1Image>(d2D1Image);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void Dispose()
|
||||
{
|
||||
this.d2D1ImageSource.Dispose();
|
||||
this.d2D1EffectDpiCompensation.Dispose();
|
||||
this.graphicsEffectSourceWrapper = null;
|
||||
}
|
||||
}
|
||||
@@ -1,115 +0,0 @@
|
||||
using System;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Numerics;
|
||||
using ABI.Microsoft.Graphics.Canvas;
|
||||
using ComputeSharp.D2D1.Extensions;
|
||||
using ComputeSharp.D2D1.WinUI.Helpers;
|
||||
using ComputeSharp.Win32;
|
||||
using Microsoft.Graphics.Canvas;
|
||||
using Windows.Foundation;
|
||||
using ICanvasImageInterop = Microsoft.Graphics.Canvas.ICanvasImageInterop;
|
||||
using ICanvasResourceCreator = Microsoft.Graphics.Canvas.ICanvasResourceCreator;
|
||||
|
||||
namespace ComputeSharp.D2D1.WinUI;
|
||||
|
||||
/// <inheritdoc/>
|
||||
partial class CanvasEffect
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
public Rect GetBounds(ICanvasResourceCreator resourceCreator)
|
||||
{
|
||||
return GetCanvasImage().GetBounds(resourceCreator);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public Rect GetBounds(ICanvasResourceCreator resourceCreator, Matrix3x2 transform)
|
||||
{
|
||||
return GetCanvasImage().GetBounds(resourceCreator, transform);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
unsafe int ICanvasImageInterop.GetDevice(ICanvasDevice** device, WIN2D_GET_DEVICE_ASSOCIATION_TYPE* type)
|
||||
{
|
||||
using ComPtr<global::ABI.Microsoft.Graphics.Canvas.ICanvasImageInterop> canvasImageInterop = default;
|
||||
|
||||
RcwMarshaller.GetNativeInterface(GetCanvasImage(), canvasImageInterop.GetAddressOf()).Assert();
|
||||
|
||||
return canvasImageInterop.Get()->GetDevice(device, type);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
unsafe int ICanvasImageInterop.GetD2DImage(
|
||||
ICanvasDevice* device,
|
||||
ID2D1DeviceContext* deviceContext,
|
||||
WIN2D_GET_D2D_IMAGE_FLAGS flags,
|
||||
float targetDpi,
|
||||
float* realizeDpi,
|
||||
ID2D1Image** ppImage)
|
||||
{
|
||||
using ComPtr<global::ABI.Microsoft.Graphics.Canvas.ICanvasImageInterop> canvasImageInterop = default;
|
||||
|
||||
RcwMarshaller.GetNativeInterface(GetCanvasImage(), canvasImageInterop.GetAddressOf()).Assert();
|
||||
|
||||
return canvasImageInterop.Get()->GetD2DImage(device, deviceContext, flags, targetDpi, realizeDpi, ppImage);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or creates the current <see cref="ICanvasImage"/> instance.
|
||||
/// </summary>
|
||||
/// <returns>The current <see cref="ICanvasImage"/> instance.</returns>
|
||||
/// <exception cref="ObjectDisposedException">Thrown if the current instance is disposed.</exception>
|
||||
[MemberNotNull(nameof(CanvasImage))]
|
||||
private ICanvasImage GetCanvasImage()
|
||||
{
|
||||
lock (this.TransformNodes)
|
||||
{
|
||||
default(ObjectDisposedException).ThrowIf(this.isDisposed, this);
|
||||
|
||||
// Build the effect graph (the output node might not have been set yet)
|
||||
if (this.InvalidationType == CanvasEffectInvalidationType.Creation)
|
||||
{
|
||||
DisposeEffectGraph();
|
||||
BuildEffectGraph(new CanvasEffectGraph(this));
|
||||
|
||||
// We successfully got past the effect graph creation, so update the current
|
||||
// invalidation state. This ensures that even if the configuration failed, the
|
||||
// next time the effect is drawn again the graph won't be created again too.
|
||||
this.InvalidationType = CanvasEffectInvalidationType.Update;
|
||||
}
|
||||
|
||||
// Configure the effect graph, if the effect is invalidated
|
||||
if (this.InvalidationType == CanvasEffectInvalidationType.Update)
|
||||
{
|
||||
ConfigureEffectGraph(new CanvasEffectGraph(this));
|
||||
|
||||
// The effect graph is now ready to go: no further work will be done before drawing
|
||||
// unless it is explicitly invalidated again, using either creation or update mode.
|
||||
this.InvalidationType = default;
|
||||
}
|
||||
|
||||
// At this point, there must be an output canvas image being set.
|
||||
// If not, it means a derived type has forgot to set an output node.
|
||||
default(InvalidOperationException).ThrowIf(this.CanvasImage is null, "No output node is set in the effect graph.");
|
||||
|
||||
return this.CanvasImage;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Disposes and clears the current effect graph, if any.
|
||||
/// </summary>
|
||||
private void DisposeEffectGraph()
|
||||
{
|
||||
// Dispose all registered canvas images
|
||||
foreach (ICanvasImage canvasImage in this.TransformNodes.Values)
|
||||
{
|
||||
canvasImage.Dispose();
|
||||
}
|
||||
|
||||
// Also clear the current effect graph. Note that the canvas image used as output
|
||||
// node for the effect graph does not need to be explicitly disposed here, as that
|
||||
// object is guaranteed to have been part of the dictionary of transform nodes.
|
||||
this.TransformNodes.Clear();
|
||||
this.CanvasImage = null;
|
||||
}
|
||||
}
|
||||
@@ -1,209 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using Microsoft.Graphics.Canvas;
|
||||
|
||||
namespace ComputeSharp.D2D1.WinUI;
|
||||
|
||||
/// <summary>
|
||||
/// A base type to implement packaged and easy to use <see cref="ICanvasImage"/>-based effects that can be used with Win2D.
|
||||
/// </summary>
|
||||
public abstract partial class CanvasEffect : ICanvasImage, ICanvasImageInterop
|
||||
{
|
||||
/// <summary>
|
||||
/// The mapping of registered transform nodes for the current effect graph.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// When not empty (ie. when an effect graph has been built), this will always also include <see cref="CanvasImage"/>.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// This field and the two below are <see langword="internal"/> due to lack of <see langword="friend"/> modifier in C#.
|
||||
/// The <see cref="CanvasEffectGraph"/> type is the only one that needs access to these fields to configure the effect.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
internal readonly Dictionary<object, ICanvasImage> TransformNodes = [];
|
||||
|
||||
/// <summary>
|
||||
/// The current cached result for <see cref="GetCanvasImage"/>, if available.
|
||||
/// </summary>
|
||||
internal ICanvasImage? CanvasImage;
|
||||
|
||||
/// <summary>
|
||||
/// Indicates the current invalidation state for the effect graph (which controls the logic in <see cref="GetCanvasImage"/>).
|
||||
/// </summary>
|
||||
internal CanvasEffectInvalidationType InvalidationType = CanvasEffectInvalidationType.Creation;
|
||||
|
||||
/// <summary>
|
||||
/// Indicates whether the effect is disposed.
|
||||
/// </summary>
|
||||
private bool isDisposed;
|
||||
|
||||
/// <summary>
|
||||
/// Builds the effect graph for the current <see cref="CanvasEffect"/> instance, and configures all effect nodes, as well as the output
|
||||
/// node for the graph. That <see cref="ICanvasImage"/> instance will then be passed to Win2D to perform the actual drawing, when needed.
|
||||
/// </summary>
|
||||
/// <param name="effectGraph">The input <see cref="CanvasEffectGraph"/> value to use to build the effect graph.</param>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// This method is called once before the current effect is drawn, and the resulting image is automatically cached and reused.
|
||||
/// It will remain in use until <see cref="InvalidateEffectGraph"/> is called with <see cref="CanvasEffectInvalidationType.Creation"/>.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// If the effect is invalidated with <see cref="CanvasEffectInvalidationType.Update"/>, only <see cref="ConfigureEffectGraph"/> will be
|
||||
/// called. As such, derived types should save any effect graph nodes that might need updates into instance fields, for later use.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// For instance, consider a <c>FrostedGlassEffect</c> type deriving from <see cref="CanvasEffect"/>, with this effect graph:
|
||||
/// <code>
|
||||
/// ┌────────┐ ┌──────┐ ┌──────┐ ┌───────┐ ┌────────┐
|
||||
/// │ source ├──►│ blur ├──►│ tint ├──►│ noise ├──►│ output │
|
||||
/// └────────┘ └──────┘ └──────┘ └───────┘ └────────┘
|
||||
/// </code>
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// In this example, <c>blur</c> is a <see cref="Microsoft.Graphics.Canvas.Effects.GaussianBlurEffect"/> instance, <c>tint</c>
|
||||
/// is a <see cref="Microsoft.Graphics.Canvas.Effects.TintEffect"/>. and <c>noise</c> is some other custom effect. In this case,
|
||||
/// the <see cref="ICanvasImage"/> instance returned by <see cref="BuildEffectGraph"/> will be <c>noise</c>, as that's the output
|
||||
/// node for the effect graph. At the same time, both <c>blur</c> and <c>tint</c> will also need to be saved as instance fields,
|
||||
/// so that <see cref="ConfigureEffectGraph"/> will be able to set properties on them when needed. For instance, the effect might
|
||||
/// expose a property to control the blur amount, as well as the tint color and opacity.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// Generally speaking, an implementation of <see cref="BuildEffectGraph"/> will consist of these steps:
|
||||
/// <list type="bullet">
|
||||
/// <item>Create an instance of all necessary nodes in the effect graph.</item>
|
||||
/// <item>Connect the effect nodes as needed to build the connected graph.</item>
|
||||
/// <item>Register all effects as effect nodes in the graph, including the output node.</item>
|
||||
/// </list>
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// For convenience, it is recommended to store all necessary <see cref="CanvasEffectNode{T}"/> objects in <see langword="static"/>
|
||||
/// <see langword="readonly"/> fields, so they can easily be accessed from <see cref="BuildEffectGraph"/> and <see cref="ConfigureEffectGraph"/>.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// This method should never be called directly. It is automatically invoked when an effect graph is needed.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
protected abstract void BuildEffectGraph(CanvasEffectGraph effectGraph);
|
||||
|
||||
/// <summary>
|
||||
/// Configures the current effect graph whenever it is invalidated.
|
||||
/// </summary>
|
||||
/// <param name="effectGraph">The input <see cref="CanvasEffectGraph"/> value to use to configure the effect graph.</param>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// This method is guaranteed to be called after <see cref="BuildEffectGraph"/> has been invoked already. As such, any instance
|
||||
/// fields that are set by <see cref="BuildEffectGraph"/> can be assumed to never be <see langword="null"/> when this method runs.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// This method should never be called directly. It is used internally to configure the current effect graph.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
protected abstract void ConfigureEffectGraph(CanvasEffectGraph effectGraph);
|
||||
|
||||
/// <summary>
|
||||
/// Invalidates the last returned result from <see cref="GetCanvasImage"/>.
|
||||
/// </summary>
|
||||
/// <param name="invalidationType">The invalidation type to request.</param>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// This method is used to signal when the effect graph should be updated, and it can indicate
|
||||
/// that either the entire graph should be created again (by calling <see cref="GetCanvasImage"/>),
|
||||
/// or whether it simply needs to refresh its internal state (by calling <see cref="ConfigureEffectGraph"/>).
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// If <see cref="InvalidateEffectGraph"/> is called with <see cref="CanvasEffectInvalidationType.Update"/>, the effect
|
||||
/// graph will only be refreshed the next time the image is actually requested. That is, repeated requests
|
||||
/// for updates do not result in unnecessarily calls to <see cref="ConfigureEffectGraph"/>.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
protected void InvalidateEffectGraph(CanvasEffectInvalidationType invalidationType = CanvasEffectInvalidationType.Update)
|
||||
{
|
||||
lock (this.TransformNodes)
|
||||
{
|
||||
// This method only updates the invalidation type. The next time the effect is drawn,
|
||||
// the current graph (if existing) will be disposed automatically before building a
|
||||
// new one. If the invalidation mode just needs an update, no disposal will happen.
|
||||
// The new invalidation type is always set to the maximum between the current one and
|
||||
// the requested one. This means that eg. if the current invalidation type is creation,
|
||||
// and a property only needing an update is set, the creation request will persist.
|
||||
this.InvalidationType = (CanvasEffectInvalidationType)Math.Max((byte)this.InvalidationType, (byte)invalidationType);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Updates the backing storage for an effect property and checks if it has changed.
|
||||
/// If so, it invalidates the effect graph as well with the requested invalidation type.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of the property that changed.</typeparam>
|
||||
/// <param name="storage">The storage for the effect property value.</param>
|
||||
/// <param name="value">The new effect property value to set.</param>
|
||||
/// <param name="invalidationType">The invalidation type to request.</param>
|
||||
protected void SetAndInvalidateEffectGraph<T>([NotNullIfNotNull(nameof(value))] ref T storage, T value, CanvasEffectInvalidationType invalidationType = CanvasEffectInvalidationType.Update)
|
||||
{
|
||||
if (EqualityComparer<T>.Default.Equals(storage, value))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
storage = value;
|
||||
|
||||
InvalidateEffectGraph(invalidationType);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Disposes any disposable resources in the current instance.
|
||||
/// </summary>
|
||||
/// <param name="disposing">
|
||||
/// Indicates whether the method was called from <see cref="Dispose()"/> of from a finalizer:
|
||||
/// <list type="bullet">
|
||||
/// <item><see langword="true"/>: the method was called from <see cref="Dispose()"/>.</item>
|
||||
/// <item><see langword="false"/>: the method was called from a finalizer.</item>
|
||||
/// </list>
|
||||
/// </param>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// If implementing a type that derives from <see cref="CanvasEffect"/>, this method can be overridden to add logic to dispose
|
||||
/// any additional state. Managed objects should only be disposed when <paramref name="disposing"/> is <see langword="true"/>.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// If the derived type does not need a finalizer (ie. if it doesn't wrap any native resources), this method should not be
|
||||
/// called directly. It will be called automatically by <see cref="Dispose()"/>. If the derived type does instead define a
|
||||
/// finalizer, it should call <see cref="Dispose(bool)"/> with a value of <see langword="false"/>. As mentioned above, when
|
||||
/// implementing <see cref="Dispose(bool)"/> in a derived type, managed objects should only be disposed when
|
||||
/// <paramref name="disposing"/> is <see langword="true"/>, otherwise only unmanaged resorces should be disposed.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// Though the pattern does support this, it is recommended not to add finalizers in types deriving from <see cref="CanvasEffect"/>.
|
||||
/// Rather, if an effect needs to wrap native resources, it should do so via some same managed wrapper type (such as
|
||||
/// <see cref="System.Runtime.InteropServices.SafeHandle"/>), or some other custom unmanaged resource wrapper type.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
/// <seealso href="https://learn.microsoft.com/en-us/dotnet/standard/garbage-collection/implementing-dispose"/>
|
||||
protected virtual void Dispose(bool disposing)
|
||||
{
|
||||
if (disposing)
|
||||
{
|
||||
// Only take a lock and dispose the image if the method was called explicitly from user code. If instead it was
|
||||
// called from the finalizer, we can make no guarantees on the order of objects being collected, meaning that
|
||||
// the lock itself might have already been collected by the time this code runs, which would lead the lock
|
||||
// statement to throw a NullReferenceException. So if a finalizer is running, just let objects be collected
|
||||
// on their own. This is fine here since there are no unmanaged references to free, but just managed wrappers.
|
||||
lock (this.TransformNodes)
|
||||
{
|
||||
DisposeEffectGraph();
|
||||
}
|
||||
}
|
||||
|
||||
this.isDisposed = true;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(disposing: true);
|
||||
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
}
|
||||
@@ -1,214 +0,0 @@
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
using Microsoft.Graphics.Canvas;
|
||||
|
||||
namespace ComputeSharp.D2D1.WinUI;
|
||||
|
||||
/// <summary>
|
||||
/// An object representing an effect graph being built or configured.
|
||||
/// </summary>
|
||||
public readonly ref struct CanvasEffectGraph
|
||||
{
|
||||
/// <summary>
|
||||
/// The owning <see cref="CanvasEffect"/> instance.
|
||||
/// </summary>
|
||||
private readonly CanvasEffect owner;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="CanvasEffectGraph"/> instance with the specified parameters.
|
||||
/// </summary>
|
||||
/// <param name="owner">The owning <see cref="CanvasEffect"/> instance.</param>
|
||||
internal CanvasEffectGraph(CanvasEffect owner)
|
||||
{
|
||||
this.owner = owner;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a previously registered <see cref="ICanvasImage"/> object associated with a given effect graph node.
|
||||
/// </summary>
|
||||
/// <param name="effectNode">The <see cref="ICanvasEffectNode"/> instance to use to lookup the <see cref="ICanvasImage"/> object to retrieve.</param>
|
||||
/// <returns>The <see cref="ICanvasImage"/> object associated with <paramref name="effectNode"/> in the effect graph.</returns>
|
||||
/// <exception cref="ArgumentNullException">Thrown if <paramref name="effectNode"/> is <see langword="null"/>.</exception>
|
||||
/// <exception cref="ArgumentException">Thrown if <paramref name="effectNode"/> is not currently registered in the effect graph.</exception>
|
||||
/// <exception cref="InvalidOperationException">Thrown if the current <see cref="CanvasEffectGraph"/> instance is not valid.</exception>
|
||||
public ICanvasImage GetNode(ICanvasEffectNode effectNode)
|
||||
{
|
||||
default(ArgumentNullException).ThrowIfNull(effectNode);
|
||||
default(InvalidOperationException).ThrowIf(this.owner is null);
|
||||
|
||||
// Try to get the canvas image associated with the input effect node marker.
|
||||
// This must have been previously registered in a call to BuildEffectGraph.
|
||||
if (!this.owner.TransformNodes.TryGetValue(effectNode, out ICanvasImage? canvasImage))
|
||||
{
|
||||
default(ArgumentException).Throw(nameof(effectNode), "The specified node is not registered in the effect graph.");
|
||||
}
|
||||
|
||||
return canvasImage;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a previously registered <typeparamref name="T"/> object (an <see cref="ICanvasImage"/> instance) associated with a given effect graph node.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of <see cref="ICanvasImage"/> object to retrieve.</typeparam>
|
||||
/// <param name="effectNode">The <see cref="ICanvasEffectNode{T}"/> instance to use to lookup the <see cref="ICanvasImage"/> object to retrieve.</param>
|
||||
/// <returns>The <typeparamref name="T"/> object associated with <paramref name="effectNode"/> in the effect graph.</returns>
|
||||
/// <exception cref="ArgumentNullException">Thrown if <paramref name="effectNode"/> is <see langword="null"/>.</exception>
|
||||
/// <exception cref="ArgumentException">Thrown if <paramref name="effectNode"/> is not currently registered in the effect graph.</exception>
|
||||
/// <exception cref="InvalidOperationException">Thrown if the current <see cref="CanvasEffectGraph"/> instance is not valid.</exception>
|
||||
public T GetNode<T>(ICanvasEffectNode<T> effectNode)
|
||||
where T : class, ICanvasImage
|
||||
{
|
||||
default(ArgumentNullException).ThrowIfNull(effectNode);
|
||||
default(InvalidOperationException).ThrowIf(this.owner is null);
|
||||
|
||||
// Retrieve the registered canvas image, same as above
|
||||
if (!this.owner.TransformNodes.TryGetValue(effectNode, out ICanvasImage? canvasImage))
|
||||
{
|
||||
default(ArgumentException).Throw(nameof(effectNode), "The specified node is not registered in the effect graph.");
|
||||
}
|
||||
|
||||
// Return the T node (we can skip the expensive cast since this is guaranteed to be valid).
|
||||
// This is because EffectNode<T> being registered can only associate T instances with them.
|
||||
return Unsafe.As<T>(canvasImage);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Registers an <see cref="ICanvasImage"/> object in the effect graph.
|
||||
/// </summary>
|
||||
/// <param name="canvasImage">The <see cref="ICanvasImage"/> object to register in the effect graph.</param>
|
||||
/// <exception cref="ArgumentNullException">Thrown if <paramref name="canvasImage"/> is <see langword="null"/>.</exception>
|
||||
/// <exception cref="InvalidOperationException">
|
||||
/// Thrown if the current <see cref="CanvasEffectGraph"/> instance is not valid or if the effect graph does not support modifications at this time.
|
||||
/// </exception>
|
||||
/// <remarks>
|
||||
/// <para><inheritdoc cref="RegisterNode{T}(CanvasEffectNode{T}, T)" path="/remarks/node()"/></para>
|
||||
/// <para>
|
||||
/// This method can be used when <paramref name="canvasImage"/> doesn't need to be retrieved from <see cref="CanvasEffect.ConfigureEffectGraph"/>, ie.
|
||||
/// when it has no properties that the current effect instance will need to mutate over it. Using this method still allows the effect to
|
||||
/// correctly track the image ownership, so that it can be disposed when the current effect instance is disposed.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// If performing lookups on <paramref name="canvasImage"/> is required, use the <see cref="RegisterNode{T}(CanvasEffectNode{T}, T)"/> overload.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
public void RegisterNode(ICanvasImage canvasImage)
|
||||
{
|
||||
default(ArgumentNullException).ThrowIfNull(canvasImage);
|
||||
default(InvalidOperationException).ThrowIf(this.owner is null);
|
||||
default(InvalidOperationException).ThrowIf(this.owner.InvalidationType != CanvasEffectInvalidationType.Creation, "The effect graph cannot be modified after its creation.");
|
||||
|
||||
// Use a new dummy object as key to register the anonymous effect node. This is cheaper than having to maintain a different
|
||||
// data structure (eg. a HashSet<ICanvasImage> instance) just to hold the anonymous effects that may be registered. This way,
|
||||
// the same dictionary can be reused for all kinds of effect nodes instead. Anonymous effects are generally a nicher scenario.
|
||||
_ = this.owner.TransformNodes.TryAdd(new object(), canvasImage);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Registers an <see cref="ICanvasImage"/> object in the effect graph, associated with a given <see cref="CanvasEffectNode{T}"/> instance.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of <see cref="ICanvasImage"/> object to register.</typeparam>
|
||||
/// <param name="effectNode">The <see cref="CanvasEffectNode{T}"/> instance to use to register <paramref name="canvasImage"/>.</param>
|
||||
/// <param name="canvasImage">The <see cref="ICanvasImage"/> object to register in the effect graph.</param>
|
||||
/// <exception cref="ArgumentNullException">Thrown if <paramref name="effectNode"/> or <paramref name="canvasImage"/> are <see langword="null"/>.</exception>
|
||||
/// <exception cref="InvalidOperationException">
|
||||
/// Thrown if the current <see cref="CanvasEffectGraph"/> instance is not valid or if the effect graph does not support modifications at this time.
|
||||
/// </exception>
|
||||
/// <exception cref="ArgumentException">Thrown if <paramref name="effectNode"/> is already registered in the effect graph.</exception>
|
||||
/// <remarks>
|
||||
/// This method can only be called from an <see cref="CanvasEffectGraph"/> value being passed to <see cref="CanvasEffect.BuildEffectGraph"/>. If this
|
||||
/// method is called from within <see cref="CanvasEffect.ConfigureEffectGraph"/>, it will fail, as the effect graph cannot be mutated from there.
|
||||
/// </remarks>
|
||||
public void RegisterNode<T>(CanvasEffectNode<T> effectNode, T canvasImage)
|
||||
where T : class, ICanvasImage
|
||||
{
|
||||
default(ArgumentNullException).ThrowIfNull(effectNode);
|
||||
default(ArgumentNullException).ThrowIfNull(canvasImage);
|
||||
default(InvalidOperationException).ThrowIf(this.owner is null);
|
||||
default(InvalidOperationException).ThrowIf(this.owner.InvalidationType != CanvasEffectInvalidationType.Creation, "The effect graph cannot be modified after its creation.");
|
||||
|
||||
// Try to add the new canvas image associated with the input effect node marker.
|
||||
// This must not have been previously added from another RegisterNode call.
|
||||
if (!this.owner.TransformNodes.TryAdd(effectNode, canvasImage))
|
||||
{
|
||||
default(ArgumentException).Throw(nameof(effectNode), "The specified node is already registered in the effect graph.");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Registers an <see cref="ICanvasImage"/> object in the effect graph, and marks it as the output node for the effect graph being built.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <para><inheritdoc cref="RegisterNode{T}(CanvasEffectNode{T}, T)" path="/remarks/node()"/></para>
|
||||
/// <para>
|
||||
/// This method can be used when <paramref name="canvasImage"/> doesn't need to be retrieved from <see cref="CanvasEffect.ConfigureEffectGraph"/>, ie.
|
||||
/// when it has no properties that the current effect instance will need to mutate over it. Using this method still allows the effect to
|
||||
/// correctly track the image ownership, so that it can be disposed when the current effect instance is disposed.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// If performing lookups on <paramref name="canvasImage"/> is required, use the <see cref="RegisterOutputNode{T}(CanvasEffectNode{T}, T)"/> overload.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
/// <inheritdoc cref="RegisterNode(ICanvasImage)"/>
|
||||
public void RegisterOutputNode(ICanvasImage canvasImage)
|
||||
{
|
||||
default(ArgumentNullException).ThrowIfNull(canvasImage);
|
||||
default(InvalidOperationException).ThrowIf(this.owner is null);
|
||||
default(InvalidOperationException).ThrowIf(this.owner.InvalidationType != CanvasEffectInvalidationType.Creation, "The effect graph cannot be modified after its creation.");
|
||||
|
||||
// Register the anonymous effect node with a dummy object, same as above
|
||||
_ = this.owner.TransformNodes.TryAdd(new object(), canvasImage);
|
||||
|
||||
// Store the anonymous output node for later use
|
||||
this.owner.CanvasImage = canvasImage;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Registers an <see cref="ICanvasImage"/> object in the effect graph, associated with a given <see cref="CanvasEffectNode{T}"/> instance.
|
||||
/// Additionally, it also marks the input <see cref="ICanvasImage"/> object as the output node for the effect graph being built.
|
||||
/// </summary>
|
||||
/// <inheritdoc cref="RegisterNode"/>
|
||||
public void RegisterOutputNode<T>(CanvasEffectNode<T> effectNode, T canvasImage)
|
||||
where T : class, ICanvasImage
|
||||
{
|
||||
default(ArgumentNullException).ThrowIfNull(effectNode);
|
||||
default(ArgumentNullException).ThrowIfNull(canvasImage);
|
||||
default(InvalidOperationException).ThrowIf(this.owner is null);
|
||||
default(InvalidOperationException).ThrowIf(this.owner.InvalidationType != CanvasEffectInvalidationType.Creation, "The effect graph cannot be modified after its creation.");
|
||||
|
||||
// Try to add the output node as in the method above (but with an extra check before doing so)
|
||||
if (!this.owner.TransformNodes.TryAdd(effectNode, canvasImage))
|
||||
{
|
||||
default(ArgumentException).Throw(nameof(effectNode), "The specified node is already registered in the effect graph.");
|
||||
}
|
||||
|
||||
// Store the output node for later use
|
||||
this.owner.CanvasImage = canvasImage;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets a previously registered <see cref="ICanvasEffectNode"/> instance as the output node for the effect graph.
|
||||
/// </summary>
|
||||
/// <param name="effectNode">The <see cref="ICanvasEffectNode"/> instance to set as output node for the effect graph.</param>
|
||||
/// <exception cref="ArgumentNullException">Thrown if <paramref name="effectNode"/> is <see langword="null"/>.</exception>
|
||||
/// <exception cref="InvalidOperationException">Thrown if the current <see cref="CanvasEffectGraph"/> instance is not valid.</exception>
|
||||
/// <exception cref="ArgumentException">Thrown if <paramref name="effectNode"/> is not registered in the effect graph.</exception>
|
||||
/// <remarks>
|
||||
/// This method can be called from both <see cref="CanvasEffect.BuildEffectGraph"/> and <see cref="CanvasEffect.ConfigureEffectGraph"/>.
|
||||
/// It can be used for efficiently changing the output node of a graph, without having to rebuild it. For instance, this can be used for
|
||||
/// effects that have multiple paths that can be taken for their inputs, depending on the value of some property describing the effect behavior.
|
||||
/// </remarks>
|
||||
public void SetOutputNode(ICanvasEffectNode effectNode)
|
||||
{
|
||||
default(ArgumentNullException).ThrowIfNull(effectNode);
|
||||
default(InvalidOperationException).ThrowIf(this.owner is null);
|
||||
|
||||
// Get the canvas image for the registered effect node (which has to exist at this point)
|
||||
if (!this.owner.TransformNodes.TryGetValue(effectNode, out ICanvasImage? canvasImage))
|
||||
{
|
||||
default(ArgumentException).Throw(nameof(effectNode), "The specified output node is not registered in the effect graph.");
|
||||
}
|
||||
|
||||
// Store the new output node
|
||||
this.owner.CanvasImage = canvasImage;
|
||||
}
|
||||
}
|
||||
@@ -1,34 +0,0 @@
|
||||
using ABI.Microsoft.Graphics.Canvas;
|
||||
using Microsoft.Graphics.Canvas;
|
||||
|
||||
namespace ComputeSharp.D2D1.WinUI;
|
||||
|
||||
/// <summary>
|
||||
/// Indicates the invalidation type to request when invoking <see cref="CanvasEffect.InvalidateEffectGraph"/> and related methods.
|
||||
/// </summary>
|
||||
public enum CanvasEffectInvalidationType : byte
|
||||
{
|
||||
/// <summary>
|
||||
/// Invalidates the state of the effect graph, causing it to be configured again the next time it is used.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// This will preserve the last returned <see cref="ICanvasImage"/> instance, if available, and it will only
|
||||
/// mark the internal state as being out of date, resulting in <see cref="CanvasEffect.ConfigureEffectGraph"/> to be called
|
||||
/// the next time the effect is used for drawing.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// This is much less expensive than creating the effect graph again, and should be preferred if possible.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
Update,
|
||||
|
||||
/// <summary>
|
||||
/// Fully invalidates the effect graph, causing it to be created again the next time it is needed.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This will cause the last returned <see cref="ICanvasImage"/> instance to be disposed and discarded,
|
||||
/// and <see cref="CanvasEffect.BuildEffectGraph"/> to be called again the next time the effect is used for drawing.
|
||||
/// </remarks>
|
||||
Creation
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
using Microsoft.Graphics.Canvas;
|
||||
|
||||
namespace ComputeSharp.D2D1.WinUI;
|
||||
|
||||
/// <summary>
|
||||
/// A marker type for an effect node that can be registered and retrieved from an <see cref="CanvasEffectGraph"/> value.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of <see cref="ICanvasImage"/> associated with the current effect node.</typeparam>
|
||||
public sealed class CanvasEffectNode<T> : ICanvasEffectNode<T>
|
||||
where T : class, ICanvasImage;
|
||||
@@ -1,89 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace ComputeSharp.D2D1.WinUI.Collections;
|
||||
|
||||
/// <summary>
|
||||
/// Shared helpers for all types implementing <see cref="IFixedCountList{T}"/>.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of elements in the list.</typeparam>
|
||||
internal static class FixedCountList<T>
|
||||
{
|
||||
/// <summary>
|
||||
/// Copies the entire <see cref="IFixedCountList{T}"/> instance to a target array range.
|
||||
/// </summary>
|
||||
/// <param name="list">The input <see cref="IFixedCountList{T}"/> instance.</param>
|
||||
/// <param name="array">The array to copy elements to.</param>
|
||||
/// <param name="index">The initial index within <paramref name="array"/>.</param>
|
||||
public static void CopyTo(IFixedCountList<T> list, T[] array, int index)
|
||||
{
|
||||
default(ArgumentNullException).ThrowIfNull(array);
|
||||
|
||||
Span<T> span = array.AsSpan(index);
|
||||
|
||||
default(ArgumentException).ThrowIf(list.Indices.Length > span.Length, nameof(array));
|
||||
|
||||
foreach (int i in list.Indices)
|
||||
{
|
||||
span[i] = list[i];
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Copies the entire <see cref="IFixedCountList{T}"/> instance to a target array range.
|
||||
/// </summary>
|
||||
/// <param name="list">The input <see cref="IFixedCountList{T}"/> instance.</param>
|
||||
/// <param name="array">The array to copy elements to.</param>
|
||||
/// <param name="index">The initial index within <paramref name="array"/>.</param>
|
||||
public static void CopyTo(IFixedCountList<T> list, Array array, int index)
|
||||
{
|
||||
default(ArgumentNullException).ThrowIfNull(array);
|
||||
default(ArgumentOutOfRangeException).ThrowIfNotInRange(index, 0, array.Length);
|
||||
|
||||
int remainingLength = array.Length - index;
|
||||
|
||||
default(ArgumentException).ThrowIf(list.Indices.Length > remainingLength, nameof(array));
|
||||
|
||||
foreach (int i in list.Indices)
|
||||
{
|
||||
array.SetValue(list[i], i);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates an <see cref="IEnumerable{T}"/> for an input <see cref="IFixedCountList{T}"/> instance.
|
||||
/// </summary>
|
||||
/// <param name="list">The input <see cref="IFixedCountList{T}"/> instance.</param>
|
||||
/// <returns>An <see cref="IEnumerable{T}"/> instance for <paramref name="list"/>.</returns>
|
||||
public static IEnumerator<T> GetEnumerator(IFixedCountList<T> list)
|
||||
{
|
||||
foreach (int i in list.Indices)
|
||||
{
|
||||
yield return list[i];
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Searches for the specified object and returns the zero-based index of the first occurrence
|
||||
/// within the range of elements in a given <see cref="IFixedCountList{T}"/> instance.
|
||||
/// </summary>
|
||||
/// <param name="list">The input <see cref="IFixedCountList{T}"/> instance.</param>
|
||||
/// <param name="value">
|
||||
/// The object to locate in <paramref name="list"/>. The value can be <see langword="null"/> for reference types.
|
||||
/// </param>
|
||||
/// <returns>
|
||||
/// The zero-based index of the first occurrence of item within <paramref name="list"/>, otherwise -1.
|
||||
/// </returns>
|
||||
public static int IndexOf(IFixedCountList<T> list, T value)
|
||||
{
|
||||
foreach (int i in list.Indices)
|
||||
{
|
||||
if (EqualityComparer<T>.Default.Equals(list[i], value))
|
||||
{
|
||||
return i;
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
@@ -1,19 +0,0 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
|
||||
namespace ComputeSharp.D2D1.WinUI.Collections;
|
||||
|
||||
/// <summary>
|
||||
/// An interface for a list with a fixed collection.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of elements in the list.</typeparam>
|
||||
internal interface IFixedCountList<T>
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the collection of valid indices for the current effect.
|
||||
/// </summary>
|
||||
ImmutableArray<int> Indices { get; }
|
||||
|
||||
/// <inheritdoc cref="IList{T}.this"/>
|
||||
T this[int index] { get; set; }
|
||||
}
|
||||
@@ -30,11 +30,6 @@
|
||||
<NoWarn>$(NoWarn);IDE0065</NoWarn>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Compile Include="..\ComputeSharp\Graphics\Interop\IReferenceTrackedObject.cs" Link="Interop\IReferenceTrackedObject.cs" />
|
||||
<Compile Include="..\ComputeSharp\Graphics\Interop\ReferenceTracker.cs" Link="Interop\ReferenceTracker.cs" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Graphics.Win2D" Version="1.2.1-experimental1" />
|
||||
|
||||
@@ -48,4 +43,6 @@
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\ComputeSharp.D2D1\ComputeSharp.D2D1.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<Import Project="..\ComputeSharp.D2D1.UI\ComputeSharp.D2D1.UI.projitems" Label="Shared" />
|
||||
</Project>
|
||||
@@ -1,77 +0,0 @@
|
||||
using ABI.Microsoft.Graphics.Canvas;
|
||||
using ComputeSharp.D2D1.Extensions;
|
||||
using ComputeSharp.Win32;
|
||||
|
||||
namespace ComputeSharp.D2D1.WinUI.Extensions;
|
||||
|
||||
/// <summary>
|
||||
/// A <see langword="class"/> with extensions for the <see cref="ICanvasDevice"/> type.
|
||||
/// </summary>
|
||||
internal static unsafe class ICanvasDeviceExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the underlying <see cref="ID2D1Device1"/> object from an <see cref="ICanvasDevice"/> object.
|
||||
/// </summary>
|
||||
/// <param name="canvasDevice">The input <see cref="ICanvasDevice"/> object.</param>
|
||||
/// <param name="d2D1Device1">The underlying <see cref="ID2D1Device1"/> object for <paramref name="canvasDevice"/>.</param>
|
||||
/// <returns>The <see cref="HRESULT"/> for the operation.</returns>
|
||||
public static HRESULT GetD2DDevice(this ref ICanvasDevice canvasDevice, ID2D1Device1** d2D1Device1)
|
||||
{
|
||||
using ComPtr<ICanvasResourceWrapperNative> canvasDeviceResourceWrapperNative = default;
|
||||
|
||||
// Get the ICanvasResourceWrapperNative wrapper around the input device
|
||||
canvasDevice.QueryInterface(
|
||||
riid: Win32.Windows.__uuidof<ICanvasResourceWrapperNative>(),
|
||||
ppvObject: (void**)canvasDeviceResourceWrapperNative.GetAddressOf()).Assert();
|
||||
|
||||
// Get the underlying ID2D1Device1 instance in use
|
||||
HRESULT hresult = canvasDeviceResourceWrapperNative.Get()->GetNativeResource(
|
||||
device: null,
|
||||
dpi: 0,
|
||||
iid: Win32.Windows.__uuidof<ID2D1Device1>(),
|
||||
resource: (void**)d2D1Device1);
|
||||
|
||||
return hresult;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieves a usable <see cref="ID2D1DeviceContext"/> from an input <see cref="ICanvasDevice"/> if one is not available.
|
||||
/// If a new one had to be rented from the pool, it also returns the owning <see cref="ID2D1DeviceContextLease"/> instance.
|
||||
/// </summary>
|
||||
/// <param name="canvasDevice">The input <see cref="ICanvasDevice"/> object.</param>
|
||||
/// <param name="d2D1DeviceContext">The current <see cref="ID2D1DeviceContext"/> object, if available.</param>
|
||||
/// <param name="d2D1DeviceContextEffective">The resulting <see cref="ID2D1DeviceContext"/> instance to use.</param>
|
||||
/// <param name="d2D1DeviceContextLease">The resulting <see cref="ID2D1DeviceContextLease"/> object, if used.</param>
|
||||
/// <remarks>
|
||||
/// When this method returns, <paramref name="d2D1DeviceContextEffective"/> is guaranteed to not be <see langword="null"/>.
|
||||
/// </remarks>
|
||||
public static void GetEffectiveD2DDeviceContextWithOptionalLease(
|
||||
this ref ICanvasDevice canvasDevice,
|
||||
ID2D1DeviceContext* d2D1DeviceContext,
|
||||
ID2D1DeviceContext** d2D1DeviceContextEffective,
|
||||
ID2D1DeviceContextLease** d2D1DeviceContextLease)
|
||||
{
|
||||
// If there is no input device context, just create a new one from the input canvas device
|
||||
if (d2D1DeviceContext is null)
|
||||
{
|
||||
using (ComPtr<ID2D1DeviceContextPool> d2D1DeviceContextPool = default)
|
||||
{
|
||||
// Get the ID2D1DeviceContextPool interface reference
|
||||
canvasDevice.QueryInterface(
|
||||
riid: Win32.Windows.__uuidof<ID2D1DeviceContextPool>(),
|
||||
ppvObject: (void**)d2D1DeviceContextPool.GetAddressOf()).Assert();
|
||||
|
||||
// Get a new ID2D1DeviceContextLease object from the retrieved pool
|
||||
d2D1DeviceContextPool.Get()->GetDeviceContextLease(d2D1DeviceContextLease).Assert();
|
||||
}
|
||||
|
||||
// Get the underlying device context from the lease (this is much faster than creating a new device context)
|
||||
(*d2D1DeviceContextLease)->GetD2DDeviceContext(d2D1DeviceContextEffective).Assert();
|
||||
}
|
||||
else
|
||||
{
|
||||
// Otherwise, just use the input device context
|
||||
*d2D1DeviceContextEffective = new ComPtr<ID2D1DeviceContext>(d2D1DeviceContext).Get();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,79 +0,0 @@
|
||||
using ComputeSharp.D2D1.Extensions;
|
||||
using ComputeSharp.Win32;
|
||||
|
||||
namespace ComputeSharp.D2D1.WinUI.Extensions;
|
||||
|
||||
/// <summary>
|
||||
/// A <see langword="class"/> with extensions for the <see cref="ID2D1DeviceContext"/> type.
|
||||
/// </summary>
|
||||
internal static unsafe class ID2D1DeviceContextExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Checks whether a given <see cref="ID2D1DeviceContext"/> has an <see cref="ID2D1CommandList"/> as render target.
|
||||
/// </summary>
|
||||
/// <param name="deviceContext">The input <see cref="ID2D1DeviceContext"/> instance to check.</param>
|
||||
/// <returns>Whether the target of <paramref name="deviceContext"/> is an <see cref="ID2D1CommandList"/>.</returns>
|
||||
public static bool HasCommandListTarget(this ref ID2D1DeviceContext deviceContext)
|
||||
{
|
||||
using ComPtr<ID2D1Image> d2D1Image = default;
|
||||
|
||||
// First get the target and check that it is not null
|
||||
deviceContext.GetTarget(d2D1Image.GetAddressOf());
|
||||
|
||||
if (d2D1Image.Get() is null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
using ComPtr<ID2D1CommandList> d2D1CommandList = default;
|
||||
|
||||
// If there is a target, check if it is an ID2D1CommandList
|
||||
HRESULT hresult = d2D1Image.CopyTo(d2D1CommandList.GetAddressOf());
|
||||
|
||||
// Special case E_NOINTERFACE: this should return false and it's not an error
|
||||
if (hresult == E.E_NOINTERFACE)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Throw for any other failure
|
||||
hresult.Assert();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads the DPI value from a target <see cref="ID2D1DeviceContext"/> object.
|
||||
/// </summary>
|
||||
/// <param name="deviceContext">The input <see cref="ID2D1DeviceContext"/> instance.</param>
|
||||
/// <returns>The DPIs from <paramref name="deviceContext"/>.</returns>
|
||||
public static float GetDpi(this ref ID2D1DeviceContext deviceContext)
|
||||
{
|
||||
float dpiX;
|
||||
float dpiY;
|
||||
|
||||
deviceContext.GetDpi(&dpiX, &dpiY);
|
||||
|
||||
return dpiX;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets an <see cref="ID2D1Factory1"/> object from a given <see cref="ID2D1DeviceContext"/>.
|
||||
/// </summary>
|
||||
/// <param name="deviceContext">The input <see cref="ID2D1DeviceContext"/> instance.</param>
|
||||
/// <param name="d2D1Factory1">The resulting <see cref="ID2D1Factory1"/> object.</param>
|
||||
/// <returns>The <see cref="HRESULT"/> for the operation.</returns>
|
||||
/// <remarks>
|
||||
/// The <see cref="Interop.D2D1PixelShaderEffect"/> APIs specifically need an <see cref="ID2D1Factory1"/> (as <see cref="ID2D1Factory1.RegisterEffectFromString"/> is used).
|
||||
/// </remarks>
|
||||
public static HRESULT GetFactory1(this ref ID2D1DeviceContext deviceContext, ID2D1Factory1** d2D1Factory1)
|
||||
{
|
||||
using ComPtr<ID2D1Factory> d2D1Factory = default;
|
||||
|
||||
// Get the underlying ID2D1Factory from the input context, which can be used to register effects
|
||||
deviceContext.GetFactory(d2D1Factory.GetAddressOf());
|
||||
|
||||
// QueryInterface for the ID2D1Factory1 object (which should always be present)
|
||||
return d2D1Factory.CopyTo(d2D1Factory1);
|
||||
}
|
||||
}
|
||||
@@ -1,233 +0,0 @@
|
||||
using System;
|
||||
using System.Buffers;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using ComputeSharp.D2D1.Descriptors;
|
||||
using ComputeSharp.D2D1.Extensions;
|
||||
using ComputeSharp.D2D1.Interop;
|
||||
using ComputeSharp.D2D1.Shaders.Interop.Effects.ResourceManagers;
|
||||
using ComputeSharp.D2D1.Shaders.Interop.Effects.TransformMappers;
|
||||
using ComputeSharp.Win32;
|
||||
|
||||
namespace ComputeSharp.D2D1.WinUI.Extensions;
|
||||
|
||||
/// <summary>
|
||||
/// A <see langword="class"/> with extensions for the <see cref="ID2D1Effect"/> type.
|
||||
/// </summary>
|
||||
internal static unsafe class ID2D1EffectExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the constant buffer of type <typeparamref name="T"/> from a given <see cref="ID2D1Effect"/> object.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of shader being used.</typeparam>
|
||||
/// <param name="d2D1Effect">The input <see cref="ID2D1Effect"/> instance.</param>
|
||||
/// <returns>The constant buffer of type <typeparamref name="T"/> for <paramref name="d2D1Effect"/>.</returns>
|
||||
public static T GetConstantBuffer<T>(this ref ID2D1Effect d2D1Effect)
|
||||
where T : unmanaged, ID2D1PixelShader, ID2D1PixelShaderDescriptor<T>
|
||||
{
|
||||
int constantBufferSize = T.ConstantBufferSize;
|
||||
|
||||
// Special cases the constant buffer type being empty. If that's the case, there is no
|
||||
// need to try to retrieve the constant buffer from the underlying effect anyway, and
|
||||
// most importantly, the ID2D1EffectImpl from ComputeSharp.D2D1 will return E_INVALIDARG.
|
||||
if (constantBufferSize == 0)
|
||||
{
|
||||
return default;
|
||||
}
|
||||
|
||||
byte[]? bufferArray = null;
|
||||
|
||||
Span<byte> buffer = constantBufferSize <= 64
|
||||
? stackalloc byte[64]
|
||||
: bufferArray = ArrayPool<byte>.Shared.Rent(constantBufferSize);
|
||||
|
||||
fixed (byte* p = buffer)
|
||||
{
|
||||
// Get the raw constant buffer from the effect
|
||||
d2D1Effect.GetValue(
|
||||
index: D2D1PixelShaderEffectProperty.ConstantBuffer,
|
||||
type: D2D1_PROPERTY_TYPE.D2D1_PROPERTY_TYPE_BLOB,
|
||||
data: p,
|
||||
dataSize: (uint)constantBufferSize).Assert();
|
||||
}
|
||||
|
||||
T shader = T.CreateFromConstantBuffer(buffer);
|
||||
|
||||
if (bufferArray is not null)
|
||||
{
|
||||
ArrayPool<byte>.Shared.Return(bufferArray);
|
||||
}
|
||||
|
||||
return shader;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the constant buffer of type <typeparamref name="T"/> from a given <see cref="ID2D1Effect"/> object.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of shader being used.</typeparam>
|
||||
/// <param name="d2D1Effect">The input <see cref="ID2D1Effect"/> instance.</param>
|
||||
/// <param name="value">The constant buffer value to set.</param>
|
||||
public static void SetConstantBuffer<T>(this ref ID2D1Effect d2D1Effect, in T value)
|
||||
where T : unmanaged, ID2D1PixelShader, ID2D1PixelShaderDescriptor<T>
|
||||
{
|
||||
// Same as above, simply do nothing for empty types. This case is still handled to
|
||||
// avoid crashing when someone's just setting an empty constant buffer, which is
|
||||
// unnecessary but still valid (eg. a stateless shader). This could be the case if
|
||||
// someone was eg. binding or using some other automated system to set constant buffer.
|
||||
if (T.ConstantBufferSize == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
D2D1PixelShaderEffect.SetConstantBufferForD2D1Effect((ID2D1Effect*)Unsafe.AsPointer(ref d2D1Effect), in value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the <see cref="D2D1DrawTransformMapper{T}"/> instance from a given <see cref="ID2D1Effect"/> object.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of shader being used.</typeparam>
|
||||
/// <param name="d2D1Effect">The input <see cref="ID2D1Effect"/> instance.</param>
|
||||
/// <returns>The <see cref="D2D1DrawTransformMapper{T}"/> instance for <paramref name="d2D1Effect"/>.</returns>
|
||||
public static D2D1DrawTransformMapper<T>? GetTransformMapper<T>(this ref ID2D1Effect d2D1Effect)
|
||||
where T : unmanaged, ID2D1PixelShader, ID2D1PixelShaderDescriptor<T>
|
||||
{
|
||||
using ComPtr<ID2D1DrawTransformMapper> d2D1TransformMapper = default;
|
||||
|
||||
// Get the ID2D1DrawTransformMapper object from the effect
|
||||
d2D1Effect.GetValue(
|
||||
index: D2D1PixelShaderEffectProperty.TransformMapper,
|
||||
type: D2D1_PROPERTY_TYPE.D2D1_PROPERTY_TYPE_IUNKNOWN,
|
||||
data: (byte*)d2D1TransformMapper.GetAddressOf(),
|
||||
dataSize: (uint)sizeof(void*)).Assert();
|
||||
|
||||
// If there is no transform, just return null (this is the default state)
|
||||
if (d2D1TransformMapper.Get() is null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
// Otherwise, check if it's an internal CCW and retrieve the managed wrapper
|
||||
using ComPtr<ID2D1DrawTransformMapperInternal> d2D1TransformMapperInternal = default;
|
||||
|
||||
d2D1TransformMapper.CopyTo(d2D1TransformMapperInternal.GetAddressOf()).Assert();
|
||||
|
||||
IntPtr handlePtr;
|
||||
|
||||
// Get the underlying GCHandle value
|
||||
d2D1TransformMapperInternal.Get()->GetManagedWrapperHandle((void**)&handlePtr).Assert();
|
||||
|
||||
// Retrieve the managed wrapper from the GCHandle
|
||||
return (D2D1DrawTransformMapper<T>)GCHandle.FromIntPtr(handlePtr).Target!;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the <see cref="D2D1ResourceTextureManager"/> instance from a given <see cref="ID2D1Effect"/> object.
|
||||
/// </summary>
|
||||
/// <param name="d2D1Effect">The input <see cref="ID2D1Effect"/> instance.</param>
|
||||
/// <param name="source">The current cached source instance.</param>
|
||||
/// <param name="index">The index of the resource texture manager to retrieve.</param>
|
||||
/// <returns>The <see cref="D2D1ResourceTextureManager"/> instance for <paramref name="d2D1Effect"/>.</returns>
|
||||
public static D2D1ResourceTextureManager? GetResourceTextureManager(
|
||||
this ref ID2D1Effect d2D1Effect,
|
||||
D2D1ResourceTextureManager? source,
|
||||
int index)
|
||||
{
|
||||
using ComPtr<ID2D1ResourceTextureManager> d2D1ResourceTextureManager = default;
|
||||
|
||||
// Get the ID2D1ResourceTextureManager object from the effect
|
||||
d2D1Effect.GetValue(
|
||||
index: (uint)(D2D1PixelShaderEffectProperty.ResourceTextureManager0 + index),
|
||||
type: D2D1_PROPERTY_TYPE.D2D1_PROPERTY_TYPE_IUNKNOWN,
|
||||
data: (byte*)d2D1ResourceTextureManager.GetAddressOf(),
|
||||
dataSize: (uint)sizeof(void*)).Assert();
|
||||
|
||||
// If there is no resource texture manager, just return null (this is the default state)
|
||||
if (d2D1ResourceTextureManager.Get() is null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
// Otherwise, the source must not be null and wrapping the same CCW
|
||||
if (source is not null)
|
||||
{
|
||||
using ComPtr<ID2D1ResourceTextureManager> d2D1ResourceTextureManagerSource = default;
|
||||
|
||||
source.GetD2D1ResourceTextureManager(d2D1ResourceTextureManagerSource.GetAddressOf());
|
||||
|
||||
// Check if the instances match, and return the managed wrapper we already have
|
||||
if (d2D1ResourceTextureManager.Get()->IsSameInstance(d2D1ResourceTextureManagerSource.Get()))
|
||||
{
|
||||
return source;
|
||||
}
|
||||
}
|
||||
|
||||
// There was no way to retrieve the correct managed wrapper
|
||||
return default(InvalidOperationException).Throw<D2D1ResourceTextureManager?>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the <see cref="D2D1_PROPERTY.D2D1_PROPERTY_CACHED"/> property for a given <see cref="ID2D1Effect"/> object.
|
||||
/// </summary>
|
||||
/// <param name="d2D1Effect">The input <see cref="ID2D1Effect"/> instance.</param>
|
||||
/// <returns>The <see cref="D2D1_PROPERTY.D2D1_PROPERTY_CACHED"/> value for <paramref name="d2D1Effect"/>.</returns>
|
||||
public static bool GetCachedProperty(this ref ID2D1Effect d2D1Effect)
|
||||
{
|
||||
int cacheOutput;
|
||||
|
||||
d2D1Effect.GetValue(
|
||||
index: (uint)D2D1_PROPERTY.D2D1_PROPERTY_CACHED,
|
||||
type: D2D1_PROPERTY_TYPE.D2D1_PROPERTY_TYPE_BOOL,
|
||||
data: (byte*)&cacheOutput,
|
||||
dataSize: sizeof(int)).Assert();
|
||||
|
||||
return cacheOutput != 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the <see cref="D2D1_PROPERTY.D2D1_PROPERTY_CACHED"/> property for a given <see cref="ID2D1Effect"/> object.
|
||||
/// </summary>
|
||||
/// <param name="d2D1Effect">The input <see cref="ID2D1Effect"/> instance.</param>
|
||||
/// <param name="value">The property value to set.</param>
|
||||
public static void SetCachedProperty(this ref ID2D1Effect d2D1Effect, bool value)
|
||||
{
|
||||
int cacheOutput = value ? 1 : 0;
|
||||
|
||||
d2D1Effect.SetValue(
|
||||
index: (uint)D2D1_PROPERTY.D2D1_PROPERTY_CACHED,
|
||||
type: D2D1_PROPERTY_TYPE.D2D1_PROPERTY_TYPE_BOOL,
|
||||
data: (byte*)&cacheOutput,
|
||||
dataSize: sizeof(int)).Assert();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the <see cref="D2D1_PROPERTY.D2D1_PROPERTY_PRECISION"/> property for a given <see cref="ID2D1Effect"/> object.
|
||||
/// </summary>
|
||||
/// <param name="d2D1Effect">The input <see cref="ID2D1Effect"/> instance.</param>
|
||||
/// <returns>The <see cref="D2D1_PROPERTY.D2D1_PROPERTY_PRECISION"/> value for <paramref name="d2D1Effect"/>.</returns>
|
||||
public static D2D1_BUFFER_PRECISION GetPrecisionProperty(this ref ID2D1Effect d2D1Effect)
|
||||
{
|
||||
D2D1_BUFFER_PRECISION d2D1BufferPrecision;
|
||||
|
||||
d2D1Effect.GetValue(
|
||||
index: (uint)D2D1_PROPERTY.D2D1_PROPERTY_PRECISION,
|
||||
type: D2D1_PROPERTY_TYPE.D2D1_PROPERTY_TYPE_UNKNOWN,
|
||||
data: (byte*)&d2D1BufferPrecision,
|
||||
dataSize: sizeof(D2D1_BUFFER_PRECISION)).Assert();
|
||||
|
||||
return d2D1BufferPrecision;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the <see cref="D2D1_PROPERTY.D2D1_PROPERTY_PRECISION"/> property for a given <see cref="ID2D1Effect"/> object.
|
||||
/// </summary>
|
||||
/// <param name="d2D1Effect">The input <see cref="ID2D1Effect"/> instance.</param>
|
||||
/// <param name="value">The property value to set.</param>
|
||||
public static void SetPrecisionProperty(this ref ID2D1Effect d2D1Effect, D2D1_BUFFER_PRECISION value)
|
||||
{
|
||||
d2D1Effect.SetValue(
|
||||
index: (uint)D2D1_PROPERTY.D2D1_PROPERTY_PRECISION,
|
||||
type: D2D1_PROPERTY_TYPE.D2D1_PROPERTY_TYPE_UNKNOWN,
|
||||
data: (byte*)&value,
|
||||
dataSize: sizeof(D2D1_BUFFER_PRECISION)).Assert();
|
||||
}
|
||||
}
|
||||
@@ -1,80 +0,0 @@
|
||||
using System.Runtime.CompilerServices;
|
||||
using ComputeSharp.D2D1.Extensions;
|
||||
using ComputeSharp.Win32;
|
||||
|
||||
namespace ComputeSharp.D2D1.WinUI.Extensions;
|
||||
|
||||
/// <summary>
|
||||
/// Helper methods for working with the <see cref="IUnknown"/> type.
|
||||
/// </summary>
|
||||
internal static unsafe class IUnknownExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Checks whether two <see cref="IUnknown"/> values have the same underlying COM object.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of the first value to check.</typeparam>
|
||||
/// <typeparam name="U">The type of the second value to check.</typeparam>
|
||||
/// <param name="left">The first <see cref="IUnknown"/> object to compare.</param>
|
||||
/// <param name="right">The second <see cref="IUnknown"/> object to compare.</param>
|
||||
/// <returns>Whether <paramref name="left"/> and <paramref name="right"/> point to the same COM object.</returns>
|
||||
public static bool IsSameInstance<T, U>(this ref T left, U* right)
|
||||
where T : unmanaged // IUnknown
|
||||
where U : unmanaged // IUnknown
|
||||
{
|
||||
if (Unsafe.AreSame(ref left, ref *(T*)right))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if (Unsafe.IsNullRef(ref left) || right is null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
using ComPtr<IUnknown> leftUnknown = default;
|
||||
using ComPtr<IUnknown> rightUnknown = default;
|
||||
|
||||
((IUnknown*)Unsafe.AsPointer(ref left))->QueryInterface(Win32.Windows.__uuidof<IUnknown>(), (void**)leftUnknown.GetAddressOf()).Assert();
|
||||
((IUnknown*)right)->QueryInterface(Win32.Windows.__uuidof<IUnknown>(), (void**)rightUnknown.GetAddressOf()).Assert();
|
||||
|
||||
return leftUnknown.Get() == rightUnknown.Get();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Copies the source <see cref="IUnknown"/> object onto a target <see cref="ComPtr{T}"/> destination.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of object being copied.</typeparam>
|
||||
/// <param name="source">The source <see cref="IUnknown"/> object.</param>
|
||||
/// <param name="destination">The destination <see cref="ComPtr{T}"/> location.</param>
|
||||
/// <returns>The <see cref="HRESULT"/> for the operation.</returns>
|
||||
public static HRESULT CopyTo<T>(this ref T source, ref ComPtr<T> destination)
|
||||
where T : unmanaged // IUnknown
|
||||
{
|
||||
// Note: this explicitly needs a temporary copy to avoid issues in case source and destination were
|
||||
// pointers to the same object (which can be the case even if the two pointers are not identical).
|
||||
// To double check, a full check through an IUnknown* cast would be needed, but to avoid the extra
|
||||
// QueryInterface call on both objects, we can just always do one AddRef call which avoids issues in
|
||||
// all cases as well. The situation we need to avoid is that without a temporary copy, releasing the
|
||||
// target object might actually destroy the object, causing the following AddRef call to AV.
|
||||
using ComPtr<T> temporary = new((T*)Unsafe.AsPointer(ref source));
|
||||
|
||||
return temporary.CopyTo(ref destination);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Copies the source <see cref="IUnknown"/> object onto a target <typeparamref name="U"/> location.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of object being copied.</typeparam>
|
||||
/// <typeparam name="U">The type of destination object being copied.</typeparam>
|
||||
/// <param name="source">The source <see cref="IUnknown"/> object.</param>
|
||||
/// <param name="destination">The destination <typeparamref name="U"/> location.</param>
|
||||
/// <returns>The <see cref="HRESULT"/> for the operation.</returns>
|
||||
public static HRESULT CopyTo<T, U>(this ref T source, U** destination)
|
||||
where T : unmanaged // IUnknown
|
||||
where U : unmanaged, IComObject
|
||||
{
|
||||
using ComPtr<T> temporary = new((T*)Unsafe.AsPointer(ref source));
|
||||
|
||||
return temporary.CopyTo(destination);
|
||||
}
|
||||
}
|
||||
@@ -1,60 +0,0 @@
|
||||
using System;
|
||||
using ComputeSharp.Win32;
|
||||
using WinRT;
|
||||
using IInspectable = ComputeSharp.Win32.IInspectable;
|
||||
|
||||
namespace ComputeSharp.D2D1.WinUI.Helpers;
|
||||
|
||||
/// <summary>
|
||||
/// A helper type to handle marshalling of RCW instances.
|
||||
/// </summary>
|
||||
internal static unsafe class RcwMarshaller
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets or creates a managed object of a specified type for an input native object.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The interface type to retrieve an instance of.</typeparam>
|
||||
/// <param name="nativeObject">A pointer to the native object to get a managed wrapper for.</param>
|
||||
/// <returns>The resulting managed object wrapping <paramref name="nativeObject"/>.</returns>
|
||||
/// <remarks>This method should only be called with <typeparamref name="T"/> being a projected interface type.</remarks>
|
||||
public static T GetOrCreateManagedInterface<T>(IUnknown* nativeObject)
|
||||
where T : class
|
||||
{
|
||||
return MarshalInterface<T>.FromAbi((IntPtr)nativeObject);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieves the underlying native object for an input RCW.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of managed object to unwrap.</typeparam>
|
||||
/// <param name="managedObject">The input RCW instance to unwrap.</param>
|
||||
/// <param name="nativeObject">A pointer to the resulting native object to retrieve.</param>
|
||||
/// <remarks>This method should only be called with <typeparamref name="T"/> being a concrete projected type.</remarks>
|
||||
public static void GetNativeObject<T>(T managedObject, IInspectable** nativeObject)
|
||||
where T : class
|
||||
{
|
||||
*nativeObject = (IInspectable*)MarshalInspectable<T>.FromManaged(managedObject);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieves the underlying native object for an input RCW and casts it to the specified type.
|
||||
/// </summary>
|
||||
/// <typeparam name="TFrom">The type of managed object to unwrap.</typeparam>
|
||||
/// <typeparam name="TTo">The interface type to retrieve an instance of.</typeparam>
|
||||
/// <param name="managedObject">The input RCW instance to unwrap.</param>
|
||||
/// <param name="nativeObject">A pointer to the resulting native object to retrieve.</param>
|
||||
/// <returns>The <see cref="HRESULT"/> for the operation.</returns>
|
||||
/// <remarks>This method should only be called with <typeparamref name="TFrom"/> being a projected interface type.</remarks>
|
||||
public static HRESULT GetNativeInterface<TFrom, TTo>(TFrom managedObject, TTo** nativeObject)
|
||||
where TFrom : class
|
||||
where TTo : unmanaged, IComObject
|
||||
{
|
||||
using ComPtr<IUnknown> unknownObject = default;
|
||||
|
||||
// Here we only want to get an IUnknown* pointer for a given interface, and then we'll do
|
||||
// QueryInterface ourselves to get the target COM type. So MarshalInterface<TFrom> is fine.
|
||||
unknownObject.Attach((IUnknown*)MarshalInterface<TFrom>.FromManaged(managedObject));
|
||||
|
||||
return unknownObject.CopyTo(nativeObject);
|
||||
}
|
||||
}
|
||||
@@ -1,119 +0,0 @@
|
||||
using System;
|
||||
using ABI.Microsoft.Graphics.Canvas;
|
||||
using ComputeSharp.D2D1.Extensions;
|
||||
using ComputeSharp.Win32;
|
||||
using Windows.Graphics.Effects;
|
||||
using WinRT;
|
||||
|
||||
namespace ComputeSharp.D2D1.WinUI.Helpers;
|
||||
|
||||
using CanvasDevice = Microsoft.Graphics.Canvas.CanvasDevice;
|
||||
using ICanvasEffectFactoryNative = Microsoft.Graphics.Canvas.ICanvasEffectFactoryNative;
|
||||
using IInspectable = Win32.IInspectable;
|
||||
|
||||
/// <summary>
|
||||
/// A helper type to replicate Win2D's <c>ResourceManager</c> type.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This type provides an entry point to use the Win2D <see cref="ICanvasFactoryNative"/> activation factory.
|
||||
/// </remarks>
|
||||
internal static unsafe class ResourceManager
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets or creates an <see cref="IGraphicsEffectSource"/> instance for a native resource.
|
||||
/// </summary>
|
||||
/// <param name="device">The input canvas device (as a marshalled <see cref="CanvasDevice"/>).</param>
|
||||
/// <param name="resource">The input native resource to create a wrapper for.</param>
|
||||
/// <param name="dpi">The realization DPIs for <paramref name="resource"/></param>
|
||||
/// <returns>The resulting <see cref="IGraphicsEffectSource"/> wrapper instance.</returns>
|
||||
/// <exception cref="Exception">Thrown if the managed wrapper could not be retrieved.</exception>
|
||||
public static IGraphicsEffectSource GetOrCreate(ICanvasDevice* device, IUnknown* resource, float dpi)
|
||||
{
|
||||
using ComPtr<ICanvasFactoryNative> canvasFactoryNative = default;
|
||||
|
||||
GetActivationFactory(canvasFactoryNative.GetAddressOf());
|
||||
|
||||
using ComPtr<IInspectable> wrapperInspectable = default;
|
||||
|
||||
// Get or create a WinRT wrapper for the resource
|
||||
canvasFactoryNative.Get()->GetOrCreate(
|
||||
device: device,
|
||||
resource: resource,
|
||||
dpi: dpi,
|
||||
wrapper: wrapperInspectable.GetAddressOf()).Assert();
|
||||
|
||||
// Get the runtime-provided RCW for the resulting WinRT wrapper
|
||||
return RcwMarshaller.GetOrCreateManagedInterface<IGraphicsEffectSource>((IUnknown*)wrapperInspectable.Get());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Registers a managed wrapper for a given native resource.
|
||||
/// </summary>
|
||||
/// <param name="resource">The input native resource to register a wrapper for.</param>
|
||||
/// <param name="wrapper">The wrapper to register for <paramref name="resource"/>.</param>
|
||||
public static void RegisterWrapper<T>(IUnknown* resource, T wrapper)
|
||||
where T : class
|
||||
{
|
||||
using ComPtr<ICanvasFactoryNative> canvasFactoryNative = default;
|
||||
|
||||
GetActivationFactory(canvasFactoryNative.GetAddressOf());
|
||||
|
||||
using ComPtr<IInspectable> wrapperInspectable = default;
|
||||
|
||||
// Unwrap the input wrapper and get an IInspectable* object
|
||||
RcwMarshaller.GetNativeObject(wrapper, wrapperInspectable.GetAddressOf());
|
||||
|
||||
// Register this pair of native resource and inspectable wrapper
|
||||
canvasFactoryNative.Get()->RegisterWrapper(resource, wrapperInspectable.Get()).Assert();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Unregisters a native resource from Win2D's internal cache.
|
||||
/// </summary>
|
||||
/// <param name="resource">The input native resource to unregister a wrapper for.</param>
|
||||
public static void UnregisterWrapper(IUnknown* resource)
|
||||
{
|
||||
using ComPtr<ICanvasFactoryNative> canvasFactoryNative = default;
|
||||
|
||||
GetActivationFactory(canvasFactoryNative.GetAddressOf());
|
||||
|
||||
// Unregister the previously registered native resource
|
||||
canvasFactoryNative.Get()->UnregisterWrapper(resource).Assert();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Registers a factory of wrappers for custom effect with a given effect id.
|
||||
/// </summary>
|
||||
/// <param name="effectId">The id of the effects to register a factory for.</param>
|
||||
/// <param name="factory">The input factory to create wrappers of native effects.</param>
|
||||
public static void RegisterEffectFactory(Guid effectId, ICanvasEffectFactoryNative factory)
|
||||
{
|
||||
using ComPtr<ICanvasFactoryNative> canvasFactoryNative = default;
|
||||
|
||||
GetActivationFactory(canvasFactoryNative.GetAddressOf());
|
||||
|
||||
using ComPtr<ABI.Microsoft.Graphics.Canvas.ICanvasEffectFactoryNative> canvasEffectFactoryNative = default;
|
||||
|
||||
RcwMarshaller.GetNativeInterface(factory, canvasEffectFactoryNative.GetAddressOf()).Assert();
|
||||
|
||||
// Register the factory for the current effect from the activation factory
|
||||
canvasFactoryNative.Get()->RegisterEffectFactory(&effectId, canvasEffectFactoryNative.Get()).Assert();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the <see cref="ICanvasFactoryNative"/> activation factory.
|
||||
/// </summary>
|
||||
/// <param name="factoryNative">A pointer to the resulting activation factory.</param>
|
||||
private static void GetActivationFactory(ICanvasFactoryNative** factoryNative)
|
||||
{
|
||||
const string ActivatableClassId = "Microsoft.Graphics.Canvas.CanvasDevice";
|
||||
|
||||
// On WinUI 3, the types are not guaranteed to be registered for activation. Additionally, for
|
||||
// concistency with other types, we just use the built-in ActivationFactory type, which will
|
||||
// automatically handle fallback logic to resolve types to activate if they're not registered.
|
||||
// For instance, this will ensure the following call will work fine in unpackaged apps.
|
||||
using IObjectReference canvasDeviceActivationFactory = ActivationFactory.Get(ActivatableClassId, *ICanvasFactoryNative.IID);
|
||||
|
||||
*factoryNative = (ICanvasFactoryNative*)canvasDeviceActivationFactory.GetRef();
|
||||
}
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
namespace ComputeSharp.D2D1.WinUI;
|
||||
|
||||
/// <summary>
|
||||
/// A marker interface for an effect node that was registered from an <see cref="CanvasEffectGraph"/> value.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// This interface allows using <see cref="CanvasEffectGraph.GetNode"/> and <see cref="CanvasEffectGraph.SetOutputNode"/> in more scenarios,
|
||||
/// including where the concrete type of the image being used (eg. to set the output node) is not known (or needed) by the caller.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// This interface only implemented by <see cref="CanvasEffectNode{T}"/> and it's not meant to be implemented by external types.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
public interface ICanvasEffectNode;
|
||||
@@ -1,19 +0,0 @@
|
||||
using Microsoft.Graphics.Canvas;
|
||||
|
||||
namespace ComputeSharp.D2D1.WinUI;
|
||||
|
||||
/// <summary>
|
||||
/// A marker interface for a generic effect node that was registered from an <see cref="CanvasEffectGraph"/> value.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The covariant type of <see cref="ICanvasImage"/> associated with the current effect node.</typeparam>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// This interface allows using <see cref="CanvasEffectGraph.GetNode"/> and <see cref="CanvasEffectGraph.SetOutputNode"/> in more scenarios,
|
||||
/// including with ternary expressions returning multiple concrete <see cref="CanvasEffectNode{T}"/> instances with a common image type.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// This interface only implemented by <see cref="CanvasEffectNode{T}"/> and it's not meant to be implemented by external types.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
public interface ICanvasEffectNode<out T> : ICanvasEffectNode
|
||||
where T : ICanvasImage;
|
||||
@@ -1,210 +0,0 @@
|
||||
using System;
|
||||
using System.Buffers;
|
||||
using System.Runtime.InteropServices;
|
||||
using ABI.Microsoft.Graphics.Canvas;
|
||||
using ComputeSharp.D2D1.Extensions;
|
||||
using ComputeSharp.D2D1.WinUI.Helpers;
|
||||
using ComputeSharp.Interop;
|
||||
using ComputeSharp.Win32;
|
||||
using Windows.Foundation;
|
||||
using ICanvasEffect = Microsoft.Graphics.Canvas.Effects.ICanvasEffect;
|
||||
using ICanvasResourceCreatorWithDpi = Microsoft.Graphics.Canvas.ICanvasResourceCreatorWithDpi;
|
||||
|
||||
namespace ComputeSharp.D2D1.WinUI;
|
||||
|
||||
/// <inheritdoc/>
|
||||
unsafe partial class PixelShaderEffect<T>
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
public void InvalidateSourceRectangle(ICanvasResourceCreatorWithDpi resourceCreator, uint sourceIndex, Rect invalidRectangle)
|
||||
{
|
||||
using ReferenceTracker.Lease _0 = GetReferenceTracker().GetLease();
|
||||
|
||||
using ComPtr<ABI.Microsoft.Graphics.Canvas.ICanvasResourceCreatorWithDpi> resourceCreatorWithDpi = default;
|
||||
using ComPtr<ICanvasImageInterop> canvasImageInterop = default;
|
||||
|
||||
GetCanvasResourceCreatorAndCanvasImageInteropUnderlyingObjects(
|
||||
resourceCreator,
|
||||
resourceCreatorWithDpi.GetAddressOf(),
|
||||
canvasImageInterop.GetAddressOf());
|
||||
|
||||
Win2D.InvalidateSourceRectangleForICanvasImageInterop(
|
||||
resourceCreator: resourceCreatorWithDpi.Get(),
|
||||
image: canvasImageInterop.Get(),
|
||||
sourceIndex: sourceIndex,
|
||||
invalidRectangle: &invalidRectangle).Assert();
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public Rect[] GetInvalidRectangles(ICanvasResourceCreatorWithDpi resourceCreator)
|
||||
{
|
||||
using ReferenceTracker.Lease _0 = GetReferenceTracker().GetLease();
|
||||
|
||||
using ComPtr<ABI.Microsoft.Graphics.Canvas.ICanvasResourceCreatorWithDpi> resourceCreatorWithDpi = default;
|
||||
using ComPtr<ICanvasImageInterop> canvasImageInterop = default;
|
||||
|
||||
GetCanvasResourceCreatorAndCanvasImageInteropUnderlyingObjects(
|
||||
resourceCreator,
|
||||
resourceCreatorWithDpi.GetAddressOf(),
|
||||
canvasImageInterop.GetAddressOf());
|
||||
|
||||
uint valueCount;
|
||||
Rect* valueElements;
|
||||
|
||||
Win2D.GetInvalidRectanglesForICanvasImageInterop(
|
||||
resourceCreator: resourceCreatorWithDpi.Get(),
|
||||
image: canvasImageInterop.Get(),
|
||||
valueCount: &valueCount,
|
||||
valueElements: &valueElements).Assert();
|
||||
|
||||
try
|
||||
{
|
||||
// Copy the data back from the COM array that Win2D created and returned.
|
||||
// If no items were returned, this will automatically return an empty array.
|
||||
return new Span<Rect>(valueElements, checked((int)valueCount)).ToArray();
|
||||
}
|
||||
finally
|
||||
{
|
||||
// Make sure to release the callee-allocated COM array
|
||||
Marshal.FreeCoTaskMem((IntPtr)valueElements);
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public Rect GetRequiredSourceRectangle(
|
||||
ICanvasResourceCreatorWithDpi resourceCreator,
|
||||
Rect outputRectangle,
|
||||
ICanvasEffect sourceEffect,
|
||||
uint sourceIndex,
|
||||
Rect sourceBounds)
|
||||
{
|
||||
using ReferenceTracker.Lease _0 = GetReferenceTracker().GetLease();
|
||||
|
||||
using ComPtr<ABI.Microsoft.Graphics.Canvas.ICanvasResourceCreatorWithDpi> resourceCreatorWithDpi = default;
|
||||
using ComPtr<ICanvasImageInterop> canvasImageInterop = default;
|
||||
|
||||
GetCanvasResourceCreatorAndCanvasImageInteropUnderlyingObjects(
|
||||
resourceCreator,
|
||||
resourceCreatorWithDpi.GetAddressOf(),
|
||||
canvasImageInterop.GetAddressOf());
|
||||
|
||||
Rect result;
|
||||
|
||||
using ComPtr<ABI.Microsoft.Graphics.Canvas.Effects.ICanvasEffect> canvasEffectAbi = default;
|
||||
|
||||
// Get the ABI.Microsoft.Graphics.Canvas.Effects.ICanvasEffect object from the input interface
|
||||
RcwMarshaller.GetNativeInterface(sourceEffect, canvasEffectAbi.GetAddressOf()).Assert();
|
||||
|
||||
Win2D.GetRequiredSourceRectanglesForICanvasImageInterop(
|
||||
resourceCreator: resourceCreatorWithDpi.Get(),
|
||||
image: canvasImageInterop.Get(),
|
||||
outputRectangle: &outputRectangle,
|
||||
sourceEffectCount: 1,
|
||||
sourceEffects: canvasEffectAbi.GetAddressOf(),
|
||||
sourceIndexCount: 1,
|
||||
sourceIndices: &sourceIndex,
|
||||
sourceBoundsCount: 1,
|
||||
sourceBounds: &sourceBounds,
|
||||
valueCount: 1,
|
||||
valueElements: &result).Assert();
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public Rect[] GetRequiredSourceRectangles(
|
||||
ICanvasResourceCreatorWithDpi resourceCreator,
|
||||
Rect outputRectangle,
|
||||
ICanvasEffect[] sourceEffects,
|
||||
uint[] sourceIndices,
|
||||
Rect[] sourceBounds)
|
||||
{
|
||||
using ReferenceTracker.Lease _0 = GetReferenceTracker().GetLease();
|
||||
|
||||
using ComPtr<ABI.Microsoft.Graphics.Canvas.ICanvasResourceCreatorWithDpi> resourceCreatorWithDpi = default;
|
||||
using ComPtr<ICanvasImageInterop> canvasImageInterop = default;
|
||||
|
||||
GetCanvasResourceCreatorAndCanvasImageInteropUnderlyingObjects(
|
||||
resourceCreator,
|
||||
resourceCreatorWithDpi.GetAddressOf(),
|
||||
canvasImageInterop.GetAddressOf());
|
||||
|
||||
// The array optionally rented from the pool is of IntPtr types to reduce the number of generic
|
||||
// instantiations. This increases the chances of the pooled arrays being shared with other code.
|
||||
IntPtr[]? canvasEffectsAbiArray = null;
|
||||
|
||||
// Create the span and optional array of ABI.Microsoft.Graphics.Canvas.Effects.ICanvasEffect objects
|
||||
Span<ComPtr<ABI.Microsoft.Graphics.Canvas.Effects.ICanvasEffect>> canvasEffects = sourceEffects.Length <= 16
|
||||
? stackalloc ComPtr<ABI.Microsoft.Graphics.Canvas.Effects.ICanvasEffect>[16]
|
||||
: MemoryMarshal.Cast<IntPtr, ComPtr<ABI.Microsoft.Graphics.Canvas.Effects.ICanvasEffect>>(
|
||||
canvasEffectsAbiArray = ArrayPool<IntPtr>.Shared.Rent(sourceEffects.Length));
|
||||
|
||||
// Clear the span to ensure no false positives are accidentally disposed
|
||||
canvasEffects.Clear();
|
||||
|
||||
try
|
||||
{
|
||||
// Get the underlying ABI.Microsoft.Graphics.Canvas.Effects.ICanvasEffect object for each input effect
|
||||
for (int i = 0; i < sourceEffects.Length; i++)
|
||||
{
|
||||
RcwMarshaller.GetNativeInterface(sourceEffects[i], canvasEffects[i].GetAddressOf()).Assert();
|
||||
}
|
||||
|
||||
Rect[] result = new Rect[sourceEffects.Length];
|
||||
|
||||
fixed (ComPtr<ABI.Microsoft.Graphics.Canvas.Effects.ICanvasEffect>* pCanvasEffects = canvasEffects)
|
||||
fixed (uint* pSourceIndices = sourceIndices)
|
||||
fixed (Rect* pSourceBounds = sourceBounds)
|
||||
fixed (Rect* pResult = result)
|
||||
{
|
||||
Win2D.GetRequiredSourceRectanglesForICanvasImageInterop(
|
||||
resourceCreator: resourceCreatorWithDpi.Get(),
|
||||
image: canvasImageInterop.Get(),
|
||||
outputRectangle: &outputRectangle,
|
||||
sourceEffectCount: (uint)sourceEffects.Length,
|
||||
sourceEffects: (ABI.Microsoft.Graphics.Canvas.Effects.ICanvasEffect**)pCanvasEffects,
|
||||
sourceIndexCount: (uint)sourceIndices.Length,
|
||||
sourceIndices: pSourceIndices,
|
||||
sourceBoundsCount: (uint)sourceBounds.Length,
|
||||
sourceBounds: pSourceBounds,
|
||||
valueCount: (uint)sourceEffects.Length,
|
||||
valueElements: pResult).Assert();
|
||||
}
|
||||
|
||||
// Only return the array to the pool if no exceptions have been thrown
|
||||
if (canvasEffectsAbiArray is not null)
|
||||
{
|
||||
ArrayPool<IntPtr>.Shared.Return(canvasEffectsAbiArray);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
finally
|
||||
{
|
||||
// Release all canvas effects that have been retrieved up until the exception
|
||||
foreach (ref ComPtr<ABI.Microsoft.Graphics.Canvas.Effects.ICanvasEffect> canvasEffect in canvasEffects)
|
||||
{
|
||||
canvasEffect.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the underlying <see cref="ABI.Microsoft.Graphics.Canvas.ICanvasResourceCreatorWithDpi"/> and <see cref="ICanvasImageInterop"/>
|
||||
/// objects from an input <see cref="ICanvasResourceCreatorWithDpi"/> object and the current <see cref="PixelShaderEffect{T}"/> instance.
|
||||
/// </summary>
|
||||
/// <param name="resourceCreator">The input <see cref="ICanvasResourceCreatorWithDpi"/> object.</param>
|
||||
/// <param name="resourceCreatorWithDpi">The resulting <see cref="ABI.Microsoft.Graphics.Canvas.ICanvasResourceCreatorWithDpi"/> object for <paramref name="resourceCreator"/>.</param>
|
||||
/// <param name="canvasImageInterop">The resulting <see cref="ICanvasImageInterop"/> object for the current <see cref="PixelShaderEffect{T}"/> instance.</param>
|
||||
private void GetCanvasResourceCreatorAndCanvasImageInteropUnderlyingObjects(
|
||||
ICanvasResourceCreatorWithDpi resourceCreator,
|
||||
ABI.Microsoft.Graphics.Canvas.ICanvasResourceCreatorWithDpi** resourceCreatorWithDpi,
|
||||
ICanvasImageInterop** canvasImageInterop)
|
||||
{
|
||||
// Get the ABI.Microsoft.Graphics.Canvas.ICanvasResourceCreatorWithDpi object from the input interface
|
||||
RcwMarshaller.GetNativeInterface(resourceCreator, resourceCreatorWithDpi).Assert();
|
||||
|
||||
// Get the ICanvasImageInterop object from the current instance
|
||||
RcwMarshaller.GetNativeInterface(this, canvasImageInterop).Assert();
|
||||
}
|
||||
}
|
||||
@@ -1,143 +0,0 @@
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
using ABI.Microsoft.Graphics.Canvas;
|
||||
using ComputeSharp.D2D1.Extensions;
|
||||
using ComputeSharp.D2D1.WinUI.Extensions;
|
||||
using ComputeSharp.D2D1.WinUI.Helpers;
|
||||
using ComputeSharp.Win32;
|
||||
using ICanvasEffectFactoryNative = Microsoft.Graphics.Canvas.ICanvasEffectFactoryNative;
|
||||
|
||||
namespace ComputeSharp.D2D1.WinUI;
|
||||
|
||||
/// <inheritdoc/>
|
||||
partial class PixelShaderEffect<T>
|
||||
{
|
||||
/// <summary>
|
||||
/// A manager type to handle the effect factory registration logic for <see cref="ICanvasEffectFactoryNative"/>.
|
||||
/// </summary>
|
||||
private sealed class EffectFactoryManager
|
||||
{
|
||||
/// <summary>
|
||||
/// Lock object used to synchronize the registration logic.
|
||||
/// </summary>
|
||||
private readonly object lockObject = new();
|
||||
|
||||
/// <summary>
|
||||
/// Indicates whether an effect factory for <see cref="PixelShaderEffect{T}"/> has been registered.
|
||||
/// </summary>
|
||||
private volatile bool isEffectFactoryRegistered;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="EffectFactoryManager"/> instance.
|
||||
/// </summary>
|
||||
private EffectFactoryManager()
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the shared <see cref="EffectFactoryManager"/> instance.
|
||||
/// </summary>
|
||||
public static EffectFactoryManager Instance { get; } = new();
|
||||
|
||||
/// <summary>
|
||||
/// Ensures an effect factory is registered in Win2D for the current effect type, or registers one otherwise.
|
||||
/// </summary>
|
||||
public void EnsureEffectFactoryIsRegistered()
|
||||
{
|
||||
if (this.isEffectFactoryRegistered)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
EnsureEffectFactoryIsRegisteredWithLock();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Same as <see cref="EnsureEffectFactoryIsRegistered"/>, but with a slower path using a lock.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
private void EnsureEffectFactoryIsRegisteredWithLock()
|
||||
{
|
||||
lock (this.lockObject)
|
||||
{
|
||||
if (!this.isEffectFactoryRegistered)
|
||||
{
|
||||
ResourceManager.RegisterEffectFactory(
|
||||
effectId: T.EffectId,
|
||||
factory: new EffectFactory());
|
||||
|
||||
this.isEffectFactoryRegistered = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A managed implementation of <see cref="ICanvasEffectFactoryNative"/> for <see cref="PixelShaderEffect{T}"/>.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This type is partial even though there are no other explicit partial type declarations in the project, so the CsWinRT
|
||||
/// generator can generate one using <see cref="WinRT.WinRTExposedTypeAttribute"/> and the precomputed vtable entries.
|
||||
/// </remarks>
|
||||
internal sealed unsafe partial class EffectFactory : ICanvasEffectFactoryNative
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
int ICanvasEffectFactoryNative.CreateWrapper(ICanvasDevice* device, ID2D1Effect* resource, float dpi, IInspectable** wrapper)
|
||||
{
|
||||
PixelShaderEffect<T> @this;
|
||||
|
||||
using (ComPtr<ID2D1Device1> d2D1Device1 = default)
|
||||
{
|
||||
// Get the underlying ID2D1Device1 instance for the input device
|
||||
device->GetD2DDevice(d2D1Device1.GetAddressOf()).Assert();
|
||||
|
||||
// Validate that the input underlying D2D device and resource share the same D2D factory
|
||||
using (ComPtr<ID2D1Image> d2D1ImageResource = default)
|
||||
using (ComPtr<ID2D1Factory> d2D1FactoryFromDevice = default)
|
||||
using (ComPtr<ID2D1Factory> d2D1FactoryFromResource = default)
|
||||
{
|
||||
resource->CopyTo(d2D1ImageResource.GetAddressOf()).Assert();
|
||||
|
||||
d2D1Device1.Get()->GetFactory(d2D1FactoryFromDevice.GetAddressOf());
|
||||
d2D1ImageResource.Get()->GetFactory(d2D1FactoryFromResource.GetAddressOf());
|
||||
|
||||
// Shared logic and comments from Win2D, as this validation path is the same as that of CanvasEffect.
|
||||
// There is a small chance of constructing a PixelShaderEffect<T> object that is in an invalid state
|
||||
// when using this method to create a managed wrapper indirectly from the interop APIs exposed in the
|
||||
// public headers. In general, this would happen if someone did manual interop to get a managed wrapper
|
||||
// for an orphaned effect previously retrieved from an existing PixelShaderEffect<T> instance that has
|
||||
// since changed. For more info and a full example, see https://github.com/microsoft/Win2D/issues/913.
|
||||
// Here we're validating the D2D factories from the wrapped effect and the device to try to minimize
|
||||
// the chances of this happening. We can't be 100% sure since there is no way with the existing D2D
|
||||
// APIs to fully determine whether two resources are "compatible", short of just trying to draw with
|
||||
// both of them to see if that fails. But, comparing factories is at least a nice additional check
|
||||
// to perform, and due to the fact that Win2D creates a new D2D factory per CanvasDevice, this should
|
||||
// already cover most scenarios where this might potentially happen, so that's good enough.
|
||||
if (!d2D1FactoryFromDevice.Get()->IsSameInstance(d2D1FactoryFromResource.Get()))
|
||||
{
|
||||
*wrapper = null;
|
||||
|
||||
return D2DERR.D2DERR_WRONG_FACTORY;
|
||||
}
|
||||
}
|
||||
|
||||
// Only initialize the effect once we're past the validation
|
||||
@this = new PixelShaderEffect<T>();
|
||||
|
||||
// Initialize the effect state
|
||||
device->CopyTo(ref @this.canvasDevice).Assert();
|
||||
d2D1Device1.CopyTo(ref @this.d2D1RealizationDevice).Assert();
|
||||
resource->CopyTo(ref @this.d2D1Effect).Assert();
|
||||
}
|
||||
|
||||
// Register the new wrapper for the input resource. This has to be done manually in this
|
||||
// case, as the effect is not being realized through the usual path from GetD2DImage.
|
||||
ResourceManager.RegisterWrapper((IUnknown*)resource, @this);
|
||||
|
||||
// Unwrap the PixelShaderEffect<T> and return its CCW to the caller
|
||||
RcwMarshaller.GetNativeObject(@this, wrapper);
|
||||
|
||||
return S.S_OK;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,53 +0,0 @@
|
||||
using System.Numerics;
|
||||
using ABI.Microsoft.Graphics.Canvas;
|
||||
using ComputeSharp.D2D1.Extensions;
|
||||
using ComputeSharp.D2D1.WinUI.Helpers;
|
||||
using ComputeSharp.Interop;
|
||||
using ComputeSharp.Win32;
|
||||
using Windows.Foundation;
|
||||
using ICanvasResourceCreator = Microsoft.Graphics.Canvas.ICanvasResourceCreator;
|
||||
|
||||
namespace ComputeSharp.D2D1.WinUI;
|
||||
|
||||
/// <inheritdoc/>
|
||||
unsafe partial class PixelShaderEffect<T>
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
public Rect GetBounds(ICanvasResourceCreator resourceCreator)
|
||||
{
|
||||
return GetBounds(resourceCreator, null);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public Rect GetBounds(ICanvasResourceCreator resourceCreator, Matrix3x2 transform)
|
||||
{
|
||||
return GetBounds(resourceCreator, &transform);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Microsoft.Graphics.Canvas.ICanvasImage.GetBounds(ICanvasResourceCreator, Matrix3x2)"/>
|
||||
private Rect GetBounds(ICanvasResourceCreator resourceCreator, Matrix3x2* transform)
|
||||
{
|
||||
using ReferenceTracker.Lease _0 = GetReferenceTracker().GetLease();
|
||||
|
||||
using ComPtr<ABI.Microsoft.Graphics.Canvas.ICanvasResourceCreator> resourceCreatorAbi = default;
|
||||
|
||||
// Get the ABI.Microsoft.Graphics.Canvas.ICanvasResourceCreator object from the input interface
|
||||
RcwMarshaller.GetNativeInterface(resourceCreator, resourceCreatorAbi.GetAddressOf()).Assert();
|
||||
|
||||
using ComPtr<ICanvasImageInterop> canvasImageInterop = default;
|
||||
|
||||
// Get the ICanvasImageInterop object from the current instance
|
||||
RcwMarshaller.GetNativeInterface(this, canvasImageInterop.GetAddressOf()).Assert();
|
||||
|
||||
Rect bounds;
|
||||
|
||||
// Forward the actual logic to Win2D to compute the image bounds (it needs the internal context)
|
||||
Win2D.GetBoundsForICanvasImageInterop(
|
||||
resourceCreator: resourceCreatorAbi.Get(),
|
||||
image: canvasImageInterop.Get(),
|
||||
transform: transform,
|
||||
rect: &bounds).Assert();
|
||||
|
||||
return bounds;
|
||||
}
|
||||
}
|
||||
@@ -1,408 +0,0 @@
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Threading;
|
||||
using ABI.Microsoft.Graphics.Canvas;
|
||||
using ComputeSharp.D2D1.Extensions;
|
||||
using ComputeSharp.D2D1.Interop;
|
||||
using ComputeSharp.D2D1.WinUI.Extensions;
|
||||
using ComputeSharp.D2D1.WinUI.Helpers;
|
||||
using ComputeSharp.Interop;
|
||||
using ComputeSharp.Win32;
|
||||
using Windows.Graphics.Effects;
|
||||
using static ABI.Microsoft.Graphics.Canvas.WIN2D_GET_D2D_IMAGE_FLAGS;
|
||||
using static ABI.Microsoft.Graphics.Canvas.WIN2D_GET_DEVICE_ASSOCIATION_TYPE;
|
||||
using ICanvasImageInterop = Microsoft.Graphics.Canvas.ICanvasImageInterop;
|
||||
|
||||
namespace ComputeSharp.D2D1.WinUI;
|
||||
|
||||
/// <inheritdoc/>
|
||||
unsafe partial class PixelShaderEffect<T>
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
int ICanvasImageInterop.GetDevice(ICanvasDevice** device, WIN2D_GET_DEVICE_ASSOCIATION_TYPE* type)
|
||||
{
|
||||
// Validate all input parameters
|
||||
if (device is null || type is null)
|
||||
{
|
||||
return E.E_POINTER;
|
||||
}
|
||||
|
||||
// Set parameters to default values
|
||||
*device = null;
|
||||
*type = WIN2D_GET_DEVICE_ASSOCIATION_TYPE_UNSPECIFIED;
|
||||
|
||||
using ReferenceTracker.Lease _0 = GetReferenceTracker().TryGetLease(out bool leaseTaken);
|
||||
|
||||
// Check for disposal
|
||||
if (!leaseTaken)
|
||||
{
|
||||
return RO.RO_E_CLOSED;
|
||||
}
|
||||
|
||||
// Ensure we can take a lock without blocking. We can't access the realization device with no
|
||||
// lock, as that could lead to memory leaks or corruption if GetD2DImage was to manipulate that
|
||||
// field concurrently. We also cannot lock directly, because that would deadlock if someone was
|
||||
// to (incorrectly) try to use the same effect from two different threads. To protect against
|
||||
// that, we try to lock, and just return E_NOT_VALID_STATE if another thread holds the lock. If
|
||||
// this call had GetD2DImage in the stack, that would work fine because locks are recursive.
|
||||
if (!Monitor.TryEnter(this.lockObject))
|
||||
{
|
||||
return E.E_NOT_VALID_STATE;
|
||||
}
|
||||
|
||||
HRESULT hresult;
|
||||
|
||||
try
|
||||
{
|
||||
// Copy the device over to the target
|
||||
hresult = this.canvasDevice.CopyTo(device);
|
||||
}
|
||||
finally
|
||||
{
|
||||
Monitor.Exit(this.lockObject);
|
||||
}
|
||||
|
||||
// Set the association type if the copy was successful
|
||||
if (Win32.Windows.SUCCEEDED(hresult))
|
||||
{
|
||||
*type = WIN2D_GET_DEVICE_ASSOCIATION_TYPE_REALIZATION_DEVICE;
|
||||
}
|
||||
|
||||
return hresult;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
int ICanvasImageInterop.GetD2DImage(
|
||||
ICanvasDevice* device,
|
||||
ID2D1DeviceContext* deviceContext,
|
||||
WIN2D_GET_D2D_IMAGE_FLAGS flags,
|
||||
float targetDpi,
|
||||
float* realizeDpi,
|
||||
ID2D1Image** ppImage)
|
||||
{
|
||||
// The device and resulting image pointers cannot be null
|
||||
if (device is null || ppImage is null)
|
||||
{
|
||||
return E.E_POINTER;
|
||||
}
|
||||
|
||||
// Set the resulting image to null
|
||||
*ppImage = null;
|
||||
|
||||
using ReferenceTracker.Lease _0 = GetReferenceTracker().TryGetLease(out bool leaseTaken);
|
||||
|
||||
// Check for disposal
|
||||
if (!leaseTaken)
|
||||
{
|
||||
return RO.RO_E_CLOSED;
|
||||
}
|
||||
|
||||
// Check for graph cycles
|
||||
if (Interlocked.CompareExchange(ref this.isInsideGetD2DImage, 1, 0) == 1)
|
||||
{
|
||||
return D2DERR.D2DERR_CYCLIC_GRAPH;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
// Lock after checking for graph cycles (other public APIs also lock on this object)
|
||||
lock (this.lockObject)
|
||||
{
|
||||
// Process the ReadDpiFromDeviceContext flag (same logic as CanvasEffect::GetD2DImage)
|
||||
if ((flags & WIN2D_GET_D2D_IMAGE_FLAGS_READ_DPI_FROM_DEVICE_CONTEXT) != WIN2D_GET_D2D_IMAGE_FLAGS_NONE)
|
||||
{
|
||||
// If ReadDpiFromDeviceContext is used, the context cannot be null
|
||||
if (deviceContext is null)
|
||||
{
|
||||
return E.E_POINTER;
|
||||
}
|
||||
|
||||
if (deviceContext->HasCommandListTarget())
|
||||
{
|
||||
// Command lists are DPI dependent, so DPI compensation effects are always needed
|
||||
flags |= WIN2D_GET_D2D_IMAGE_FLAGS_ALWAYS_INSERT_DPI_COMPENSATION;
|
||||
}
|
||||
else
|
||||
{
|
||||
// If the effect is not being drawn onto a command list, the DPIs can be read from the device context
|
||||
targetDpi = deviceContext->GetDpi();
|
||||
}
|
||||
|
||||
// ReadDpiFromDeviceContext has been processed, so it can be removed now
|
||||
flags &= ~WIN2D_GET_D2D_IMAGE_FLAGS_READ_DPI_FROM_DEVICE_CONTEXT;
|
||||
}
|
||||
|
||||
using ComPtr<ID2D1Device1> d2D1Device1 = default;
|
||||
|
||||
// Get the underlying ID2D1Device1 instance in use
|
||||
device->GetD2DDevice(d2D1Device1.GetAddressOf()).Assert();
|
||||
|
||||
// Check that the input D2D device is the same instance as the current realization device, if any.
|
||||
// If not and the effect has already been realized, unrealize it and store the new device to use.
|
||||
if (!d2D1Device1.Get()->IsSameInstance(this.d2D1RealizationDevice.Get()))
|
||||
{
|
||||
Unrealize();
|
||||
|
||||
// Store the input realization device and canvas device for future uses
|
||||
d2D1Device1.CopyTo(ref this.d2D1RealizationDevice).Assert();
|
||||
device->CopyTo(ref this.canvasDevice).Assert();
|
||||
}
|
||||
|
||||
// Create the effect, if it doesn't exist already
|
||||
if (this.d2D1Effect.Get() is null)
|
||||
{
|
||||
if (!Realize(flags, targetDpi, deviceContext))
|
||||
{
|
||||
return E.E_FAIL;
|
||||
}
|
||||
}
|
||||
else if (!flags.HasFlag(WIN2D_GET_D2D_IMAGE_FLAGS_MINIMAL_REALIZATION))
|
||||
{
|
||||
// Recurse through the effect graph and ensure all nodes are initialized
|
||||
RefreshInputs(flags, targetDpi, deviceContext);
|
||||
}
|
||||
|
||||
// The realized DPIs are always 0 for pixel shader effects
|
||||
if (realizeDpi is not null)
|
||||
{
|
||||
*realizeDpi = 0;
|
||||
}
|
||||
|
||||
this.d2D1Effect.CopyTo(ppImage).Assert();
|
||||
|
||||
return S.S_OK;
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
// Ensure that reset this flag so that this method can be called again
|
||||
this.isInsideGetD2DImage = 0;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Unrealizes the currently realized <see cref="ID2D1Effect"/> object.
|
||||
/// </summary>
|
||||
/// <param name="source">The value to set for <see cref="Sources"/>.</param>
|
||||
/// <param name="index">The index of the <see cref="IGraphicsEffectSource"/> source to get or set.</param>
|
||||
private void Unrealize(IGraphicsEffectSource source, int index)
|
||||
{
|
||||
// Unrealize the effect and indicate to the logic to skip the source at the current index.
|
||||
// That is, there is no need to marshal it back to the cache, as we're about to do that here.
|
||||
Unrealize(index);
|
||||
|
||||
// Once the effect is unrealized, also update the cached source managed wrapper
|
||||
Sources.Storage[index].SetWrapper(source);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Unrealizes the currently realized <see cref="ID2D1Effect"/> object.
|
||||
/// </summary>
|
||||
private void Unrealize(int? sourceIndexToSkip = null)
|
||||
{
|
||||
// If there is no ID2D1Effect object, there is nothing left to do
|
||||
if (this.d2D1Effect.Get() is null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// Cache the last update values for the constant buffer, cache output, buffer precision properties, etc.
|
||||
// Note: the resource texture managers are deliberately not read back, as the only supported way to set
|
||||
// those is directly from the current effect instance. As such, they'd all either be the same instances
|
||||
// that are already stored locally, or trying to retrieve them would just throw an exception.
|
||||
this.constantBuffer = this.d2D1Effect.Get()->GetConstantBuffer<T>();
|
||||
this.transformMapper = this.d2D1Effect.Get()->GetTransformMapper<T>();
|
||||
this.cacheOutput = this.d2D1Effect.Get()->GetCachedProperty();
|
||||
this.d2D1BufferPrecision = this.d2D1Effect.Get()->GetPrecisionProperty();
|
||||
|
||||
// Loop over all effect inputs and update the cache back as well
|
||||
for (int i = 0; i < Sources.Count; i++)
|
||||
{
|
||||
// If this index has been explicitly requested to be skipped, just dispose the source reference.
|
||||
// This is the case when the effect is being unrealized, and callers will set this value on their own.
|
||||
if (i == sourceIndexToSkip)
|
||||
{
|
||||
Sources.Storage[i].Dispose();
|
||||
}
|
||||
else
|
||||
{
|
||||
// Otherwise, just get the input, which will automatically update the cached values. The
|
||||
// returned IGraphicsEffectSource managed wrapper is not used here and is safe to ignore.
|
||||
_ = GetD2DInput(i);
|
||||
}
|
||||
}
|
||||
|
||||
// Unregister the wrapper for the effect. In case someone is still holding a reference
|
||||
// to it, the effect is now "orphaned" and without a registered inspectable wrapper.
|
||||
ResourceManager.UnregisterWrapper((IUnknown*)this.d2D1Effect.Get());
|
||||
|
||||
// Finally release the effect as well
|
||||
this.d2D1Effect.Dispose();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Realizes the underlying <see cref="ID2D1Effect"/> for this object.
|
||||
/// </summary>
|
||||
/// <param name="flags">The current flags in use.</param>
|
||||
/// <param name="targetDpi">The target DPI in use.</param>
|
||||
/// <param name="deviceContext">The <see cref="ID2D1DeviceContext"/> instance in use.</param>
|
||||
/// <returns>Whether the effect was realized correctly.</returns>
|
||||
private bool Realize(WIN2D_GET_D2D_IMAGE_FLAGS flags, float targetDpi, ID2D1DeviceContext* deviceContext)
|
||||
{
|
||||
fixed (ID2D1Effect** d2D1Effect = this.d2D1Effect)
|
||||
{
|
||||
Guid effectId = T.EffectId;
|
||||
|
||||
using ComPtr<ID2D1DeviceContext> d2D1DeviceContextEffective = default;
|
||||
using ComPtr<ID2D1DeviceContextLease> d2D1DeviceContextLease = default;
|
||||
|
||||
// We need to realize the current effect (ComputeSharp.D2D1's ID2D1Effect), which needs a device context
|
||||
this.canvasDevice.Get()->GetEffectiveD2DDeviceContextWithOptionalLease(
|
||||
d2D1DeviceContext: deviceContext,
|
||||
d2D1DeviceContextEffective: d2D1DeviceContextEffective.GetAddressOf(),
|
||||
d2D1DeviceContextLease: d2D1DeviceContextLease.GetAddressOf());
|
||||
|
||||
// Try to create an instance of the effect in use and store it in the current object
|
||||
HRESULT hresult = d2D1DeviceContextEffective.Get()->CreateEffect(effectId: &effectId, effect: d2D1Effect);
|
||||
|
||||
// Check if creation failed due to the effect not being registered. In that case, register
|
||||
// it and then try again. This is much faster than check whether the effect is registered
|
||||
// manually every time, by enumerating all registered effects for the target device context.
|
||||
// An unregistered effect can also sometimes return E_NOTFOUND, although it's not documented.
|
||||
if (hresult == D2DERR.D2DERR_EFFECT_IS_NOT_REGISTERED ||
|
||||
hresult == E.E_NOTFOUND)
|
||||
{
|
||||
using ComPtr<ID2D1Factory1> d2D1Factory1 = default;
|
||||
|
||||
// Get the ID2D1Factory1 object to register the effect (required by D2D1PixelShaderEffect)
|
||||
d2D1DeviceContextEffective.Get()->GetFactory1(d2D1Factory1.GetAddressOf()).Assert();
|
||||
|
||||
// Register the effect with the factory (pass the same D2D1 draw transform mapper factory that was used before)
|
||||
D2D1PixelShaderEffect.RegisterForD2D1Factory1<T>(d2D1Factory1.Get(), out _);
|
||||
|
||||
// Try to create the effect again
|
||||
hresult = d2D1DeviceContextEffective.Get()->CreateEffect(effectId: &effectId, effect: d2D1Effect);
|
||||
}
|
||||
|
||||
// Rethrow any other errors
|
||||
hresult.Assert();
|
||||
}
|
||||
|
||||
// Set the constant buffer for the effect
|
||||
D2D1PixelShaderEffect.SetConstantBufferForD2D1Effect(this.d2D1Effect.Get(), in this.constantBuffer);
|
||||
|
||||
// If there is a transform mapper, set it in the effect
|
||||
if (this.transformMapper is not null)
|
||||
{
|
||||
D2D1PixelShaderEffect.SetTransformMapperForD2D1Effect(this.d2D1Effect.Get(), this.transformMapper);
|
||||
}
|
||||
|
||||
// If cache input has been set, forward that to the new effect
|
||||
if (this.cacheOutput)
|
||||
{
|
||||
this.d2D1Effect.Get()->SetCachedProperty(true);
|
||||
}
|
||||
|
||||
// If the buffer precision isn't unknown, forward that as well
|
||||
if (this.d2D1BufferPrecision != D2D1_BUFFER_PRECISION.D2D1_BUFFER_PRECISION_UNKNOWN)
|
||||
{
|
||||
this.d2D1Effect.Get()->SetPrecisionProperty(this.d2D1BufferPrecision);
|
||||
}
|
||||
|
||||
// Forward all available sources (the effect is being created now, so no need to validate the underlying resources)
|
||||
for (int i = 0; i < Sources.Count; i++)
|
||||
{
|
||||
if (!SetD2DInput(
|
||||
index: i,
|
||||
value: Sources.Storage[i].GetWrapper(),
|
||||
flags: flags,
|
||||
targetDpi: targetDpi,
|
||||
d2D1DeviceContext: deviceContext))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Set all available resource texture managers (only set those that are not null, as they're all null by default anyway).
|
||||
// The loop only goes over the indices of valid slots for resource textures for the current effect, and ignores the others.
|
||||
foreach (int index in ResourceTextureManagerCollection.Indices)
|
||||
{
|
||||
if (ResourceTextureManagers.Storage[index] is D2D1ResourceTextureManager resourceTextureManager)
|
||||
{
|
||||
D2D1PixelShaderEffect.SetResourceTextureManagerForD2D1Effect(this.d2D1Effect.Get(), resourceTextureManager, index);
|
||||
}
|
||||
}
|
||||
|
||||
// Register the new realized effect
|
||||
ResourceManager.RegisterWrapper((IUnknown*)this.d2D1Effect.Get(), this);
|
||||
|
||||
// Register the effect factory, if necessary
|
||||
EffectFactoryManager.Instance.EnsureEffectFactoryIsRegistered();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Updates the draw state for the currently realized effect.
|
||||
/// </summary>
|
||||
/// <param name="flags">The current flags in use.</param>
|
||||
/// <param name="targetDpi">The target DPI in use.</param>
|
||||
/// <param name="deviceContext">The <see cref="ID2D1DeviceContext"/> instance in use.</param>
|
||||
private void RefreshInputs(WIN2D_GET_D2D_IMAGE_FLAGS flags, float targetDpi, ID2D1DeviceContext* deviceContext)
|
||||
{
|
||||
for (int i = 0; i < Sources.Count; i++)
|
||||
{
|
||||
// Retrieve the managed wrapper for the current source
|
||||
IGraphicsEffectSource? source = GetD2DInput(i);
|
||||
|
||||
if (source is null)
|
||||
{
|
||||
// If the source is null and that is not valid here (eg. when drawing), just throw
|
||||
default(InvalidOperationException).ThrowIf((flags & WIN2D_GET_D2D_IMAGE_FLAGS_ALLOW_NULL_EFFECT_INPUTS) == WIN2D_GET_D2D_IMAGE_FLAGS_NONE);
|
||||
}
|
||||
else
|
||||
{
|
||||
using ComPtr<global::ABI.Microsoft.Graphics.Canvas.ICanvasImageInterop> canvasImageInterop = default;
|
||||
|
||||
// Convert to ICanvasImageInterop (this must always succeed, and throws if it doesn't)
|
||||
RcwMarshaller.GetNativeInterface(source, canvasImageInterop.GetAddressOf()).Assert();
|
||||
|
||||
using ComPtr<ID2D1Image> d2D1Image = default;
|
||||
|
||||
float realizedDpi;
|
||||
|
||||
// Invoke GetD2DImage on the underlying object (either a built-in effect or a custom one)
|
||||
HRESULT hresult = canvasImageInterop.Get()->GetD2DImage(
|
||||
device: this.canvasDevice.Get(),
|
||||
deviceContext: deviceContext,
|
||||
flags: flags,
|
||||
targetDpi: targetDpi,
|
||||
realizeDpi: &realizedDpi,
|
||||
ppImage: d2D1Image.GetAddressOf());
|
||||
|
||||
// To match the behavior of ICanvasImageInternal::GetD2DImage in case of failure, check if the flags being used did have the flag
|
||||
// WIN2D_GET_D2D_IMAGE_FLAGS_UNREALIZE_ON_FAILURE set. If not, and the call failed, then we explicitly throw from the returned HRESULT.
|
||||
if ((flags & WIN2D_GET_D2D_IMAGE_FLAGS_UNREALIZE_ON_FAILURE) == WIN2D_GET_D2D_IMAGE_FLAGS_NONE)
|
||||
{
|
||||
Marshal.ThrowExceptionForHR(hresult);
|
||||
}
|
||||
|
||||
bool isDifferentResource = Sources.Storage[i].UpdateResource(d2D1Image.Get());
|
||||
|
||||
bool isDifferentDpi = ApplyDpiCompensation(
|
||||
index: i,
|
||||
d2D1Image: ref *&d2D1Image,
|
||||
inputDpi: realizedDpi,
|
||||
flags: flags,
|
||||
targetDpi: targetDpi,
|
||||
d2D1DeviceContext: deviceContext);
|
||||
|
||||
// If the source or the DPI setting has changed, also update the D2D effect graph
|
||||
if (isDifferentResource || isDifferentDpi)
|
||||
{
|
||||
this.d2D1Effect.Get()->SetInput(index: (uint)i, input: d2D1Image.Get());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,355 +0,0 @@
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using ABI.Microsoft.Graphics.Canvas;
|
||||
using ComputeSharp.D2D1.Extensions;
|
||||
using ComputeSharp.D2D1.WinUI.Buffers;
|
||||
using ComputeSharp.D2D1.WinUI.Extensions;
|
||||
using ComputeSharp.D2D1.WinUI.Helpers;
|
||||
using ComputeSharp.Interop;
|
||||
using ComputeSharp.Win32;
|
||||
using Windows.Graphics.Effects;
|
||||
using static ABI.Microsoft.Graphics.Canvas.WIN2D_GET_D2D_IMAGE_FLAGS;
|
||||
using static ABI.Microsoft.Graphics.Canvas.WIN2D_GET_DEVICE_ASSOCIATION_TYPE;
|
||||
using static ComputeSharp.Win32.D2D1_BORDER_MODE;
|
||||
using static ComputeSharp.Win32.D2D1_DPICOMPENSATION_INTERPOLATION_MODE;
|
||||
using static ComputeSharp.Win32.D2D1_DPICOMPENSATION_PROP;
|
||||
using static ComputeSharp.Win32.D2D1_PROPERTY_TYPE;
|
||||
|
||||
namespace ComputeSharp.D2D1.WinUI;
|
||||
|
||||
/// <inheritdoc/>
|
||||
unsafe partial class PixelShaderEffect<T>
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the marshalled value for <see cref="Sources"/>.
|
||||
/// </summary>
|
||||
/// <param name="index">The index of the <see cref="Sources"/> source to get or set.</param>
|
||||
/// <returns>The marshalled value for <see cref="Sources"/>.</returns>
|
||||
private IGraphicsEffectSource? GetSource(int index)
|
||||
{
|
||||
using ReferenceTracker.Lease _0 = GetReferenceTracker().GetLease();
|
||||
|
||||
lock (this.lockObject)
|
||||
{
|
||||
return this.d2D1Effect.Get() switch
|
||||
{
|
||||
not null => GetD2DInput(index),
|
||||
_ => Sources.Storage[index].GetWrapper()
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the marshalled value for <see cref="Sources"/>.
|
||||
/// </summary>
|
||||
/// <param name="value">The value to set for <see cref="Sources"/>.</param>
|
||||
/// <param name="index">The index of the <see cref="IGraphicsEffectSource"/> source to get or set.</param>
|
||||
private void SetSource(IGraphicsEffectSource? value, int index)
|
||||
{
|
||||
using ReferenceTracker.Lease _0 = GetReferenceTracker().GetLease();
|
||||
|
||||
lock (this.lockObject)
|
||||
{
|
||||
ref SourceReference source = ref Sources.Storage[index];
|
||||
|
||||
if (this.d2D1Effect.Get() is not null)
|
||||
{
|
||||
const WIN2D_GET_D2D_IMAGE_FLAGS flags =
|
||||
WIN2D_GET_D2D_IMAGE_FLAGS_MINIMAL_REALIZATION |
|
||||
WIN2D_GET_D2D_IMAGE_FLAGS_ALLOW_NULL_EFFECT_INPUTS |
|
||||
WIN2D_GET_D2D_IMAGE_FLAGS_UNREALIZE_ON_FAILURE;
|
||||
|
||||
// The WIN2D_GET_D2D_IMAGE_FLAGS_UNREALIZE_ON_FAILURE flag is passed, so we just let the effect
|
||||
// unrealize in case of failure and take no other action (including throwing) if that happens.
|
||||
_ = SetD2DInput(index, value, flags, targetDpi: 0.0f, d2D1DeviceContext: null);
|
||||
}
|
||||
else
|
||||
{
|
||||
source.SetWrapper(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <remarks>This method assumes a lock is taken and the effect is already realized.</remarks>
|
||||
/// <inheritdoc cref="GetSource"/>
|
||||
private IGraphicsEffectSource? GetD2DInput(int index)
|
||||
{
|
||||
using ComPtr<ID2D1Image> d2D1Image = default;
|
||||
|
||||
// Get the ID2D1Image input from the effect
|
||||
this.d2D1Effect.Get()->GetInput(
|
||||
index: (uint)index,
|
||||
input: d2D1Image.GetAddressOf());
|
||||
|
||||
ref SourceReference sourceReference = ref Sources.Storage[index];
|
||||
|
||||
// Check whether the input had a DPI compensation effect added to it, and skip it if so
|
||||
if (sourceReference.HasDpiCompensationEffect)
|
||||
{
|
||||
// Since a realized effect is the only source of authority for the effect state, we also
|
||||
// need to check that the actual source is in fact the same that was previous set by the
|
||||
// managed wrapper. If it is, we get the input and return that (ie. the underlying image).
|
||||
// If not, we just return it directly and reset the locally stored DPI compensation effect.
|
||||
if (d2D1Image.Get()->IsSameInstance(sourceReference.GetDpiCompensationEffect()))
|
||||
{
|
||||
sourceReference.GetDpiCompensationEffect()->GetInput(0, d2D1Image.ReleaseAndGetAddressOf());
|
||||
}
|
||||
else
|
||||
{
|
||||
sourceReference.SetDpiCompensationEffect(null);
|
||||
}
|
||||
}
|
||||
|
||||
// Get or create a wrapper for the input image
|
||||
return sourceReference.GetOrCreateWrapper(this.canvasDevice.Get(), d2D1Image.Get());
|
||||
}
|
||||
|
||||
/// <param name="index">The index of the <see cref="IGraphicsEffectSource"/> source to get or set.</param>
|
||||
/// <param name="value">The value to set for <see cref="Sources"/>.</param>
|
||||
/// <param name="flags">The <see cref="WIN2D_GET_D2D_IMAGE_FLAGS"/> flags for <paramref name="value"/>.</param>
|
||||
/// <param name="targetDpi">The target DPI for the realized image, if present.</param>
|
||||
/// <param name="d2D1DeviceContext">The input <see cref="ID2D1DeviceContext"/> object to use, if present.</param>
|
||||
/// <returns>Whether the input was set successfully or not.</returns>
|
||||
/// <remarks>This method assumes a lock is taken and the effect is already realized.</remarks>
|
||||
/// <inheritdoc cref="SetSource"/>
|
||||
private bool SetD2DInput(
|
||||
int index,
|
||||
IGraphicsEffectSource? value,
|
||||
WIN2D_GET_D2D_IMAGE_FLAGS flags,
|
||||
float targetDpi,
|
||||
ID2D1DeviceContext* d2D1DeviceContext)
|
||||
{
|
||||
using ComPtr<ID2D1Image> d2D1Image = default;
|
||||
|
||||
float realizedDpi = 0;
|
||||
|
||||
if (value is not null)
|
||||
{
|
||||
using ComPtr<ICanvasImageInterop> canvasImageInterop = default;
|
||||
|
||||
// Try to get the ICanvasImageInterop interface from the input source
|
||||
HRESULT hresult = RcwMarshaller.GetNativeInterface(value, canvasImageInterop.GetAddressOf());
|
||||
|
||||
if (!Win32.Windows.SUCCEEDED(hresult))
|
||||
{
|
||||
// Only potentially handle E_NOINTERFACE, always throw in other cases
|
||||
if (hresult != E.E_NOINTERFACE)
|
||||
{
|
||||
Marshal.ThrowExceptionForHR(hresult);
|
||||
}
|
||||
|
||||
// If unrealization isn't requested for failures, just throw
|
||||
default(InvalidOperationException).ThrowIf((flags & WIN2D_GET_D2D_IMAGE_FLAGS_UNREALIZE_ON_FAILURE) == WIN2D_GET_D2D_IMAGE_FLAGS_NONE);
|
||||
|
||||
Unrealize(value, index);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
using ComPtr<ICanvasDevice> sourceCanvasDevice = default;
|
||||
|
||||
WIN2D_GET_DEVICE_ASSOCIATION_TYPE sourceCanvasDeviceType = WIN2D_GET_DEVICE_ASSOCIATION_TYPE_UNSPECIFIED;
|
||||
|
||||
// Get the canvas device the source is realized on, if any, and its association type
|
||||
canvasImageInterop.Get()->GetDevice(sourceCanvasDevice.GetAddressOf(), &sourceCanvasDeviceType).Assert();
|
||||
|
||||
// If the specified source is bound to a creation device, validate that this matches the one we are realized on.
|
||||
// A "creation device" is a canvas device that is used to create the native resources in a given canvas image
|
||||
// implementation, such that the canvas device and the canvas image are bound for the entire lifetime of the
|
||||
// canvas image itself. This applies to eg. CanvasBitmap, which directly wraps some native memory allocated on
|
||||
// a specific device. In these cases, if the associated device doesn't match the one this effect is currently
|
||||
// realized on, that is an error and the source is invalid.
|
||||
//
|
||||
// Otherwise, the device returned here will depend on the specific implementation of this external effect. That is,
|
||||
// it could be null for an unrealized effect, it could be null for a lazily initialized device-bound resource, or it
|
||||
// could also be an already instantiated resource tied to a specific device.
|
||||
//
|
||||
// The only case where the device has to be an exact match is if the device is an owning device for the canvas image.
|
||||
// In all other cases (including when the device is null), we can proceed to resolve the effects graph normally. This
|
||||
// will allow sources to either just work in case the device matches, or unrealize and re-realize themselves on the
|
||||
// new device if they were already realized on a different device (so that things would just work fine if someone was
|
||||
// trying to draw a reused effect after a device lost event, for instance). In the niche scenario where a given source
|
||||
// was some lazily-initialized instance that doesn't allow changing device after that, then the call to Get2D2Image
|
||||
// on that specific source will just fail during the recursive traversal, but that couldn't be checked from here anyway.
|
||||
if (sourceCanvasDeviceType == WIN2D_GET_DEVICE_ASSOCIATION_TYPE_CREATION_DEVICE && !this.canvasDevice.Get()->IsSameInstance(sourceCanvasDevice.Get()))
|
||||
{
|
||||
default(InvalidOperationException).ThrowIf((flags & WIN2D_GET_D2D_IMAGE_FLAGS_UNREALIZE_ON_FAILURE) == WIN2D_GET_D2D_IMAGE_FLAGS_NONE);
|
||||
|
||||
Unrealize(value, index);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// Try to realize the image from the input source (this will recurse through the effect graph)
|
||||
hresult = canvasImageInterop.Get()->GetD2DImage(
|
||||
device: this.canvasDevice.Get(),
|
||||
deviceContext: d2D1DeviceContext,
|
||||
flags: flags,
|
||||
targetDpi: targetDpi,
|
||||
realizeDpi: &realizedDpi,
|
||||
ppImage: d2D1Image.GetAddressOf());
|
||||
|
||||
// Unless requested, a failure to retrieve the image should just bubble up the HRESULT in an exception
|
||||
if ((flags & WIN2D_GET_D2D_IMAGE_FLAGS_UNREALIZE_ON_FAILURE) == WIN2D_GET_D2D_IMAGE_FLAGS_NONE)
|
||||
{
|
||||
hresult.Assert();
|
||||
}
|
||||
|
||||
// If no realized image is available, unrealize the effect
|
||||
if (d2D1Image.Get() is null)
|
||||
{
|
||||
Unrealize(value, index);
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else if ((flags & WIN2D_GET_D2D_IMAGE_FLAGS_ALLOW_NULL_EFFECT_INPUTS) == WIN2D_GET_D2D_IMAGE_FLAGS_NONE)
|
||||
{
|
||||
// If the source is null and this is not explicitly allowed, the invocation is not valid
|
||||
default(ArgumentNullException).Throw(nameof(value));
|
||||
}
|
||||
|
||||
// Save the managed wrapper and realized image
|
||||
Sources.Storage[index].SetWrapperAndResource(value, d2D1Image.Get());
|
||||
|
||||
// Handle DPI compensation (the return is ignored here as whether DPI compensation was applied doesn't matter)
|
||||
_ = ApplyDpiCompensation(
|
||||
index: index,
|
||||
d2D1Image: ref *&d2D1Image,
|
||||
inputDpi: realizedDpi,
|
||||
flags: flags,
|
||||
targetDpi: targetDpi,
|
||||
d2D1DeviceContext: d2D1DeviceContext);
|
||||
|
||||
// Set the actual effect input
|
||||
this.d2D1Effect.Get()->SetInput(index: (uint)index, input: d2D1Image.Get());
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Applies a DPI compensation effect onto a realized image, if needed.
|
||||
/// </summary>
|
||||
/// <param name="index">The index of the source being processed.</param>
|
||||
/// <param name="d2D1Image">A reference to the realized source (this might be replaced if DPI compensation is added).</param>
|
||||
/// <param name="inputDpi">The input DPU for <paramref name="d2D1Image"/>.</param>
|
||||
/// <param name="flags">The <see cref="WIN2D_GET_D2D_IMAGE_FLAGS"/> flags for <paramref name="d2D1Image"/>.</param>
|
||||
/// <param name="targetDpi">The target DPI for the realized image, if present.</param>
|
||||
/// <param name="d2D1DeviceContext">The input <see cref="ID2D1DeviceContext"/> object to use, if present.</param>
|
||||
/// <returns>Whether the DPI configuration has changed.</returns>
|
||||
private bool ApplyDpiCompensation(
|
||||
int index,
|
||||
ref ComPtr<ID2D1Image> d2D1Image,
|
||||
float inputDpi,
|
||||
WIN2D_GET_D2D_IMAGE_FLAGS flags,
|
||||
float targetDpi,
|
||||
ID2D1DeviceContext* d2D1DeviceContext)
|
||||
{
|
||||
ref SourceReference sourceReference = ref Sources.Storage[index];
|
||||
|
||||
bool isDpiCompensationNeeded;
|
||||
|
||||
// Detemine whether DPI compensation is needed (direct port of Win2D's CanvasEffect::ApplyDpiCompensation)
|
||||
if ((flags & WIN2D_GET_D2D_IMAGE_FLAGS_MINIMAL_REALIZATION) != WIN2D_GET_D2D_IMAGE_FLAGS_NONE)
|
||||
{
|
||||
// In minimal mode, we don't yet know the target DPI. For instance this occurs when setting an
|
||||
// effect as the source of an image brush. We'll fix up later when the brush is drawn to a device
|
||||
// context, at which point the real DPI is known. Since we don't yet know whether to include
|
||||
// compensation, we just preserve the existing state. This avoids repeated recreation during
|
||||
// set-draw-set-draw sequences.
|
||||
isDpiCompensationNeeded = sourceReference.HasDpiCompensationEffect && (inputDpi != 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
bool neverCompensate = (flags & WIN2D_GET_D2D_IMAGE_FLAGS_NEVER_INSERT_DPI_COMPENSATION) != WIN2D_GET_D2D_IMAGE_FLAGS_NONE;
|
||||
bool alwaysCompensate = (flags & WIN2D_GET_D2D_IMAGE_FLAGS_ALWAYS_INSERT_DPI_COMPENSATION) != WIN2D_GET_D2D_IMAGE_FLAGS_NONE;
|
||||
|
||||
// We need DPI compensation if:
|
||||
// - The input has a fixed DPI
|
||||
// - We were not told never to include it
|
||||
// - Either we were told to always include it, or the input DPI is different from the target
|
||||
isDpiCompensationNeeded = (inputDpi != 0) && !neverCompensate && (alwaysCompensate || (inputDpi != targetDpi));
|
||||
|
||||
// Early out if we are already in the right state
|
||||
if (isDpiCompensationNeeded == sourceReference.HasDpiCompensationEffect)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Apply the determined DPI compensation configuration
|
||||
if (isDpiCompensationNeeded)
|
||||
{
|
||||
using ComPtr<ID2D1Effect> d2D1EffectDpiCompensation = default;
|
||||
|
||||
// Create an ID2D1Effect for the DPI compensation, if there isn't one already
|
||||
if (!sourceReference.HasDpiCompensationEffect)
|
||||
{
|
||||
using (ComPtr<ID2D1DeviceContext> d2D1DeviceContextEffective = default)
|
||||
using (ComPtr<ID2D1DeviceContextLease> d2D1DeviceContextLease = default)
|
||||
{
|
||||
// Just like when realizing the current effect, rent a device context from the pool if one is not available
|
||||
this.canvasDevice.Get()->GetEffectiveD2DDeviceContextWithOptionalLease(
|
||||
d2D1DeviceContext: d2D1DeviceContext,
|
||||
d2D1DeviceContextEffective: d2D1DeviceContextEffective.GetAddressOf(),
|
||||
d2D1DeviceContextLease: d2D1DeviceContextLease.GetAddressOf());
|
||||
|
||||
// Create the DPI compensation effect
|
||||
d2D1DeviceContextEffective.Get()->CreateEffect(
|
||||
effectId: (Guid*)Unsafe.AsPointer(ref Unsafe.AsRef(in CLSID.CLSID_D2D1DpiCompensation)),
|
||||
effect: d2D1EffectDpiCompensation.GetAddressOf()).Assert();
|
||||
}
|
||||
|
||||
D2D1_BORDER_MODE d2D1BorderMode = D2D1_BORDER_MODE_HARD;
|
||||
D2D1_DPICOMPENSATION_INTERPOLATION_MODE d2D1DpiCompensationInterpolationMode = D2D1_DPICOMPENSATION_INTERPOLATION_MODE_LINEAR;
|
||||
|
||||
// Set the border mode to hard
|
||||
d2D1EffectDpiCompensation.Get()->SetValue(
|
||||
index: (uint)D2D1_DPICOMPENSATION_PROP_BORDER_MODE,
|
||||
type: D2D1_PROPERTY_TYPE_UNKNOWN,
|
||||
data: (byte*)&d2D1BorderMode,
|
||||
dataSize: sizeof(D2D1_BORDER_MODE)).Assert();
|
||||
|
||||
// Set the interpolation mode to linear
|
||||
d2D1EffectDpiCompensation.Get()->SetValue(
|
||||
index: (uint)D2D1_DPICOMPENSATION_PROP_INTERPOLATION_MODE,
|
||||
type: D2D1_PROPERTY_TYPE_UNKNOWN,
|
||||
data: (byte*)&d2D1DpiCompensationInterpolationMode,
|
||||
dataSize: sizeof(D2D1_DPICOMPENSATION_INTERPOLATION_MODE)).Assert();
|
||||
|
||||
// Store the DPI effect for later use
|
||||
sourceReference.SetDpiCompensationEffect(d2D1EffectDpiCompensation.Get());
|
||||
}
|
||||
else
|
||||
{
|
||||
// Just reuse the existing DPI effect otherwise
|
||||
d2D1EffectDpiCompensation.Attach(new ComPtr<ID2D1Effect>(sourceReference.GetDpiCompensationEffect()).Get());
|
||||
}
|
||||
|
||||
// Set the input image as source for the DPI compensation effect
|
||||
sourceReference.GetDpiCompensationEffect()->SetInput(index: (uint)index, input: d2D1Image.Get());
|
||||
|
||||
D2D_VECTOR_2F d2DVectorDpi;
|
||||
d2DVectorDpi.x = inputDpi;
|
||||
d2DVectorDpi.y = inputDpi;
|
||||
|
||||
// Set the DPI setting
|
||||
sourceReference.GetDpiCompensationEffect()->SetValue(
|
||||
index: (uint)D2D1_DPICOMPENSATION_PROP_INPUT_DPI,
|
||||
type: D2D1_PROPERTY_TYPE_UNKNOWN,
|
||||
data: (byte*)&d2DVectorDpi,
|
||||
dataSize: (uint)sizeof(D2D_VECTOR_2F)).Assert();
|
||||
|
||||
// Substitute the input image with the DPI compensation effect
|
||||
d2D1EffectDpiCompensation.CopyTo(ref d2D1Image).Assert();
|
||||
}
|
||||
else
|
||||
{
|
||||
// Ensure the existing effect is released, if present
|
||||
sourceReference.SetDpiCompensationEffect(null);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -1,235 +0,0 @@
|
||||
using System;
|
||||
using ComputeSharp.D2D1.Interop;
|
||||
using ComputeSharp.D2D1.WinUI.Extensions;
|
||||
using ComputeSharp.Interop;
|
||||
using ComputeSharp.Win32;
|
||||
using Microsoft.Graphics.Canvas;
|
||||
|
||||
namespace ComputeSharp.D2D1.WinUI;
|
||||
|
||||
/// <inheritdoc/>
|
||||
unsafe partial class PixelShaderEffect<T>
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the marshalled value for <see cref="CacheOutput"/>.
|
||||
/// </summary>
|
||||
/// <returns>The marshalled value for <see cref="CacheOutput"/>.</returns>
|
||||
private T GetConstantBuffer()
|
||||
{
|
||||
using ReferenceTracker.Lease _0 = GetReferenceTracker().GetLease();
|
||||
|
||||
lock (this.lockObject)
|
||||
{
|
||||
return this.d2D1Effect.Get() switch
|
||||
{
|
||||
not null => this.d2D1Effect.Get()->GetConstantBuffer<T>(),
|
||||
_ => this.constantBuffer
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the marshalled value for <see cref="ConstantBuffer"/>.
|
||||
/// </summary>
|
||||
/// <param name="value">The value to set for <see cref="ConstantBuffer"/>.</param>
|
||||
private void SetConstantBuffer(in T value)
|
||||
{
|
||||
using ReferenceTracker.Lease _0 = GetReferenceTracker().GetLease();
|
||||
|
||||
lock (this.lockObject)
|
||||
{
|
||||
// If the effect is realized, set the property on the underlying ID2D1Effect object
|
||||
if (this.d2D1Effect.Get() is not null)
|
||||
{
|
||||
this.d2D1Effect.Get()->SetConstantBuffer(in value);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Otherwise, just store the value locally
|
||||
this.constantBuffer = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the marshalled value for <see cref="TransformMapper"/>.
|
||||
/// </summary>
|
||||
/// <returns>The marshalled value for <see cref="TransformMapper"/>.</returns>
|
||||
private D2D1DrawTransformMapper<T>? GetTransformMapper()
|
||||
{
|
||||
using ReferenceTracker.Lease _0 = GetReferenceTracker().GetLease();
|
||||
|
||||
lock (this.lockObject)
|
||||
{
|
||||
return this.d2D1Effect.Get() switch
|
||||
{
|
||||
not null => this.d2D1Effect.Get()->GetTransformMapper<T>(),
|
||||
_ => this.transformMapper
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the marshalled value for <see cref="TransformMapper"/>.
|
||||
/// </summary>
|
||||
/// <param name="value">The value to set for <see cref="TransformMapper"/>.</param>
|
||||
private void SetTransformMapper(D2D1DrawTransformMapper<T> value)
|
||||
{
|
||||
using ReferenceTracker.Lease _0 = GetReferenceTracker().GetLease();
|
||||
|
||||
lock (this.lockObject)
|
||||
{
|
||||
if (this.d2D1Effect.Get() is not null)
|
||||
{
|
||||
D2D1PixelShaderEffect.SetTransformMapperForD2D1Effect(this.d2D1Effect.Get(), value);
|
||||
}
|
||||
else
|
||||
{
|
||||
// If the effect is not realized, manually ensure the transform isn't null
|
||||
default(ArgumentNullException).ThrowIfNull(value);
|
||||
|
||||
this.transformMapper = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the marshalled value for <see cref="ResourceTextureManagers"/>.
|
||||
/// </summary>
|
||||
/// <param name="index">The index of the <see cref="D2D1ResourceTextureManager"/> source to get or set.</param>
|
||||
/// <returns>The marshalled value for <see cref="ResourceTextureManagers"/>.</returns>
|
||||
private D2D1ResourceTextureManager? GetResourceTextureManager(int index)
|
||||
{
|
||||
using ReferenceTracker.Lease _0 = GetReferenceTracker().GetLease();
|
||||
|
||||
lock (this.lockObject)
|
||||
{
|
||||
D2D1ResourceTextureManager? resourceTextureManager = ResourceTextureManagers.Storage[index];
|
||||
|
||||
return this.d2D1Effect.Get() switch
|
||||
{
|
||||
not null => this.d2D1Effect.Get()->GetResourceTextureManager(resourceTextureManager, index),
|
||||
_ => resourceTextureManager
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the marshalled value for <see cref="ResourceTextureManagers"/>.
|
||||
/// </summary>
|
||||
/// <param name="value">The value to set for <see cref="ResourceTextureManagers"/>.</param>
|
||||
/// <param name="index">The index of the <see cref="D2D1ResourceTextureManager"/> source to get or set.</param>
|
||||
private void SetResourceTextureManager(D2D1ResourceTextureManager value, int index)
|
||||
{
|
||||
using ReferenceTracker.Lease _0 = GetReferenceTracker().GetLease();
|
||||
|
||||
lock (this.lockObject)
|
||||
{
|
||||
if (this.d2D1Effect.Get() is not null)
|
||||
{
|
||||
D2D1PixelShaderEffect.SetResourceTextureManagerForD2D1Effect(this.d2D1Effect.Get(), value, index);
|
||||
}
|
||||
else
|
||||
{
|
||||
ResourceTextureManagers.Storage[index] = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the marshalled value for <see cref="CacheOutput"/>.
|
||||
/// </summary>
|
||||
/// <returns>The marshalled value for <see cref="CacheOutput"/>.</returns>
|
||||
private bool GetCacheOutput()
|
||||
{
|
||||
using ReferenceTracker.Lease _0 = GetReferenceTracker().GetLease();
|
||||
|
||||
lock (this.lockObject)
|
||||
{
|
||||
return this.d2D1Effect.Get() switch
|
||||
{
|
||||
not null => this.d2D1Effect.Get()->GetCachedProperty(),
|
||||
_ => this.cacheOutput
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the marshalled value for <see cref="CacheOutput"/>.
|
||||
/// </summary>
|
||||
/// <param name="value">The value to set for <see cref="CacheOutput"/>.</param>
|
||||
private void SetCacheOutput(bool value)
|
||||
{
|
||||
using ReferenceTracker.Lease _0 = GetReferenceTracker().GetLease();
|
||||
|
||||
lock (this.lockObject)
|
||||
{
|
||||
if (this.d2D1Effect.Get() is not null)
|
||||
{
|
||||
this.d2D1Effect.Get()->SetCachedProperty(value);
|
||||
}
|
||||
else
|
||||
{
|
||||
this.cacheOutput = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the marshalled value for <see cref="BufferPrecision"/>.
|
||||
/// </summary>
|
||||
/// <returns>The marshalled value for <see cref="BufferPrecision"/>.</returns>
|
||||
private CanvasBufferPrecision? GetBufferPrecision()
|
||||
{
|
||||
using ReferenceTracker.Lease _0 = GetReferenceTracker().GetLease();
|
||||
|
||||
lock (this.lockObject)
|
||||
{
|
||||
D2D1_BUFFER_PRECISION d2D1BufferPrecision = this.d2D1Effect.Get() switch
|
||||
{
|
||||
not null => this.d2D1Effect.Get()->GetPrecisionProperty(),
|
||||
_ => this.d2D1BufferPrecision
|
||||
};
|
||||
|
||||
// Map from D2D1_BUFFER_PRECISION to CanvasBufferPrecision, and return null for D2D1_BUFFER_PRECISION_UNKNOWN
|
||||
return d2D1BufferPrecision switch
|
||||
{
|
||||
D2D1_BUFFER_PRECISION.D2D1_BUFFER_PRECISION_8BPC_UNORM => CanvasBufferPrecision.Precision8UIntNormalized,
|
||||
D2D1_BUFFER_PRECISION.D2D1_BUFFER_PRECISION_8BPC_UNORM_SRGB => CanvasBufferPrecision.Precision8UIntNormalizedSrgb,
|
||||
D2D1_BUFFER_PRECISION.D2D1_BUFFER_PRECISION_16BPC_UNORM => CanvasBufferPrecision.Precision16UIntNormalized,
|
||||
D2D1_BUFFER_PRECISION.D2D1_BUFFER_PRECISION_16BPC_FLOAT => CanvasBufferPrecision.Precision16Float,
|
||||
D2D1_BUFFER_PRECISION.D2D1_BUFFER_PRECISION_32BPC_FLOAT => CanvasBufferPrecision.Precision32Float,
|
||||
_ => null
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the marshalled value for <see cref="BufferPrecision"/>.
|
||||
/// </summary>
|
||||
/// <param name="value">The value to set for <see cref="BufferPrecision"/>.</param>
|
||||
private void SetBufferPrecision(CanvasBufferPrecision? value)
|
||||
{
|
||||
using ReferenceTracker.Lease _0 = GetReferenceTracker().GetLease();
|
||||
|
||||
lock (this.lockObject)
|
||||
{
|
||||
// Map back from CanvasBufferPrecision? to D2D1_BUFFER_PRECISION (null results in D2D1_BUFFER_PRECISION_UNKNOWN)
|
||||
this.d2D1BufferPrecision = value switch
|
||||
{
|
||||
CanvasBufferPrecision.Precision8UIntNormalized => D2D1_BUFFER_PRECISION.D2D1_BUFFER_PRECISION_8BPC_UNORM,
|
||||
CanvasBufferPrecision.Precision8UIntNormalizedSrgb => D2D1_BUFFER_PRECISION.D2D1_BUFFER_PRECISION_8BPC_UNORM_SRGB,
|
||||
CanvasBufferPrecision.Precision16UIntNormalized => D2D1_BUFFER_PRECISION.D2D1_BUFFER_PRECISION_16BPC_UNORM,
|
||||
CanvasBufferPrecision.Precision16Float => D2D1_BUFFER_PRECISION.D2D1_BUFFER_PRECISION_16BPC_FLOAT,
|
||||
CanvasBufferPrecision.Precision32Float => D2D1_BUFFER_PRECISION.D2D1_BUFFER_PRECISION_32BPC_FLOAT,
|
||||
_ => D2D1_BUFFER_PRECISION.D2D1_BUFFER_PRECISION_UNKNOWN
|
||||
};
|
||||
|
||||
// If the effect is realized, set the property on the underlying ID2D1Effect object too
|
||||
if (this.d2D1Effect.Get() is not null)
|
||||
{
|
||||
this.d2D1Effect.Get()->SetPrecisionProperty(this.d2D1BufferPrecision);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
-261
@@ -1,261 +0,0 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Runtime.InteropServices;
|
||||
using ComputeSharp.D2D1.Interop;
|
||||
using ComputeSharp.D2D1.WinUI.Buffers;
|
||||
using ComputeSharp.D2D1.WinUI.Collections;
|
||||
|
||||
#pragma warning disable CS8767
|
||||
|
||||
namespace ComputeSharp.D2D1.WinUI;
|
||||
|
||||
/// <inheritdoc/>
|
||||
partial class PixelShaderEffect<T>
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents the collection of <see cref="D2D1ResourceTextureManager"/> objects in a <see cref="PixelShaderEffect{T}"/> instance.
|
||||
/// </summary>
|
||||
/// <remarks><inheritdoc cref="EffectFactory" path="/remarks/node()"/></remarks>
|
||||
public sealed partial class ResourceTextureManagerCollection : IList<D2D1ResourceTextureManager?>, IReadOnlyList<D2D1ResourceTextureManager?>, IList, IFixedCountList<D2D1ResourceTextureManager?>
|
||||
{
|
||||
/// <summary>
|
||||
/// The bitmask of valid indices for resource texture managers in the current shader type.
|
||||
/// </summary>
|
||||
private static readonly int IndexBitmask = GetIndexBitmask();
|
||||
|
||||
/// <summary>
|
||||
/// The fixed buffer of <see cref="D2D1ResourceTextureManager"/> instances.
|
||||
/// </summary>
|
||||
private ResourceTextureManagerBuffer fixedBuffer;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="ResourceTextureManagerCollection"/> instance with the specified parameters.
|
||||
/// </summary>
|
||||
/// <param name="owner">The owning <see cref="PixelShaderEffect{T}"/> instance.</param>
|
||||
internal ResourceTextureManagerCollection(PixelShaderEffect<T> owner)
|
||||
{
|
||||
Owner = owner;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public int Count => Indices.Length;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the <see cref="D2D1ResourceTextureManager"/> object at a specified index.
|
||||
/// </summary>
|
||||
/// <param name="index">The index of the <see cref="D2D1ResourceTextureManager"/> source to get or set.</param>
|
||||
/// <returns>The <see cref="D2D1ResourceTextureManager"/> object at the specified index.</returns>
|
||||
/// <exception cref="ArgumentNullException">Thrown if the value is set to <see langword="null"/>.</exception>
|
||||
/// <exception cref="ArgumentOutOfRangeException">Thrown if <paramref name="index"/> is not a valid index for the current effect.</exception>
|
||||
[DisallowNull]
|
||||
public D2D1ResourceTextureManager? this[int index]
|
||||
{
|
||||
get
|
||||
{
|
||||
default(ArgumentOutOfRangeException).ThrowIfNotInRange(index, 0, 16, nameof(index));
|
||||
default(ArgumentOutOfRangeException).ThrowIf((IndexBitmask & (1 << index)) == 0, nameof(index));
|
||||
|
||||
return Owner.GetResourceTextureManager(index);
|
||||
}
|
||||
set
|
||||
{
|
||||
default(ArgumentOutOfRangeException).ThrowIfNotInRange(index, 0, 16, nameof(index));
|
||||
default(ArgumentOutOfRangeException).ThrowIf((IndexBitmask & (1 << index)) == 0, nameof(index));
|
||||
default(ArgumentNullException).ThrowIfNull(value);
|
||||
|
||||
Owner.SetResourceTextureManager(value, index);
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
object? IList.this[int index]
|
||||
{
|
||||
get => this[index];
|
||||
set => this[index] = (D2D1ResourceTextureManager?)value!;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the collection of valid resource texture indices for the current effect.
|
||||
/// </summary>
|
||||
internal static ImmutableArray<int> Indices { get; } = GetIndices();
|
||||
|
||||
/// <summary>
|
||||
/// Gets the owning <see cref="PixelShaderEffect{T}"/> instance.
|
||||
/// </summary>
|
||||
internal PixelShaderEffect<T> Owner { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets a reference to the <see cref="ResourceTextureManagerBuffer"/> value containing the available <see cref="D2D1ResourceTextureManager"/>-s.
|
||||
/// </summary>
|
||||
internal ref ResourceTextureManagerBuffer Storage => ref this.fixedBuffer;
|
||||
|
||||
/// <inheritdoc/>
|
||||
ImmutableArray<int> IFixedCountList<D2D1ResourceTextureManager?>.Indices => Indices;
|
||||
|
||||
/// <inheritdoc/>
|
||||
bool ICollection<D2D1ResourceTextureManager?>.IsReadOnly => false;
|
||||
|
||||
/// <inheritdoc/>
|
||||
bool IList.IsReadOnly => false;
|
||||
|
||||
/// <inheritdoc/>
|
||||
bool IList.IsFixedSize => true;
|
||||
|
||||
/// <inheritdoc/>
|
||||
bool ICollection.IsSynchronized => true;
|
||||
|
||||
/// <inheritdoc/>
|
||||
object ICollection.SyncRoot => default(NotSupportedException).Throw<object>();
|
||||
|
||||
/// <inheritdoc/>
|
||||
void ICollection<D2D1ResourceTextureManager?>.Add(D2D1ResourceTextureManager? item)
|
||||
{
|
||||
default(NotSupportedException).Throw();
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
int IList.Add(object? value)
|
||||
{
|
||||
return default(NotSupportedException).Throw<int>();
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
void ICollection<D2D1ResourceTextureManager?>.Clear()
|
||||
{
|
||||
default(NotSupportedException).Throw();
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
void IList.Clear()
|
||||
{
|
||||
default(NotSupportedException).Throw();
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
bool ICollection<D2D1ResourceTextureManager?>.Contains(D2D1ResourceTextureManager? item)
|
||||
{
|
||||
return FixedCountList<D2D1ResourceTextureManager?>.IndexOf(this, item) != -1;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
bool IList.Contains(object? value)
|
||||
{
|
||||
return FixedCountList<D2D1ResourceTextureManager?>.IndexOf(this, (D2D1ResourceTextureManager?)value) != -1;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
void ICollection<D2D1ResourceTextureManager?>.CopyTo(D2D1ResourceTextureManager?[] array, int arrayIndex)
|
||||
{
|
||||
FixedCountList<D2D1ResourceTextureManager?>.CopyTo(this, array, arrayIndex);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
void ICollection.CopyTo(Array array, int index)
|
||||
{
|
||||
FixedCountList<D2D1ResourceTextureManager?>.CopyTo(this, array, index);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
IEnumerator<D2D1ResourceTextureManager?> IEnumerable<D2D1ResourceTextureManager?>.GetEnumerator()
|
||||
{
|
||||
return FixedCountList<D2D1ResourceTextureManager?>.GetEnumerator(this);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
IEnumerator IEnumerable.GetEnumerator()
|
||||
{
|
||||
return FixedCountList<D2D1ResourceTextureManager?>.GetEnumerator(this);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
int IList<D2D1ResourceTextureManager?>.IndexOf(D2D1ResourceTextureManager? item)
|
||||
{
|
||||
return FixedCountList<D2D1ResourceTextureManager?>.IndexOf(this, item);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
int IList.IndexOf(object? value)
|
||||
{
|
||||
return FixedCountList<D2D1ResourceTextureManager?>.IndexOf(this, (D2D1ResourceTextureManager?)value);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
void IList<D2D1ResourceTextureManager?>.Insert(int index, D2D1ResourceTextureManager? item)
|
||||
{
|
||||
default(NotSupportedException).Throw();
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
void IList.Insert(int index, object? value)
|
||||
{
|
||||
default(NotSupportedException).Throw();
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
bool ICollection<D2D1ResourceTextureManager?>.Remove(D2D1ResourceTextureManager? item)
|
||||
{
|
||||
return default(NotSupportedException).Throw<bool>();
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
void IList.Remove(object? value)
|
||||
{
|
||||
default(NotSupportedException).Throw();
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
void IList<D2D1ResourceTextureManager?>.RemoveAt(int index)
|
||||
{
|
||||
default(NotSupportedException).Throw();
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
void IList.RemoveAt(int index)
|
||||
{
|
||||
default(NotSupportedException).Throw();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the bitmask of indices for resource textures in the target shader.
|
||||
/// </summary>
|
||||
/// <returns>The bitmask of indices for resource textures in the target shader.</returns>
|
||||
private static int GetIndexBitmask()
|
||||
{
|
||||
int indexBitmask = 0;
|
||||
|
||||
foreach (ref readonly D2D1ResourceTextureDescription description in T.ResourceTextureDescriptions.Span)
|
||||
{
|
||||
indexBitmask |= 1 << description.Index;
|
||||
}
|
||||
|
||||
return indexBitmask;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the collection of valid indices for the current effect.
|
||||
/// </summary>
|
||||
/// <returns>The collection of valid indices for the current effect.</returns>
|
||||
private static ImmutableArray<int> GetIndices()
|
||||
{
|
||||
ReadOnlySpan<D2D1ResourceTextureDescription> descriptions = T.ResourceTextureDescriptions.Span;
|
||||
|
||||
// Skip the array allocation if there are no resource texture descriptions
|
||||
if (descriptions.IsEmpty)
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
int[] indices = new int[descriptions.Length];
|
||||
|
||||
for (int i = 0; i < descriptions.Length; i++)
|
||||
{
|
||||
indices[i] = descriptions[i].Index;
|
||||
}
|
||||
|
||||
return ImmutableCollectionsMarshal.AsImmutableArray(indices);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,231 +0,0 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using ComputeSharp.D2D1.WinUI.Buffers;
|
||||
using ComputeSharp.D2D1.WinUI.Collections;
|
||||
using Windows.Graphics.Effects;
|
||||
|
||||
namespace ComputeSharp.D2D1.WinUI;
|
||||
|
||||
/// <inheritdoc/>
|
||||
partial class PixelShaderEffect<T>
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents the collection of <see cref="IGraphicsEffectSource"/> sources in a <see cref="PixelShaderEffect{T}"/> instance.
|
||||
/// </summary>
|
||||
/// <remarks><inheritdoc cref="EffectFactory" path="/remarks/node()"/></remarks>
|
||||
public sealed partial class SourceCollection : IList<IGraphicsEffectSource?>, IReadOnlyList<IGraphicsEffectSource?>, IList, IFixedCountList<IGraphicsEffectSource?>
|
||||
{
|
||||
/// <summary>
|
||||
/// The collection of valid resource texture indices for the current effect.
|
||||
/// </summary>
|
||||
private static readonly ImmutableArray<int> Indices = GetIndices();
|
||||
|
||||
/// <summary>
|
||||
/// The fixed buffer of <see cref="IGraphicsEffectSource"/> instances.
|
||||
/// </summary>
|
||||
private GraphicsEffectSourceBuffer fixedBuffer;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="SourceCollection"/> instance with the specified parameters.
|
||||
/// </summary>
|
||||
/// <param name="owner">The owning <see cref="PixelShaderEffect{T}"/> instance.</param>
|
||||
internal SourceCollection(PixelShaderEffect<T> owner)
|
||||
{
|
||||
Owner = owner;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public int Count
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => T.InputCount;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public IGraphicsEffectSource? this[int index]
|
||||
{
|
||||
get
|
||||
{
|
||||
default(ArgumentOutOfRangeException).ThrowIfNotInRange(index, 0, T.InputCount, nameof(index));
|
||||
|
||||
return Owner.GetSource(index);
|
||||
}
|
||||
set
|
||||
{
|
||||
default(ArgumentOutOfRangeException).ThrowIfNotInRange(index, 0, T.InputCount, nameof(index));
|
||||
|
||||
Owner.SetSource(value, index);
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
object? IList.this[int index]
|
||||
{
|
||||
get => this[index];
|
||||
set => this[index] = (IGraphicsEffectSource?)value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the owning <see cref="PixelShaderEffect{T}"/> instance.
|
||||
/// </summary>
|
||||
internal PixelShaderEffect<T> Owner { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets a reference to the <see cref="GraphicsEffectSourceBuffer"/> value containing the available <see cref="IGraphicsEffectSource"/>-s.
|
||||
/// </summary>
|
||||
internal ref GraphicsEffectSourceBuffer Storage => ref this.fixedBuffer;
|
||||
|
||||
/// <inheritdoc/>
|
||||
ImmutableArray<int> IFixedCountList<IGraphicsEffectSource?>.Indices => Indices;
|
||||
|
||||
/// <inheritdoc/>
|
||||
bool ICollection<IGraphicsEffectSource?>.IsReadOnly => false;
|
||||
|
||||
/// <inheritdoc/>
|
||||
bool IList.IsReadOnly => false;
|
||||
|
||||
/// <inheritdoc/>
|
||||
bool IList.IsFixedSize => true;
|
||||
|
||||
/// <inheritdoc/>
|
||||
bool ICollection.IsSynchronized => true;
|
||||
|
||||
/// <inheritdoc/>
|
||||
object ICollection.SyncRoot => default(NotSupportedException).Throw<object>();
|
||||
|
||||
/// <inheritdoc/>
|
||||
void ICollection<IGraphicsEffectSource?>.Add(IGraphicsEffectSource? item)
|
||||
{
|
||||
default(NotSupportedException).Throw();
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
int IList.Add(object? value)
|
||||
{
|
||||
return default(NotSupportedException).Throw<int>();
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
void ICollection<IGraphicsEffectSource?>.Clear()
|
||||
{
|
||||
default(NotSupportedException).Throw();
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
void IList.Clear()
|
||||
{
|
||||
default(NotSupportedException).Throw();
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
bool ICollection<IGraphicsEffectSource?>.Contains(IGraphicsEffectSource? item)
|
||||
{
|
||||
return FixedCountList<IGraphicsEffectSource?>.IndexOf(this, item) != -1;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
bool IList.Contains(object? value)
|
||||
{
|
||||
return FixedCountList<IGraphicsEffectSource?>.IndexOf(this, (IGraphicsEffectSource?)value) != -1;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
void ICollection<IGraphicsEffectSource?>.CopyTo(IGraphicsEffectSource?[] array, int arrayIndex)
|
||||
{
|
||||
FixedCountList<IGraphicsEffectSource?>.CopyTo(this, array, arrayIndex);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
void ICollection.CopyTo(Array array, int index)
|
||||
{
|
||||
FixedCountList<IGraphicsEffectSource?>.CopyTo(this, array, index);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
IEnumerator<IGraphicsEffectSource?> IEnumerable<IGraphicsEffectSource?>.GetEnumerator()
|
||||
{
|
||||
return FixedCountList<IGraphicsEffectSource?>.GetEnumerator(this);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
IEnumerator IEnumerable.GetEnumerator()
|
||||
{
|
||||
return FixedCountList<IGraphicsEffectSource?>.GetEnumerator(this);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
int IList<IGraphicsEffectSource?>.IndexOf(IGraphicsEffectSource? item)
|
||||
{
|
||||
return FixedCountList<IGraphicsEffectSource?>.IndexOf(this, item);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
int IList.IndexOf(object? value)
|
||||
{
|
||||
return FixedCountList<IGraphicsEffectSource?>.IndexOf(this, (IGraphicsEffectSource?)value);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
void IList<IGraphicsEffectSource?>.Insert(int index, IGraphicsEffectSource? item)
|
||||
{
|
||||
default(NotSupportedException).Throw();
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
void IList.Insert(int index, object? value)
|
||||
{
|
||||
default(NotSupportedException).Throw();
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
bool ICollection<IGraphicsEffectSource?>.Remove(IGraphicsEffectSource? item)
|
||||
{
|
||||
return default(NotSupportedException).Throw<bool>();
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
void IList.Remove(object? value)
|
||||
{
|
||||
default(NotSupportedException).Throw();
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
void IList<IGraphicsEffectSource?>.RemoveAt(int index)
|
||||
{
|
||||
default(NotSupportedException).Throw();
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
void IList.RemoveAt(int index)
|
||||
{
|
||||
default(NotSupportedException).Throw();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the collection of valid indices for the current effect.
|
||||
/// </summary>
|
||||
/// <returns>The collection of valid indices for the current effect.</returns>
|
||||
private static ImmutableArray<int> GetIndices()
|
||||
{
|
||||
int inputCount = T.InputCount;
|
||||
|
||||
if (inputCount == 0)
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
int[] indices = new int[inputCount];
|
||||
|
||||
for (int i = 0; i < indices.Length; i++)
|
||||
{
|
||||
indices[i] = i;
|
||||
}
|
||||
|
||||
return ImmutableCollectionsMarshal.AsImmutableArray(indices);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,57 +0,0 @@
|
||||
using System;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using ComputeSharp.D2D1.Interop;
|
||||
using Microsoft.Graphics.Canvas;
|
||||
|
||||
namespace ComputeSharp.D2D1.WinUI;
|
||||
|
||||
/// <inheritdoc/>
|
||||
partial class PixelShaderEffect<T>
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets or sets the current <typeparamref name="T"/> value in use.
|
||||
/// </summary>
|
||||
public T ConstantBuffer
|
||||
{
|
||||
get => GetConstantBuffer();
|
||||
set => SetConstantBuffer(in value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the <see cref="D2D1DrawTransformMapper{T}"/> instance to use for the effect, if any.
|
||||
/// </summary>
|
||||
/// <exception cref="ArgumentNullException">Thrown if the value is set to <see langword="null"/>.</exception>
|
||||
[DisallowNull]
|
||||
public D2D1DrawTransformMapper<T>? TransformMapper
|
||||
{
|
||||
get => GetTransformMapper();
|
||||
set => SetTransformMapper(value);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public bool CacheOutput
|
||||
{
|
||||
get => GetCacheOutput();
|
||||
set => SetCacheOutput(value);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public CanvasBufferPrecision? BufferPrecision
|
||||
{
|
||||
get => GetBufferPrecision();
|
||||
set => SetBufferPrecision(value);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public string? Name { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the <see cref="SourceCollection"/> object associated with the current instance.
|
||||
/// </summary>
|
||||
public SourceCollection Sources { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the <see cref="ResourceTextureManagerCollection"/> object associated with the current instance.
|
||||
/// </summary>
|
||||
public ResourceTextureManagerCollection ResourceTextureManagers { get; }
|
||||
}
|
||||
@@ -1,68 +0,0 @@
|
||||
using System;
|
||||
using ComputeSharp.Interop;
|
||||
using System.Runtime.CompilerServices;
|
||||
using ComputeSharp.D2D1.WinUI.Helpers;
|
||||
using ComputeSharp.Win32;
|
||||
|
||||
namespace ComputeSharp.D2D1.WinUI;
|
||||
|
||||
/// <inheritdoc/>
|
||||
partial class PixelShaderEffect<T>
|
||||
{
|
||||
/// <summary>
|
||||
/// Releases unmanaged resources and performs other cleanup operations.
|
||||
/// </summary>
|
||||
~PixelShaderEffect()
|
||||
{
|
||||
this.referenceTracker.Dispose();
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void Dispose()
|
||||
{
|
||||
this.referenceTracker.Dispose();
|
||||
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IReferenceTrackedObject.GetReferenceTracker"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
internal ref ReferenceTracker GetReferenceTracker()
|
||||
{
|
||||
return ref this.referenceTracker;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
ref ReferenceTracker IReferenceTrackedObject.GetReferenceTracker()
|
||||
{
|
||||
return ref this.referenceTracker;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
unsafe void IReferenceTrackedObject.DangerousOnDispose()
|
||||
{
|
||||
// When the effect is disposed, we also unregister it from the resource manager cache. This
|
||||
// is analogous to what is done when manually unrealizing the effect (eg. when using a new
|
||||
// device). Without this call, the resource manager would keep registered effects alive for
|
||||
// the entire process duration, even if their original wrappers are gone (ie. collected).
|
||||
if (this.d2D1Effect.Get() is not null)
|
||||
{
|
||||
ResourceManager.UnregisterWrapper((IUnknown*)this.d2D1Effect.Get());
|
||||
}
|
||||
|
||||
// Release the native D2D objects that were set during realization
|
||||
this.canvasDevice.Dispose();
|
||||
this.d2D1RealizationDevice.Dispose();
|
||||
this.d2D1Effect.Dispose();
|
||||
|
||||
// Also release all native resources for the used sources. The transform mapper and
|
||||
// resource texture managers don't need to be disposed, as those could be shared
|
||||
// between effects. Note that SourceReference.Dispose() will only release the native
|
||||
// resources (ie. D2D images and effects), but it will not also dispose the managed
|
||||
// wrappers (ie. IGraphicsEffectSource objects), as those can be shared as well.
|
||||
for (int i = 0; i < Sources.Count; i++)
|
||||
{
|
||||
Sources.Storage[i].Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,79 +0,0 @@
|
||||
using ABI.Microsoft.Graphics.Canvas;
|
||||
using ComputeSharp.D2D1.Descriptors;
|
||||
using ComputeSharp.D2D1.Interop;
|
||||
using ComputeSharp.Interop;
|
||||
using ComputeSharp.Win32;
|
||||
using Microsoft.Graphics.Canvas;
|
||||
using Microsoft.Graphics.Canvas.Effects;
|
||||
using ICanvasImageInterop = Microsoft.Graphics.Canvas.ICanvasImageInterop;
|
||||
|
||||
namespace ComputeSharp.D2D1.WinUI;
|
||||
|
||||
/// <summary>
|
||||
/// A custom <see cref="ICanvasEffect"/> implementation powered by a supplied shader type.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of shader to use to render frames.</typeparam>
|
||||
public sealed partial class PixelShaderEffect<T> : IReferenceTrackedObject, ICanvasEffect, ICanvasImageInterop
|
||||
where T : unmanaged, ID2D1PixelShader, ID2D1PixelShaderDescriptor<T>
|
||||
{
|
||||
/// <summary>
|
||||
/// The <see cref="ReferenceTracker"/> value for the current instance.
|
||||
/// </summary>
|
||||
private ReferenceTracker referenceTracker;
|
||||
|
||||
/// <summary>
|
||||
/// Lock object used to synchronize calls to <see cref="ICanvasImageInterop"/> APIs.
|
||||
/// </summary>
|
||||
private readonly object lockObject = new();
|
||||
|
||||
/// <summary>
|
||||
/// Flag to track whether a given call is recursively invoked by <see cref="ICanvasImageInterop.GetD2DImage"/>, to avoid graph cycles.
|
||||
/// </summary>
|
||||
private volatile int isInsideGetD2DImage;
|
||||
|
||||
/// <summary>
|
||||
/// The current device the effect is realized on, if one is available (this is the underlying COM object for <see cref="CanvasDevice"/>).
|
||||
/// </summary>
|
||||
private ComPtr<ICanvasDevice> canvasDevice;
|
||||
|
||||
/// <summary>
|
||||
/// The underlying D2D1 device the effect is realized on, if one is available (this is the D2D device backing <see cref="canvasDevice"/>).
|
||||
/// </summary>
|
||||
private ComPtr<ID2D1Device1> d2D1RealizationDevice;
|
||||
|
||||
/// <summary>
|
||||
/// The current realized <see cref="ID2D1Effect"/>, if one is available.
|
||||
/// </summary>
|
||||
private ComPtr<ID2D1Effect> d2D1Effect;
|
||||
|
||||
/// <summary>
|
||||
/// The backing field for <see cref="ConstantBuffer"/>.
|
||||
/// </summary>
|
||||
private T constantBuffer;
|
||||
|
||||
/// <summary>
|
||||
/// The backing field for <see cref="TransformMapper"/>.
|
||||
/// </summary>
|
||||
private D2D1DrawTransformMapper<T>? transformMapper;
|
||||
|
||||
/// <summary>
|
||||
/// The backing field for <see cref="CacheOutput"/>.
|
||||
/// </summary>
|
||||
private bool cacheOutput;
|
||||
|
||||
/// <summary>
|
||||
/// The backing field for <see cref="BufferPrecision"/>.
|
||||
/// </summary>
|
||||
private D2D1_BUFFER_PRECISION d2D1BufferPrecision;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="PixelShaderEffect{T}"/> instance.
|
||||
/// </summary>
|
||||
public PixelShaderEffect()
|
||||
{
|
||||
using ReferenceTracker.Lease _0 = ReferenceTracker.Create(this, out this.referenceTracker);
|
||||
|
||||
Sources = new SourceCollection(this);
|
||||
ResourceTextureManagers = new ResourceTextureManagerCollection(this);
|
||||
}
|
||||
}
|
||||
新しいイシューから参照
ユーザーをブロックする