Support CsWinRT RCW marshalling in WinUI project
このコミットが含まれているのは:
1 親
0eee72ff29
コミット
df8a4c250b
7ファイル変更
+75
-49
検索条件に一致するファイルはありません
@@ -1,8 +1,12 @@
|
||||
using System;
|
||||
using System.Numerics;
|
||||
using System.Runtime.InteropServices;
|
||||
using ABI.Microsoft.Graphics.Canvas;
|
||||
using ComputeSharp.D2D1.Extensions;
|
||||
#if WINDOWS_UWP
|
||||
using ComputeSharp.D2D1.Uwp.Helpers;
|
||||
#else
|
||||
using ComputeSharp.D2D1.WinUI.Helpers;
|
||||
#endif
|
||||
using Microsoft.Graphics.Canvas;
|
||||
using TerraFX.Interop.DirectX;
|
||||
using TerraFX.Interop.Windows;
|
||||
@@ -35,7 +39,7 @@ unsafe partial class CanvasEffect
|
||||
{
|
||||
using ComPtr<ICanvasImageInterop> canvasImageInterop = default;
|
||||
|
||||
GetCanvasImageInterop(canvasImageInterop.GetAddressOf());
|
||||
RcwMarshaller.QueryInterface(GetCanvasImage(), canvasImageInterop.GetAddressOf()).Assert();
|
||||
|
||||
return canvasImageInterop.Get()->GetDevice(device, type);
|
||||
}
|
||||
@@ -51,7 +55,7 @@ unsafe partial class CanvasEffect
|
||||
{
|
||||
using ComPtr<ICanvasImageInterop> canvasImageInterop = default;
|
||||
|
||||
GetCanvasImageInterop(canvasImageInterop.GetAddressOf());
|
||||
RcwMarshaller.QueryInterface(GetCanvasImage(), canvasImageInterop.GetAddressOf()).Assert();
|
||||
|
||||
return canvasImageInterop.Get()->GetD2DImage(device, deviceContext, flags, targetDpi, realizeDpi, ppImage);
|
||||
}
|
||||
@@ -79,17 +83,4 @@ unsafe partial class CanvasEffect
|
||||
return this.canvasImage;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the <see cref="ICanvasImageInterop"/> object currently in use.
|
||||
/// </summary>
|
||||
/// <param name="canvasImageInterop">A pointer to the <see cref="ICanvasImageInterop"/> object currently in use.</param>
|
||||
private void GetCanvasImageInterop(ICanvasImageInterop** canvasImageInterop)
|
||||
{
|
||||
using ComPtr<IUnknown> canvasImageUnknown = default;
|
||||
|
||||
canvasImageUnknown.Attach((IUnknown*)Marshal.GetIUnknownForObject(GetCanvasImage()));
|
||||
|
||||
canvasImageUnknown.CopyTo(canvasImageInterop).Assert();
|
||||
}
|
||||
}
|
||||
@@ -32,6 +32,7 @@
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Extensions\ID2D1DeviceContextExtensions.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Extensions\ID2D1EffectExtensions.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Extensions\IUnknownExtensions.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Helpers\RcwMarshaller.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Helpers\ResourceManager.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Loaders\D2D1ImmutableArrayResourceTextureDescriptionsLoader.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Loaders\D2D1IndexBitmaskResourceTextureDescriptionsLoader.cs" />
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
#if WINDOWS_UWP
|
||||
using System.Runtime.InteropServices;
|
||||
#endif
|
||||
using TerraFX.Interop.Windows;
|
||||
#if !WINDOWS_UWP
|
||||
using WinRT;
|
||||
#endif
|
||||
|
||||
#if WINDOWS_UWP
|
||||
namespace ComputeSharp.D2D1.Uwp.Helpers;
|
||||
#else
|
||||
namespace ComputeSharp.D2D1.WinUI.Helpers;
|
||||
#endif
|
||||
|
||||
/// <summary>
|
||||
/// A helper type to handle marshalling of RCW instances.
|
||||
/// </summary>
|
||||
internal static class RcwMarshaller
|
||||
{
|
||||
/// <summary>
|
||||
/// Retrieves the underlying native object for an input RCW and casts it to the specified type.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">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>
|
||||
public static unsafe HRESULT QueryInterface<T>(object managedObject, T** nativeObject)
|
||||
where T : unmanaged // IUnknown
|
||||
{
|
||||
using ComPtr<IUnknown> unknownObject = default;
|
||||
|
||||
#if WINDOWS_UWP
|
||||
// On UWP, due to built-in COM/WinRT support, Marshal.GetIUnknownForObject can handle all the logic
|
||||
unknownObject.Attach((IUnknown*)Marshal.GetIUnknownForObject(managedObject));
|
||||
#else
|
||||
// On WinUI 3, delegate the RCW unwrapping or CCW creation logic to CsWinRT's APIs
|
||||
ObjectReferenceValue objectReference = MarshalInspectable<object>.CreateMarshaler2(managedObject);
|
||||
|
||||
unknownObject.Attach((IUnknown*)objectReference.Detach());
|
||||
#endif
|
||||
|
||||
// QueryInterface to the specific interface we need
|
||||
return unknownObject.CopyTo(nativeObject);
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,11 @@ using System.Buffers;
|
||||
using System.Runtime.InteropServices;
|
||||
using ABI.Microsoft.Graphics.Canvas;
|
||||
using ComputeSharp.D2D1.Extensions;
|
||||
#if WINDOWS_UWP
|
||||
using ComputeSharp.D2D1.Uwp.Helpers;
|
||||
#else
|
||||
using ComputeSharp.D2D1.WinUI.Helpers;
|
||||
#endif
|
||||
using ComputeSharp.Interop;
|
||||
using TerraFX.Interop.Windows;
|
||||
using Windows.Foundation;
|
||||
@@ -93,12 +98,10 @@ unsafe partial class PixelShaderEffect<T>
|
||||
|
||||
Rect result;
|
||||
|
||||
using ComPtr<IUnknown> canvasEffectUnknown = default;
|
||||
using ComPtr<ABI.Microsoft.Graphics.Canvas.Effects.ICanvasEffect> canvasEffectAbi = default;
|
||||
|
||||
// Get the ABI.Microsoft.Graphics.Canvas.Effects.ICanvasEffect object from the input interface
|
||||
canvasEffectUnknown.Attach((IUnknown*)Marshal.GetIUnknownForObject(sourceEffect));
|
||||
canvasEffectUnknown.CopyTo(canvasEffectAbi.GetAddressOf()).Assert();
|
||||
RcwMarshaller.QueryInterface(sourceEffect, canvasEffectAbi.GetAddressOf()).Assert();
|
||||
|
||||
Win2D.GetRequiredSourceRectanglesForICanvasImageInterop(
|
||||
resourceCreator: resourceCreatorWithDpi.Get(),
|
||||
@@ -152,10 +155,7 @@ unsafe partial class PixelShaderEffect<T>
|
||||
// Get the underlying ABI.Microsoft.Graphics.Canvas.Effects.ICanvasEffect object for each input effect
|
||||
for (int i = 0; i < sourceEffects.Length; i++)
|
||||
{
|
||||
using ComPtr<IUnknown> canvasEffectUnknown = default;
|
||||
|
||||
canvasEffectUnknown.Attach((IUnknown*)Marshal.GetIUnknownForObject(sourceEffects[i]));
|
||||
canvasEffectUnknown.CopyTo(canvasEffects[i].GetAddressOf()).Assert();
|
||||
RcwMarshaller.QueryInterface(sourceEffects[i], canvasEffects[i].GetAddressOf()).Assert();
|
||||
}
|
||||
|
||||
Rect[] result = new Rect[sourceEffects.Length];
|
||||
@@ -209,16 +209,10 @@ unsafe partial class PixelShaderEffect<T>
|
||||
ABI.Microsoft.Graphics.Canvas.ICanvasResourceCreatorWithDpi** resourceCreatorWithDpi,
|
||||
ICanvasImageInterop** canvasImageInterop)
|
||||
{
|
||||
using ComPtr<IUnknown> resourceCreatorUnknown = default;
|
||||
|
||||
// Get the ABI.Microsoft.Graphics.Canvas.ICanvasResourceCreatorWithDpi object from the input interface
|
||||
resourceCreatorUnknown.Attach((IUnknown*)Marshal.GetIUnknownForObject(resourceCreator));
|
||||
resourceCreatorUnknown.CopyTo(resourceCreatorWithDpi).Assert();
|
||||
|
||||
using ComPtr<IUnknown> canvasImageInteropUnknown = default;
|
||||
RcwMarshaller.QueryInterface(resourceCreator, resourceCreatorWithDpi).Assert();
|
||||
|
||||
// Get the ICanvasImageInterop object from the current instance
|
||||
canvasImageInteropUnknown.Attach((IUnknown*)Marshal.GetIUnknownForObject(this));
|
||||
canvasImageInteropUnknown.CopyTo(canvasImageInterop).Assert();
|
||||
RcwMarshaller.QueryInterface(this, canvasImageInterop).Assert();
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,11 @@
|
||||
using System.Numerics;
|
||||
using System.Runtime.InteropServices;
|
||||
using ABI.Microsoft.Graphics.Canvas;
|
||||
using ComputeSharp.D2D1.Extensions;
|
||||
#if WINDOWS_UWP
|
||||
using ComputeSharp.D2D1.Uwp.Helpers;
|
||||
#else
|
||||
using ComputeSharp.D2D1.WinUI.Helpers;
|
||||
#endif
|
||||
using ComputeSharp.Interop;
|
||||
using TerraFX.Interop.Windows;
|
||||
using Windows.Foundation;
|
||||
@@ -33,19 +37,15 @@ unsafe partial class PixelShaderEffect<T>
|
||||
{
|
||||
using ReferenceTracker.Lease _0 = GetReferenceTracker().GetLease();
|
||||
|
||||
using ComPtr<IUnknown> resourceCreatorUnknown = default;
|
||||
using ComPtr<ABI.Microsoft.Graphics.Canvas.ICanvasResourceCreator> resourceCreatorAbi = default;
|
||||
|
||||
// Get the ABI.Microsoft.Graphics.Canvas.ICanvasResourceCreator object from the input interface
|
||||
resourceCreatorUnknown.Attach((IUnknown*)Marshal.GetIUnknownForObject(resourceCreator));
|
||||
resourceCreatorUnknown.CopyTo(resourceCreatorAbi.GetAddressOf()).Assert();
|
||||
RcwMarshaller.QueryInterface(resourceCreator, resourceCreatorAbi.GetAddressOf()).Assert();
|
||||
|
||||
using ComPtr<IUnknown> canvasImageInteropUnknown = default;
|
||||
using ComPtr<ICanvasImageInterop> canvasImageInterop = default;
|
||||
|
||||
// Get the ICanvasImageInterop object from the current instance
|
||||
canvasImageInteropUnknown.Attach((IUnknown*)Marshal.GetIUnknownForObject(this));
|
||||
canvasImageInteropUnknown.CopyTo(canvasImageInterop.GetAddressOf()).Assert();
|
||||
RcwMarshaller.QueryInterface(this, canvasImageInterop.GetAddressOf()).Assert();
|
||||
|
||||
Rect bounds;
|
||||
|
||||
|
||||
@@ -7,8 +7,10 @@ using ComputeSharp.D2D1.Interop;
|
||||
using ComputeSharp.D2D1.Interop.Effects;
|
||||
#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 ComputeSharp.Interop;
|
||||
using TerraFX.Interop.DirectX;
|
||||
@@ -361,14 +363,10 @@ unsafe partial class PixelShaderEffect<T>
|
||||
}
|
||||
else
|
||||
{
|
||||
using ComPtr<IUnknown> canvasImageInteropUnknown = default;
|
||||
using ComPtr<ICanvasImageInterop> canvasImageInterop = default;
|
||||
|
||||
// Get the underlying IUnknown object for the current source
|
||||
canvasImageInteropUnknown.Attach((IUnknown*)Marshal.GetIUnknownForObject(source));
|
||||
|
||||
// Convert to ICanvasImageInterop (this must always succeed, and throws if it doesn't)
|
||||
canvasImageInteropUnknown.CopyTo(canvasImageInterop.GetAddressOf()).Assert();
|
||||
RcwMarshaller.QueryInterface(source, canvasImageInterop.GetAddressOf()).Assert();
|
||||
|
||||
using ComPtr<ID2D1Image> d2D1Image = default;
|
||||
|
||||
|
||||
@@ -6,9 +6,11 @@ using ComputeSharp.D2D1.Extensions;
|
||||
#if WINDOWS_UWP
|
||||
using ComputeSharp.D2D1.Uwp.Buffers;
|
||||
using ComputeSharp.D2D1.Uwp.Extensions;
|
||||
using ComputeSharp.D2D1.Uwp.Helpers;
|
||||
#else
|
||||
using ComputeSharp.D2D1.WinUI.Buffers;
|
||||
using ComputeSharp.D2D1.WinUI.Extensions;
|
||||
using ComputeSharp.D2D1.WinUI.Helpers;
|
||||
#endif
|
||||
using ComputeSharp.Interop;
|
||||
using TerraFX.Interop.DirectX;
|
||||
@@ -136,15 +138,10 @@ unsafe partial class PixelShaderEffect<T>
|
||||
|
||||
if (value is not null)
|
||||
{
|
||||
using ComPtr<IUnknown> canvasImageUnknown = default;
|
||||
|
||||
// Get the underlying IUnknown object for the input source
|
||||
canvasImageUnknown.Attach((IUnknown*)Marshal.GetIUnknownForObject(value));
|
||||
|
||||
using ComPtr<ICanvasImageInterop> canvasImageInterop = default;
|
||||
|
||||
// Try to get the ICanvasImageInterop interface from the input source
|
||||
HRESULT hresult = canvasImageUnknown.CopyTo(canvasImageInterop.GetAddressOf());
|
||||
HRESULT hresult = RcwMarshaller.QueryInterface(value, canvasImageInterop.GetAddressOf());
|
||||
|
||||
if (!Win32.SUCCEEDED(hresult))
|
||||
{
|
||||
|
||||
新しいイシューから参照
ユーザーをブロックする