Switch ICanvasEffectFactoryNative to COM interface

このコミットが含まれているのは:
Sergio Pedri committed 2023-04-22 15:42:47 +02:00
1 親 5ca2792892
コミット 6c17745c5d
6ファイル変更
+171 -75

検索条件に一致するファイルはありません

+81 -1
ファイルの表示
@@ -4,7 +4,11 @@ using System.Runtime.InteropServices;
using TerraFX.Interop;
using TerraFX.Interop.DirectX;
using TerraFX.Interop.Windows;
using TerraFX.Interop.WinRT;
#if !WINDOWS_UWP
using WinRT;
using WinRT.Interop;
#endif
using IInspectable = TerraFX.Interop.WinRT.IInspectable;
#pragma warning disable CS0649, IDE1006
@@ -69,4 +73,80 @@ internal unsafe struct ICanvasEffectFactoryNative
dpi,
wrapper);
}
/// <summary>
/// The managed implementation of <see cref="ICanvasEffectFactoryNative"/>.
/// </summary>
[Guid("29BA1A1F-1CFE-44C3-984D-426D61B51427")]
#if WINDOWS_UWP
[ComImport]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
#else
[WindowsRuntimeType]
[WindowsRuntimeHelperType(typeof(Interface))]
#endif
public interface Interface
{
/// <inheritdoc cref="ICanvasEffectFactoryNative.CreateWrapper"/>
#if WINDOWS_UWP
[PreserveSig]
#endif
[return: NativeTypeName("HRESULT")]
int CreateWrapper(ICanvasDevice* device, ID2D1Effect* resource, float dpi, IInspectable** wrapper);
#if !WINDOWS_UWP
/// <summary>
/// The vtable type for <see cref="Interface"/>.
/// </summary>
[Guid("29BA1A1F-1CFE-44C3-984D-426D61B51427")]
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="Interface"/>.
/// </summary>
/// <returns>The method table pointer for <see cref="Interface"/>.</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>
internal IUnknownVftbl IUnknownVftbl;
/// <summary>
/// Function pointer for <see cref="CreateWrapperFromAbi"/>.
/// </summary>
internal delegate* unmanaged[Stdcall]<IntPtr, ICanvasDevice*, ID2D1Effect*, float, IInspectable**, int> CreateWrapper;
/// <inheritdoc cref="Interface.CreateWrapper"/>
[UnmanagedCallersOnly(CallConvs = new[] { typeof(CallConvStdcall) })]
[return: NativeTypeName("HRESULT")]
private static int CreateWrapperFromAbi(IntPtr thisPtr, ICanvasDevice* device, ID2D1Effect* resource, float dpi, IInspectable** wrapper)
{
try
{
return ComWrappersSupport.FindObject<Interface>(thisPtr).CreateWrapper(device, resource, dpi, wrapper);
}
catch (Exception e)
{
ExceptionHelpers.SetErrorInfo(e);
return Marshal.GetHRForException(e);
}
}
}
#endif
}
}
+1 -1
ファイルの表示
@@ -38,7 +38,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Helpers\ResourceManager.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Loaders\D2D1ImmutableArrayResourceTextureDescriptionsLoader.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Loaders\D2D1IndexBitmaskResourceTextureDescriptionsLoader.cs" />
<Compile Include="$(MSBuildThisFileDirectory)PixelShaderEffect{T}.ICanvasEffectFactoryNative.cs" />
<Compile Include="$(MSBuildThisFileDirectory)PixelShaderEffect{T}.EffectFactoryManager.cs" />
<Compile Include="$(MSBuildThisFileDirectory)PixelShaderEffect{T}.cs" />
<Compile Include="$(MSBuildThisFileDirectory)PixelShaderEffect{T}.ICanvasEffect.cs" />
<Compile Include="$(MSBuildThisFileDirectory)PixelShaderEffect{T}.ICanvasImage.cs" />
+85
ファイルの表示
@@ -0,0 +1,85 @@
using System;
using ABI.Microsoft.Graphics.Canvas;
using ComputeSharp.D2D1.Extensions;
#if WINDOWS_UWP
using ComputeSharp.D2D1.Uwp.Extensions;
using ComputeSharp.D2D1.Uwp.Helpers;
#else
using ComputeSharp.D2D1.WinUI.Extensions;
using ComputeSharp.D2D1.WinUI.Helpers;
#endif
using TerraFX.Interop.DirectX;
using TerraFX.Interop.Windows;
using TerraFX.Interop.WinRT;
#if WINDOWS_UWP
namespace ComputeSharp.D2D1.Uwp;
#else
namespace ComputeSharp.D2D1.WinUI;
#endif
/// <inheritdoc/>
unsafe partial class PixelShaderEffect<T>
{
/// <summary>
/// A manager type to handle the effect factory logic and registration for <see cref="ICanvasEffectFactoryNative"/>.
/// </summary>
private sealed class EffectFactoryManager : ICanvasEffectFactoryNative.Interface
{
/// <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 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();
public void EnsureEffectFactoryIsRegistered()
{
lock (this.lockObject)
{
if (!this.isEffectFactoryRegistered)
{
// TODO
this.isEffectFactoryRegistered = true;
}
}
}
/// <inheritdoc/>
int ICanvasEffectFactoryNative.Interface.CreateWrapper(ICanvasDevice* device, ID2D1Effect* resource, float dpi, IInspectable** wrapper)
{
PixelShaderEffect<T> @this = new();
using (ComPtr<ID2D1Device1> d2D1Device1 = default)
{
// Get the underlying ID2D1Device1 instance for the input device
device->GetD2DDevice(d2D1Device1.GetAddressOf()).Assert();
// Initialize the effect state
device->CopyTo(ref @this.canvasDevice).Assert();
d2D1Device1.CopyTo(ref @this.d2D1RealizationDevice).Assert();
resource->CopyTo(ref @this.d2D1Effect).Assert();
}
RcwMarshaller.GetNativeObject(@this, wrapper);
return S.S_OK;
}
}
}
-72
ファイルの表示
@@ -1,72 +0,0 @@
using System;
using System.Runtime.InteropServices;
using ABI.Microsoft.Graphics.Canvas;
using ComputeSharp.D2D1.Extensions;
#if WINDOWS_UWP
using ComputeSharp.D2D1.Uwp.Extensions;
using ComputeSharp.D2D1.Uwp.Helpers;
#else
using ComputeSharp.D2D1.WinUI.Extensions;
using ComputeSharp.D2D1.WinUI.Helpers;
#endif
using TerraFX.Interop;
using TerraFX.Interop.DirectX;
using TerraFX.Interop.Windows;
using TerraFX.Interop.WinRT;
#if WINDOWS_UWP
namespace ComputeSharp.D2D1.Uwp;
#else
namespace ComputeSharp.D2D1.WinUI;
#endif
/// <inheritdoc/>
unsafe partial class PixelShaderEffect<T>
{
/// <inheritdoc cref="ICanvasEffectFactoryNative.CreateWrapper"/>
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
[return: NativeTypeName("HRESULT")]
private delegate int CreateWrapperDelegate(ICanvasDevice* device, ID2D1Effect* resource, float dpi, IInspectable** wrapper);
/// <summary>
/// A cached <see cref="CreateWrapperDelegate"/> instance wrapping <see cref="CreateWrapper"/>.
/// </summary>
private static readonly CreateWrapperDelegate CreateWrapperWrapper = CreateWrapper;
/// <summary>
/// Creates a new <see cref="PixelShaderEffect{T}"/> instance for an input D2D effect and marshals it as an <see cref="IInspectable"/> wrapper.
/// </summary>
/// <param name="device"><inheritdoc cref="ICanvasEffectFactoryNative.CreateWrapper" path="/param[@name='device']"/></param>
/// <param name="resource"><inheritdoc cref="ICanvasEffectFactoryNative.CreateWrapper" path="/param[@name='resource']"/></param>
/// <param name="dpi"><inheritdoc cref="ICanvasEffectFactoryNative.CreateWrapper" path="/param[@name='dpi']"/></param>
/// <param name="wrapper"><inheritdoc cref="ICanvasEffectFactoryNative.CreateWrapper" path="/param[@name='wrapper']"/></param>
/// <returns>This method always returns <see cref="S.S_OK"/> (any exceptions will be marshalled by the delegate stub).</returns>
private static int CreateWrapper(ICanvasDevice* device, ID2D1Effect* resource, float dpi, IInspectable** wrapper)
{
PixelShaderEffect<T> @this = new();
@this.InitializeFromNativeResource(device, resource);
RcwMarshaller.GetNativeObject(@this, wrapper);
return S.S_OK;
}
/// <summary>
/// Initializes a <see cref="PixelShaderEffect{T}"/> instance from an external realized D2D1 effect.
/// </summary>
/// <param name="device"><inheritdoc cref="ICanvasEffectFactoryNative.CreateWrapper" path="/param[@name='device']"/></param>
/// <param name="resource"><inheritdoc cref="ICanvasEffectFactoryNative.CreateWrapper" path="/param[@name='resource']"/></param>
private void InitializeFromNativeResource(ICanvasDevice* device, ID2D1Effect* resource)
{
using ComPtr<ID2D1Device1> d2D1Device1 = default;
// Get the underlying ID2D1Device1 instance for the input device
device->GetD2DDevice(d2D1Device1.GetAddressOf()).Assert();
// Initialize the effect state
device->CopyTo(ref this.canvasDevice).Assert();
d2D1Device1.CopyTo(ref this.d2D1RealizationDevice).Assert();
resource->CopyTo(ref this.d2D1Effect).Assert();
}
}
+3
ファイルの表示
@@ -347,6 +347,9 @@ unsafe partial class PixelShaderEffect<T>
// Register the new realized effect
ResourceManager.RegisterWrapper((IUnknown*)this.d2D1Effect.Get(), this);
// Register the effect factory, if necessary
EffectFactoryManager.Instance.EnsureEffectFactoryIsRegistered();
return true;
}
+1 -1
ファイルの表示
@@ -154,7 +154,7 @@ internal unsafe partial struct PixelShaderEffect
}
/// <summary>
/// Gets the shader <see cref="For{T}"/> instance.
/// Gets the shared <see cref="For{T}"/> instance.
/// </summary>
public static For<T> Instance { get; } = CreateInstance();