Support CsWinRT CCW marshalling in WinUI project

このコミットが含まれているのは:
Sergio Pedri committed 2023-04-20 20:07:19 +02:00
1 親 df8a4c250b
コミット 2578cf3028
7ファイル変更
+35 -20

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

+2 -2
ファイルの表示
@@ -39,7 +39,7 @@ unsafe partial class CanvasEffect
{
using ComPtr<ICanvasImageInterop> canvasImageInterop = default;
RcwMarshaller.QueryInterface(GetCanvasImage(), canvasImageInterop.GetAddressOf()).Assert();
RcwMarshaller.GetNativeObject(GetCanvasImage(), canvasImageInterop.GetAddressOf()).Assert();
return canvasImageInterop.Get()->GetDevice(device, type);
}
@@ -55,7 +55,7 @@ unsafe partial class CanvasEffect
{
using ComPtr<ICanvasImageInterop> canvasImageInterop = default;
RcwMarshaller.QueryInterface(GetCanvasImage(), canvasImageInterop.GetAddressOf()).Assert();
RcwMarshaller.GetNativeObject(GetCanvasImage(), canvasImageInterop.GetAddressOf()).Assert();
return canvasImageInterop.Get()->GetD2DImage(device, deviceContext, flags, targetDpi, realizeDpi, ppImage);
}
+24 -6
ファイルの表示
@@ -1,3 +1,4 @@
using System;
#if WINDOWS_UWP
using System.Runtime.InteropServices;
#endif
@@ -18,14 +19,33 @@ namespace ComputeSharp.D2D1.WinUI.Helpers;
internal static class RcwMarshaller
{
/// <summary>
/// Retrieves the underlying native object for an input RCW and casts it to the specified type.
/// 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>
public static unsafe T GetOrCreateManagedObject<T>(IUnknown* nativeObject)
where T : class
{
#if WINDOWS_UWP
// On UWP, Marshal.GetObjectForIUnknown handles all the marshalling/wrapping logic
return (T)Marshal.GetObjectForIUnknown((IntPtr)nativeObject);
#else
return MarshalInspectable<T>.FromAbi((IntPtr)nativeObject);
#endif
}
/// <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>
public static unsafe HRESULT QueryInterface<T>(object managedObject, T** nativeObject)
where T : unmanaged // IUnknown
public static unsafe HRESULT GetNativeObject<TFrom, TTo>(TFrom managedObject, TTo** nativeObject)
where TFrom : class
where TTo : unmanaged // IUnknown
{
using ComPtr<IUnknown> unknownObject = default;
@@ -34,9 +54,7 @@ internal static class RcwMarshaller
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());
unknownObject.Attach((IUnknown*)MarshalInspectable<TFrom>.FromManaged(managedObject));
#endif
// QueryInterface to the specific interface we need
+1 -4
ファイルの表示
@@ -1,5 +1,4 @@
using System;
using System.Runtime.InteropServices;
using ABI.Microsoft.Graphics.Canvas;
using ComputeSharp.D2D1.Extensions;
using TerraFX.Interop.Windows;
@@ -67,8 +66,6 @@ internal static class ResourceManager
wrapper: wrapperUnknown.GetVoidAddressOf()).Assert();
// Get the runtime-provided RCW for the resulting WinRT wrapper
object wrapper = Marshal.GetObjectForIUnknown((IntPtr)wrapperUnknown.Get());
return (IGraphicsEffectSource)wrapper;
return RcwMarshaller.GetOrCreateManagedObject<IGraphicsEffectSource>(wrapperUnknown.Get());
}
}
+4 -4
ファイルの表示
@@ -101,7 +101,7 @@ unsafe partial class PixelShaderEffect<T>
using ComPtr<ABI.Microsoft.Graphics.Canvas.Effects.ICanvasEffect> canvasEffectAbi = default;
// Get the ABI.Microsoft.Graphics.Canvas.Effects.ICanvasEffect object from the input interface
RcwMarshaller.QueryInterface(sourceEffect, canvasEffectAbi.GetAddressOf()).Assert();
RcwMarshaller.GetNativeObject(sourceEffect, canvasEffectAbi.GetAddressOf()).Assert();
Win2D.GetRequiredSourceRectanglesForICanvasImageInterop(
resourceCreator: resourceCreatorWithDpi.Get(),
@@ -155,7 +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++)
{
RcwMarshaller.QueryInterface(sourceEffects[i], canvasEffects[i].GetAddressOf()).Assert();
RcwMarshaller.GetNativeObject(sourceEffects[i], canvasEffects[i].GetAddressOf()).Assert();
}
Rect[] result = new Rect[sourceEffects.Length];
@@ -210,9 +210,9 @@ unsafe partial class PixelShaderEffect<T>
ICanvasImageInterop** canvasImageInterop)
{
// Get the ABI.Microsoft.Graphics.Canvas.ICanvasResourceCreatorWithDpi object from the input interface
RcwMarshaller.QueryInterface(resourceCreator, resourceCreatorWithDpi).Assert();
RcwMarshaller.GetNativeObject(resourceCreator, resourceCreatorWithDpi).Assert();
// Get the ICanvasImageInterop object from the current instance
RcwMarshaller.QueryInterface(this, canvasImageInterop).Assert();
RcwMarshaller.GetNativeObject(this, canvasImageInterop).Assert();
}
}
+2 -2
ファイルの表示
@@ -40,12 +40,12 @@ unsafe partial class PixelShaderEffect<T>
using ComPtr<ABI.Microsoft.Graphics.Canvas.ICanvasResourceCreator> resourceCreatorAbi = default;
// Get the ABI.Microsoft.Graphics.Canvas.ICanvasResourceCreator object from the input interface
RcwMarshaller.QueryInterface(resourceCreator, resourceCreatorAbi.GetAddressOf()).Assert();
RcwMarshaller.GetNativeObject(resourceCreator, resourceCreatorAbi.GetAddressOf()).Assert();
using ComPtr<ICanvasImageInterop> canvasImageInterop = default;
// Get the ICanvasImageInterop object from the current instance
RcwMarshaller.QueryInterface(this, canvasImageInterop.GetAddressOf()).Assert();
RcwMarshaller.GetNativeObject(this, canvasImageInterop.GetAddressOf()).Assert();
Rect bounds;
+1 -1
ファイルの表示
@@ -366,7 +366,7 @@ unsafe partial class PixelShaderEffect<T>
using ComPtr<ICanvasImageInterop> canvasImageInterop = default;
// Convert to ICanvasImageInterop (this must always succeed, and throws if it doesn't)
RcwMarshaller.QueryInterface(source, canvasImageInterop.GetAddressOf()).Assert();
RcwMarshaller.GetNativeObject(source, canvasImageInterop.GetAddressOf()).Assert();
using ComPtr<ID2D1Image> d2D1Image = default;
+1 -1
ファイルの表示
@@ -141,7 +141,7 @@ unsafe partial class PixelShaderEffect<T>
using ComPtr<ICanvasImageInterop> canvasImageInterop = default;
// Try to get the ICanvasImageInterop interface from the input source
HRESULT hresult = RcwMarshaller.QueryInterface(value, canvasImageInterop.GetAddressOf());
HRESULT hresult = RcwMarshaller.GetNativeObject(value, canvasImageInterop.GetAddressOf());
if (!Win32.SUCCEEDED(hresult))
{