Support CsWinRT CCW marshalling in WinUI project

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

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

+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());
}
}