diff --git a/src/ComputeSharp.D2D1.WinUI/ABI.Microsoft.Graphics.Canvas/ICanvasImageInterop.cs b/src/ComputeSharp.D2D1.WinUI/ABI.Microsoft.Graphics.Canvas/ICanvasImageInterop.cs index 8cefb7c3..c094f01d 100644 --- a/src/ComputeSharp.D2D1.WinUI/ABI.Microsoft.Graphics.Canvas/ICanvasImageInterop.cs +++ b/src/ComputeSharp.D2D1.WinUI/ABI.Microsoft.Graphics.Canvas/ICanvasImageInterop.cs @@ -7,113 +7,132 @@ using WinRT.Interop; #pragma warning disable CS0649, IDE0055, IDE1006 -namespace ABI.Microsoft.Graphics.Canvas; - -/// -/// An interop Win2D interface for all images and effects that can be drawn. -/// -[NativeTypeName("class ICanvasImageInterop : IUnknown")] -[NativeInheritance("IUnknown")] -internal unsafe struct ICanvasImageInterop : IComObject +namespace ABI.Microsoft.Graphics.Canvas { - /// - static Guid* IComObject.IID + /// + /// An interop Win2D interface for all images and effects that can be drawn. + /// + [NativeTypeName("class ICanvasImageInterop : IUnknown")] + [NativeInheritance("IUnknown")] + internal unsafe struct ICanvasImageInterop : IComObject { - [MethodImpl(MethodImplOptions.AggressiveInlining)] - get + /// + public static Guid* IID { - ReadOnlySpan data = - [ - 0xF7, 0xD1, 0x42, 0xE0, - 0xAD, 0xF9, - 0x79, 0x44, - 0xA7, 0x13, - 0x67, - 0x62, - 0x7E, - 0xA3, - 0x18, - 0x63 - ]; + [MethodImpl(MethodImplOptions.AggressiveInlining)] + get + { + ReadOnlySpan 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; + + /// + /// Gets the device that the effect is currently realized on, if any. + /// + /// The resulting device, if available (as a marshalled ). + /// The value describing the returned instance. + /// The for the operation. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [VtblIndex(3)] + public HRESULT GetDevice(ICanvasDevice** device, WIN2D_GET_DEVICE_ASSOCIATION_TYPE* type) + { + return ((delegate* unmanaged[MemberFunction])this.lpVtbl[3])((ICanvasImageInterop*)Unsafe.AsPointer(ref this), device, type); + } + + /// + /// Gets an from an instance. + /// + /// The input canvas device (as a marshalled ). + /// + /// The device context in use. This value is is optional (but recommended), except when the + /// flag is specified. This is because + /// not all callers of 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. + /// + /// The flags to use to get the image. + /// + /// The DPI of the target device context. This is used to determine when a D2D1DpiCompensation + /// effect needs to be inserted. Behavior of this parameter can be overridden by the flag values + /// , + /// + /// or + /// + /// + /// The DPI of a source bitmap, or zero if the image does not have a fixed DPI. A D2D1DpiCompensation effect + /// will be inserted if and are different (flags permitting). + /// + /// The resulting for the effect. + /// The for the operation. + [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])this.lpVtbl[4])( + (ICanvasImageInterop*)Unsafe.AsPointer(ref this), + device, + deviceContext, + flags, + targetDpi, + realizeDpi, + ppImage); } } - public void** lpVtbl; - /// - /// Gets the device that the effect is currently realized on, if any. + /// The ABI methods for . /// - /// The resulting device, if available (as a marshalled ). - /// The value describing the returned instance. - /// The for the operation. - [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])this.lpVtbl[3])((ICanvasImageInterop*)Unsafe.AsPointer(ref this), device, type); + /// + public static Guid IID => *ICanvasImageInterop.IID; + + /// + public static IntPtr AbiToProjectionVftablePtr => global::Microsoft.Graphics.Canvas.ICanvasImageInterop.Vftbl.AbiToProjectionVftablePtr; } +} + +namespace Microsoft.Graphics.Canvas +{ + using ABI.Microsoft.Graphics.Canvas; /// - /// Gets an from an instance. - /// - /// The input canvas device (as a marshalled ). - /// - /// The device context in use. This value is is optional (but recommended), except when the - /// flag is specified. This is because - /// not all callers of 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. - /// - /// The flags to use to get the image. - /// - /// The DPI of the target device context. This is used to determine when a D2D1DpiCompensation - /// effect needs to be inserted. Behavior of this parameter can be overridden by the flag values - /// , - /// - /// or - /// - /// - /// The DPI of a source bitmap, or zero if the image does not have a fixed DPI. A D2D1DpiCompensation effect - /// will be inserted if and are different (flags permitting). - /// - /// The resulting for the effect. - /// The for the operation. - [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])this.lpVtbl[4])( - (ICanvasImageInterop*)Unsafe.AsPointer(ref this), - device, - deviceContext, - flags, - targetDpi, - realizeDpi, - ppImage); - } - - /// - /// The managed implementation of . + /// The managed implementation of . /// [Guid("E042D1F7-F9AD-4479-A713-67627EA31863")] [WindowsRuntimeType] - [WindowsRuntimeHelperType(typeof(Interface))] - public interface Interface + [WindowsRuntimeHelperType(typeof(ICanvasImageInterop))] + internal unsafe interface ICanvasImageInterop { - /// + /// [return: NativeTypeName("HRESULT")] int GetDevice(ICanvasDevice** device, WIN2D_GET_DEVICE_ASSOCIATION_TYPE* type); - /// + /// [return: NativeTypeName("HRESULT")] int GetD2DImage( ICanvasDevice* device, @@ -124,7 +143,7 @@ internal unsafe struct ICanvasImageInterop : IComObject ID2D1Image** ppImage); /// - /// The vtable type for . + /// The vtable type for . /// [Guid("E042D1F7-F9AD-4479-A713-67627EA31863")] public struct Vftbl @@ -135,9 +154,9 @@ internal unsafe struct ICanvasImageInterop : IComObject public static readonly IntPtr AbiToProjectionVftablePtr = InitVtbl(); /// - /// Builds the custom method table pointer for . + /// Builds the custom method table pointer for . /// - /// The method table pointer for . + /// The method table pointer for . private static IntPtr InitVtbl() { Vftbl* lpVtbl = (Vftbl*)ComWrappersSupport.AllocateVtableMemory(typeof(Vftbl), sizeof(Vftbl)); @@ -164,14 +183,14 @@ internal unsafe struct ICanvasImageInterop : IComObject /// private delegate* unmanaged[MemberFunction] GetD2DImage; - /// + /// [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(thisPtr).GetDevice(device, type); + return ComWrappersSupport.FindObject(thisPtr).GetDevice(device, type); } catch (Exception e) { @@ -181,7 +200,7 @@ internal unsafe struct ICanvasImageInterop : IComObject } } - /// + /// [UnmanagedCallersOnly(CallConvs = [typeof(CallConvMemberFunction)])] [return: NativeTypeName("HRESULT")] private static int GetD2DImageFromAbi( @@ -195,7 +214,7 @@ internal unsafe struct ICanvasImageInterop : IComObject { try { - return ComWrappersSupport.FindObject(thisPtr).GetD2DImage(device, deviceContext, flags, targetDpi, realizeDpi, ppImage); + return ComWrappersSupport.FindObject(thisPtr).GetD2DImage(device, deviceContext, flags, targetDpi, realizeDpi, ppImage); } catch (Exception e) { diff --git a/src/ComputeSharp.D2D1.WinUI/ABI.Microsoft.Graphics.Canvas/WIN2D_GET_D2D_IMAGE_FLAGS.cs b/src/ComputeSharp.D2D1.WinUI/ABI.Microsoft.Graphics.Canvas/WIN2D_GET_D2D_IMAGE_FLAGS.cs index 063bd8a6..0180f17b 100644 --- a/src/ComputeSharp.D2D1.WinUI/ABI.Microsoft.Graphics.Canvas/WIN2D_GET_D2D_IMAGE_FLAGS.cs +++ b/src/ComputeSharp.D2D1.WinUI/ABI.Microsoft.Graphics.Canvas/WIN2D_GET_D2D_IMAGE_FLAGS.cs @@ -3,7 +3,7 @@ using System; namespace ABI.Microsoft.Graphics.Canvas; /// -/// Options for fine-tuning the behavior of . +/// Options for fine-tuning the behavior of . /// [Flags] internal enum WIN2D_GET_D2D_IMAGE_FLAGS : uint diff --git a/src/ComputeSharp.D2D1.WinUI/ABI.Microsoft.Graphics.Canvas/WIN2D_GET_DEVICE_ASSOCIATION_TYPE.cs b/src/ComputeSharp.D2D1.WinUI/ABI.Microsoft.Graphics.Canvas/WIN2D_GET_DEVICE_ASSOCIATION_TYPE.cs index 9ce3c2e4..32452df2 100644 --- a/src/ComputeSharp.D2D1.WinUI/ABI.Microsoft.Graphics.Canvas/WIN2D_GET_DEVICE_ASSOCIATION_TYPE.cs +++ b/src/ComputeSharp.D2D1.WinUI/ABI.Microsoft.Graphics.Canvas/WIN2D_GET_DEVICE_ASSOCIATION_TYPE.cs @@ -3,7 +3,7 @@ using System; namespace ABI.Microsoft.Graphics.Canvas; /// -/// Options for fine-tuning the behavior of . +/// Options for fine-tuning the behavior of . /// [Flags] internal enum WIN2D_GET_DEVICE_ASSOCIATION_TYPE : uint diff --git a/src/ComputeSharp.D2D1.WinUI/Buffers/SourceReference.cs b/src/ComputeSharp.D2D1.WinUI/Buffers/SourceReference.cs index 28642405..a20583ca 100644 --- a/src/ComputeSharp.D2D1.WinUI/Buffers/SourceReference.cs +++ b/src/ComputeSharp.D2D1.WinUI/Buffers/SourceReference.cs @@ -41,7 +41,7 @@ namespace ComputeSharp.D2D1.WinUI.Buffers; internal unsafe struct SourceReference : IDisposable { /// - /// The produced by calling + /// The produced by calling /// on the input managed wrapper used as source for the effect (ie. ). /// private ComPtr d2D1ImageSource; diff --git a/src/ComputeSharp.D2D1.WinUI/CanvasEffect.Interop.cs b/src/ComputeSharp.D2D1.WinUI/CanvasEffect.Interop.cs index 5f4d8b0d..a539d160 100644 --- a/src/ComputeSharp.D2D1.WinUI/CanvasEffect.Interop.cs +++ b/src/ComputeSharp.D2D1.WinUI/CanvasEffect.Interop.cs @@ -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 } /// - 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 canvasImageInterop = default; + using ComPtr canvasImageInterop = default; RcwMarshaller.GetNativeInterface(GetCanvasImage(), canvasImageInterop.GetAddressOf()).Assert(); @@ -37,7 +38,7 @@ partial class CanvasEffect } /// - 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 canvasImageInterop = default; + using ComPtr canvasImageInterop = default; RcwMarshaller.GetNativeInterface(GetCanvasImage(), canvasImageInterop.GetAddressOf()).Assert(); diff --git a/src/ComputeSharp.D2D1.WinUI/CanvasEffect.cs b/src/ComputeSharp.D2D1.WinUI/CanvasEffect.cs index 4247f5d2..f9bd17c4 100644 --- a/src/ComputeSharp.D2D1.WinUI/CanvasEffect.cs +++ b/src/ComputeSharp.D2D1.WinUI/CanvasEffect.cs @@ -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; /// /// A base type to implement packaged and easy to use -based effects that can be used with Win2D. /// -public abstract partial class CanvasEffect : ICanvasImage, ICanvasImageInterop.Interface +public abstract partial class CanvasEffect : ICanvasImage, ICanvasImageInterop { /// /// The mapping of registered transform nodes for the current effect graph. diff --git a/src/ComputeSharp.D2D1.WinUI/PixelShaderEffect{T}.ICanvasImageInterop.cs b/src/ComputeSharp.D2D1.WinUI/PixelShaderEffect{T}.ICanvasImageInterop.cs index 0d729c98..0ac9a12c 100644 --- a/src/ComputeSharp.D2D1.WinUI/PixelShaderEffect{T}.ICanvasImageInterop.cs +++ b/src/ComputeSharp.D2D1.WinUI/PixelShaderEffect{T}.ICanvasImageInterop.cs @@ -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 { /// - 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 } /// - 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 } else { - using ComPtr canvasImageInterop = default; + using ComPtr canvasImageInterop = default; // Convert to ICanvasImageInterop (this must always succeed, and throws if it doesn't) RcwMarshaller.GetNativeInterface(source, canvasImageInterop.GetAddressOf()).Assert(); diff --git a/src/ComputeSharp.D2D1.WinUI/PixelShaderEffect{T}.cs b/src/ComputeSharp.D2D1.WinUI/PixelShaderEffect{T}.cs index 65e2ddc5..cae06b13 100644 --- a/src/ComputeSharp.D2D1.WinUI/PixelShaderEffect{T}.cs +++ b/src/ComputeSharp.D2D1.WinUI/PixelShaderEffect{T}.cs @@ -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 implementation powered by a supplied shader type. /// /// The type of shader to use to render frames. -public sealed partial class PixelShaderEffect : IReferenceTrackedObject, ICanvasEffect, ICanvasImageInterop.Interface +public sealed partial class PixelShaderEffect : IReferenceTrackedObject, ICanvasEffect, ICanvasImageInterop where T : unmanaged, ID2D1PixelShader, ID2D1PixelShaderDescriptor { /// @@ -27,7 +27,7 @@ public sealed partial class PixelShaderEffect : IReferenceTrackedObject, ICan private readonly object lockObject = new(); /// - /// Flag to track whether a given call is recursively invoked by , to avoid graph cycles. + /// Flag to track whether a given call is recursively invoked by , to avoid graph cycles. /// private volatile int isInsideGetD2DImage; @@ -69,14 +69,6 @@ public sealed partial class PixelShaderEffect : IReferenceTrackedObject, ICan /// /// Creates a new instance. /// - // 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);