Make ICanvasImageInterop bindings AOT safe

このコミットが含まれているのは:
Sergio Pedri committed 2024-06-28 15:36:25 -07:00
1 親 2af2849161
コミット 736610e741
8ファイル変更
+129 -117

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

+113 -94
ファイルの表示
@@ -7,113 +7,132 @@ 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
namespace ABI.Microsoft.Graphics.Canvas
{
/// <inheritdoc/>
static Guid* IComObject.IID
/// <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
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
/// <inheritdoc/>
public static Guid* IID
{
ReadOnlySpan<byte> data =
[
0xF7, 0xD1, 0x42, 0xE0,
0xAD, 0xF9,
0x79, 0x44,
0xA7, 0x13,
0x67,
0x62,
0x7E,
0xA3,
0x18,
0x63
];
[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));
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);
}
}
public void** lpVtbl;
/// <summary>
/// Gets the device that the effect is currently realized on, if any.
/// The ABI methods for <see cref="ICanvasImageInterop"/>.
/// </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)
[Guid("E042D1F7-F9AD-4479-A713-67627EA31863")]
internal static unsafe class ICanvasImageInteropMethods
{
return ((delegate* unmanaged[MemberFunction]<ICanvasImageInterop*, ICanvasDevice**, WIN2D_GET_DEVICE_ASSOCIATION_TYPE*, int>)this.lpVtbl[3])((ICanvasImageInterop*)Unsafe.AsPointer(ref this), device, type);
/// <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>
/// Gets an <see cref="ID2D1Image"/> from an <see cref="Interface"/> 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 managed implementation of <see cref="ICanvasImageInterop"/>.
/// The managed implementation of <see cref="ABI.Microsoft.Graphics.Canvas.ICanvasImageInterop"/>.
/// </summary>
[Guid("E042D1F7-F9AD-4479-A713-67627EA31863")]
[WindowsRuntimeType]
[WindowsRuntimeHelperType(typeof(Interface))]
public interface Interface
[WindowsRuntimeHelperType(typeof(ICanvasImageInterop))]
internal unsafe interface ICanvasImageInterop
{
/// <inheritdoc cref="ICanvasImageInterop.GetDevice"/>
/// <inheritdoc cref="ABI.Microsoft.Graphics.Canvas.ICanvasImageInterop.GetDevice"/>
[return: NativeTypeName("HRESULT")]
int GetDevice(ICanvasDevice** device, WIN2D_GET_DEVICE_ASSOCIATION_TYPE* type);
/// <inheritdoc cref="ICanvasImageInterop.GetD2DImage"/>
/// <inheritdoc cref="ABI.Microsoft.Graphics.Canvas.ICanvasImageInterop.GetD2DImage"/>
[return: NativeTypeName("HRESULT")]
int GetD2DImage(
ICanvasDevice* device,
@@ -124,7 +143,7 @@ internal unsafe struct ICanvasImageInterop : IComObject
ID2D1Image** ppImage);
/// <summary>
/// The vtable type for <see cref="Interface"/>.
/// The vtable type for <see cref="ICanvasImageInterop"/>.
/// </summary>
[Guid("E042D1F7-F9AD-4479-A713-67627EA31863")]
public struct Vftbl
@@ -135,9 +154,9 @@ internal unsafe struct ICanvasImageInterop : IComObject
public static readonly IntPtr AbiToProjectionVftablePtr = InitVtbl();
/// <summary>
/// Builds the custom method table pointer for <see cref="Interface"/>.
/// Builds the custom method table pointer for <see cref="ICanvasImageInterop"/>.
/// </summary>
/// <returns>The method table pointer for <see cref="Interface"/>.</returns>
/// <returns>The method table pointer for <see cref="ICanvasImageInterop"/>.</returns>
private static IntPtr InitVtbl()
{
Vftbl* lpVtbl = (Vftbl*)ComWrappersSupport.AllocateVtableMemory(typeof(Vftbl), sizeof(Vftbl));
@@ -164,14 +183,14 @@ internal unsafe struct ICanvasImageInterop : IComObject
/// </summary>
private delegate* unmanaged[MemberFunction]<IntPtr, ICanvasDevice*, ID2D1DeviceContext*, WIN2D_GET_D2D_IMAGE_FLAGS, float, float*, ID2D1Image**, int> GetD2DImage;
/// <inheritdoc cref="Interface.GetDevice"/>
/// <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<Interface>(thisPtr).GetDevice(device, type);
return ComWrappersSupport.FindObject<ICanvasImageInterop>(thisPtr).GetDevice(device, type);
}
catch (Exception e)
{
@@ -181,7 +200,7 @@ internal unsafe struct ICanvasImageInterop : IComObject
}
}
/// <inheritdoc cref="Interface.GetDevice"/>
/// <inheritdoc cref="ICanvasImageInterop.GetDevice"/>
[UnmanagedCallersOnly(CallConvs = [typeof(CallConvMemberFunction)])]
[return: NativeTypeName("HRESULT")]
private static int GetD2DImageFromAbi(
@@ -195,7 +214,7 @@ internal unsafe struct ICanvasImageInterop : IComObject
{
try
{
return ComWrappersSupport.FindObject<Interface>(thisPtr).GetD2DImage(device, deviceContext, flags, targetDpi, realizeDpi, ppImage);
return ComWrappersSupport.FindObject<ICanvasImageInterop>(thisPtr).GetD2DImage(device, deviceContext, flags, targetDpi, realizeDpi, ppImage);
}
catch (Exception e)
{
+1 -1
ファイルの表示
@@ -3,7 +3,7 @@ using System;
namespace ABI.Microsoft.Graphics.Canvas;
/// <summary>
/// Options for fine-tuning the behavior of <see cref="ICanvasImageInterop.Interface.GetD2DImage"/>.
/// Options for fine-tuning the behavior of <see cref="ICanvasImageInterop.GetD2DImage"/>.
/// </summary>
[Flags]
internal enum WIN2D_GET_D2D_IMAGE_FLAGS : uint
@@ -3,7 +3,7 @@ using System;
namespace ABI.Microsoft.Graphics.Canvas;
/// <summary>
/// Options for fine-tuning the behavior of <see cref="ICanvasImageInterop.Interface.GetDevice"/>.
/// Options for fine-tuning the behavior of <see cref="ICanvasImageInterop.GetDevice"/>.
/// </summary>
[Flags]
internal enum WIN2D_GET_DEVICE_ASSOCIATION_TYPE : uint
+1 -1
ファイルの表示
@@ -41,7 +41,7 @@ namespace ComputeSharp.D2D1.WinUI.Buffers;
internal unsafe struct SourceReference : IDisposable
{
/// <summary>
/// The <see cref="ID2D1Image"/> produced by calling <see cref="ICanvasImageInterop.Interface.GetD2DImage"/>
/// 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;
+5 -4
ファイルの表示
@@ -7,6 +7,7 @@ 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;
@@ -27,9 +28,9 @@ partial class CanvasEffect
}
/// <inheritdoc/>
unsafe int ICanvasImageInterop.Interface.GetDevice(ICanvasDevice** device, WIN2D_GET_DEVICE_ASSOCIATION_TYPE* type)
unsafe int ICanvasImageInterop.GetDevice(ICanvasDevice** device, WIN2D_GET_DEVICE_ASSOCIATION_TYPE* type)
{
using ComPtr<ICanvasImageInterop> canvasImageInterop = default;
using ComPtr<global::ABI.Microsoft.Graphics.Canvas.ICanvasImageInterop> canvasImageInterop = default;
RcwMarshaller.GetNativeInterface(GetCanvasImage(), canvasImageInterop.GetAddressOf()).Assert();
@@ -37,7 +38,7 @@ partial class CanvasEffect
}
/// <inheritdoc/>
unsafe int ICanvasImageInterop.Interface.GetD2DImage(
unsafe int ICanvasImageInterop.GetD2DImage(
ICanvasDevice* device,
ID2D1DeviceContext* deviceContext,
WIN2D_GET_D2D_IMAGE_FLAGS flags,
@@ -45,7 +46,7 @@ partial class CanvasEffect
float* realizeDpi,
ID2D1Image** ppImage)
{
using ComPtr<ICanvasImageInterop> canvasImageInterop = default;
using ComPtr<global::ABI.Microsoft.Graphics.Canvas.ICanvasImageInterop> canvasImageInterop = default;
RcwMarshaller.GetNativeInterface(GetCanvasImage(), canvasImageInterop.GetAddressOf()).Assert();
+1 -2
ファイルの表示
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using ABI.Microsoft.Graphics.Canvas;
using Microsoft.Graphics.Canvas;
namespace ComputeSharp.D2D1.WinUI;
@@ -9,7 +8,7 @@ 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.Interface
public abstract partial class CanvasEffect : ICanvasImage, ICanvasImageInterop
{
/// <summary>
/// The mapping of registered transform nodes for the current effect graph.
+4 -3
ファイルの表示
@@ -11,6 +11,7 @@ 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;
@@ -18,7 +19,7 @@ namespace ComputeSharp.D2D1.WinUI;
unsafe partial class PixelShaderEffect<T>
{
/// <inheritdoc/>
int ICanvasImageInterop.Interface.GetDevice(ICanvasDevice** device, WIN2D_GET_DEVICE_ASSOCIATION_TYPE* type)
int ICanvasImageInterop.GetDevice(ICanvasDevice** device, WIN2D_GET_DEVICE_ASSOCIATION_TYPE* type)
{
// Validate all input parameters
if (device is null || type is null)
@@ -71,7 +72,7 @@ unsafe partial class PixelShaderEffect<T>
}
/// <inheritdoc/>
int ICanvasImageInterop.Interface.GetD2DImage(
int ICanvasImageInterop.GetD2DImage(
ICanvasDevice* device,
ID2D1DeviceContext* deviceContext,
WIN2D_GET_D2D_IMAGE_FLAGS flags,
@@ -361,7 +362,7 @@ unsafe partial class PixelShaderEffect<T>
}
else
{
using ComPtr<ICanvasImageInterop> canvasImageInterop = default;
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();
+3 -11
ファイルの表示
@@ -1,4 +1,3 @@
using System.Diagnostics.CodeAnalysis;
using ABI.Microsoft.Graphics.Canvas;
using ComputeSharp.D2D1.Descriptors;
using ComputeSharp.D2D1.Interop;
@@ -6,6 +5,7 @@ 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;
@@ -13,7 +13,7 @@ namespace ComputeSharp.D2D1.WinUI;
/// 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.Interface
public sealed partial class PixelShaderEffect<T> : IReferenceTrackedObject, ICanvasEffect, ICanvasImageInterop
where T : unmanaged, ID2D1PixelShader, ID2D1PixelShaderDescriptor<T>
{
/// <summary>
@@ -27,7 +27,7 @@ public sealed partial class PixelShaderEffect<T> : IReferenceTrackedObject, ICan
private readonly object lockObject = new();
/// <summary>
/// Flag to track whether a given call is recursively invoked by <see cref="ICanvasImageInterop.Interface.GetD2DImage"/>, to avoid graph cycles.
/// Flag to track whether a given call is recursively invoked by <see cref="ICanvasImageInterop.GetD2DImage"/>, to avoid graph cycles.
/// </summary>
private volatile int isInsideGetD2DImage;
@@ -69,14 +69,6 @@ public sealed partial class PixelShaderEffect<T> : IReferenceTrackedObject, ICan
/// <summary>
/// Creates a new <see cref="PixelShaderEffect{T}"/> instance.
/// </summary>
// Workaround for trimming bug in custom COM/WinRT components with CsWinRT. Without manually preserving metadata for
// these types, using them will throw an InvalidCastException (see https://github.com/microsoft/CsWinRT/issues/1319).
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(ICanvasImageInterop.Interface))]
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(ICanvasImageInterop.Interface.Vftbl))]
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(ICanvasFactoryNative.Interface))]
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(ICanvasFactoryNative.Interface.Vftbl))]
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(ICanvasEffectFactoryNative.Interface))]
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(ICanvasEffectFactoryNative.Interface.Vftbl))]
public PixelShaderEffect()
{
using ReferenceTracker.Lease _0 = ReferenceTracker.Create(this, out this.referenceTracker);