From f5a9277f1f617379b4cda11a4a769bf36c23c4bc Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Fri, 21 Apr 2023 02:38:26 +0200 Subject: [PATCH] Fix CanvasDevice factory activation on WASDK --- .../ICanvasFactoryNative.cs | 29 +++++++++++++++++++ .../Helpers/ResourceManager.cs | 20 ++++++++++++- 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/src/ComputeSharp.D2D1.UI/ABI.Microsoft.Graphics.Canvas/ICanvasFactoryNative.cs b/src/ComputeSharp.D2D1.UI/ABI.Microsoft.Graphics.Canvas/ICanvasFactoryNative.cs index 2e14c612..2fd2cf6d 100644 --- a/src/ComputeSharp.D2D1.UI/ABI.Microsoft.Graphics.Canvas/ICanvasFactoryNative.cs +++ b/src/ComputeSharp.D2D1.UI/ABI.Microsoft.Graphics.Canvas/ICanvasFactoryNative.cs @@ -3,7 +3,13 @@ using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using TerraFX.Interop; using TerraFX.Interop.Windows; +#if WINDOWS_UWP using TerraFX.Interop.WinRT; +#else +using WinRT; +using WinRT.Interop; +using IInspectable = TerraFX.Interop.WinRT.IInspectable; +#endif #pragma warning disable CS0649, IDE1006 @@ -142,4 +148,27 @@ internal unsafe struct ICanvasFactoryNative return ((delegate* unmanaged[Stdcall])this.lpVtbl[10])( (ICanvasFactoryNative*)Unsafe.AsPointer(ref this)); } + +#if !WINDOWS_UWP + /// + /// The managed interface for . + /// + [Guid("695C440D-04B3-4EDD-BFD9-63E51E9F7202")] + [WindowsRuntimeType] + [WindowsRuntimeHelperType(typeof(Interface))] + public interface Interface + { + /// + /// The vtable type for . + /// + [Guid("695C440D-04B3-4EDD-BFD9-63E51E9F7202")] + public readonly struct Vftbl + { + /// + /// Allows CsWinRT to retrieve a pointer to the projection vtable (the name is hardcoded by convention). + /// + public static readonly IntPtr AbiToProjectionVftablePtr = IUnknownVftbl.AbiToProjectionVftblPtr; + } + } +#endif } \ No newline at end of file diff --git a/src/ComputeSharp.D2D1.UI/Helpers/ResourceManager.cs b/src/ComputeSharp.D2D1.UI/Helpers/ResourceManager.cs index 77c1b4a7..2e9b9fe1 100644 --- a/src/ComputeSharp.D2D1.UI/Helpers/ResourceManager.cs +++ b/src/ComputeSharp.D2D1.UI/Helpers/ResourceManager.cs @@ -3,15 +3,21 @@ using System.Diagnostics.CodeAnalysis; using ABI.Microsoft.Graphics.Canvas; using ComputeSharp.D2D1.Extensions; using TerraFX.Interop.Windows; +#if WINDOWS_UWP using TerraFX.Interop.WinRT; +#endif using Windows.Graphics.Effects; +#if !WINDOWS_UWP +using WinRT; +#endif #if WINDOWS_UWP namespace ComputeSharp.D2D1.Uwp.Helpers; #else namespace ComputeSharp.D2D1.WinUI.Helpers; -using WinRT = TerraFX.Interop.WinRT.WinRT; +using CanvasDevice = Microsoft.Graphics.Canvas.CanvasDevice; +using IInspectable = TerraFX.Interop.WinRT.IInspectable; #endif /// @@ -90,8 +96,11 @@ internal static unsafe class ResourceManager /// A pointer to the resulting activation factory. private static void GetActivationFactory(ICanvasFactoryNative** factoryNative) { +#if WINDOWS_UWP const string activatableClassId = "Microsoft.Graphics.Canvas.CanvasDevice"; + // On UWP, the WinRT types from Win2D are automatically registered for activation. + // This means we can simply use RoGetActivationFactory to retrieve the factory. fixed (char* pActivatableClassId = activatableClassId) { HSTRING_HEADER hStringHeaderActivatableClassId; @@ -112,5 +121,14 @@ internal static unsafe class ResourceManager iid: &canvasFactoryNativeId, factory: (void**)factoryNative).Assert(); } +#else + // On WinUI 3, the types are not guaranteed to be registered for activation. Additionally, + // for concistency with other types, we just use the built-in T.As() method, which will + // automatically handle fallback logic to resolve types to activate if they're not registered. + // For instance, this will ensure the following call will work fine in unpackaged apps. + ICanvasFactoryNative.Interface canvasDeviceActivationFactory = CanvasDevice.As(); + + *factoryNative = (ICanvasFactoryNative*)MarshalInspectable.FromManaged(canvasDeviceActivationFactory); +#endif } }