From 4954bec5f41b92dc17dcd9251caf484795c384bc Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 10 Dec 2022 02:58:02 +0100 Subject: [PATCH] Implement Win2D's ResourceManager::GetOrCreate logic --- .../Buffers/SourceReference.cs | 9 ++- .../Helpers/ResourceManager.cs | 68 +++++++++++++++++++ ...derEffect{T}.Properties.Interop.Sources.cs | 2 +- 3 files changed, 75 insertions(+), 4 deletions(-) create mode 100644 src/ComputeSharp.D2D1.Uwp/Helpers/ResourceManager.cs diff --git a/src/ComputeSharp.D2D1.Uwp/Buffers/SourceReference.cs b/src/ComputeSharp.D2D1.Uwp/Buffers/SourceReference.cs index 09096450..b128c63a 100644 --- a/src/ComputeSharp.D2D1.Uwp/Buffers/SourceReference.cs +++ b/src/ComputeSharp.D2D1.Uwp/Buffers/SourceReference.cs @@ -2,6 +2,7 @@ using System; using System.Runtime.CompilerServices; using ABI.Microsoft.Graphics.Canvas; using ComputeSharp.D2D1.Uwp.Extensions; +using ComputeSharp.D2D1.Uwp.Helpers; using TerraFX.Interop.DirectX; using TerraFX.Interop.Windows; using Windows.Graphics.Effects; @@ -140,12 +141,14 @@ internal unsafe struct SourceReference : IDisposable // If that's not the case, create a new wrapper if the input image is not null if (d2D1Image is not null) { - // m_wrapper = ResourceManager::GetOrCreate(device, resource); - throw new NotImplementedException(); + this.graphicsEffectSourceWrapper = ResourceManager.GetOrCreate( + device: canvasDevice, + resource: (IUnknown*)d2D1Image, + dpi: 0); } else { - this.d2D1ImageSource.Dispose(); + this.graphicsEffectSourceWrapper = null; } this.d2D1ImageSource = new ComPtr(d2D1Image); diff --git a/src/ComputeSharp.D2D1.Uwp/Helpers/ResourceManager.cs b/src/ComputeSharp.D2D1.Uwp/Helpers/ResourceManager.cs new file mode 100644 index 00000000..1d650574 --- /dev/null +++ b/src/ComputeSharp.D2D1.Uwp/Helpers/ResourceManager.cs @@ -0,0 +1,68 @@ +using System; +using System.Runtime.InteropServices; +using ABI.Microsoft.Graphics.Canvas; +using ComputeSharp.D2D1.Extensions; +using TerraFX.Interop.Windows; +using TerraFX.Interop.WinRT; +using Windows.Graphics.Effects; + +namespace ComputeSharp.D2D1.Uwp.Helpers; + +/// +/// A helper type to replicate Win2D's ResourceManager type. +/// +/// +/// This type provides an entry point to use the Win2D activation factory. +/// +internal static class ResourceManager +{ + /// + /// Gets or creates an instance for a native resource. + /// + /// The input canvas device (as a marshalled ). + /// The input native resource to create a wrapper for. + /// The realization DPIs for + /// The resulting wrapper instance. + /// Thrown if the managed wrapper could not be retrieved. + public static unsafe IGraphicsEffectSource GetOrCreate(ICanvasDevice* device, IUnknown* resource, float dpi) + { + const string activatableClassId = "Microsoft.Graphics.Canvas.CanvasDevice"; + + fixed (char* pActivatableClassId = activatableClassId) + { + HSTRING_HEADER hStringHeaderActivatableClassId; + HSTRING hStringActivatableClass; + + // Create a fast-pass HSTRING for "Microsoft.Graphics.Canvas.CanvasDevice" + WinRT.WindowsCreateStringReference( + sourceString: (ushort*)pActivatableClassId, + length: (uint)activatableClassId.Length, + hstringHeader: &hStringHeaderActivatableClassId, + @string: &hStringActivatableClass).Assert(); + + Guid canvasFactoryNativeId = typeof(ICanvasFactoryNative).GUID; + + using ComPtr canvasFactoryNative = default; + + // Get the activation factory for CanvasDevice + WinRT.RoGetActivationFactory( + activatableClassId: hStringActivatableClass, + iid: &canvasFactoryNativeId, + factory: canvasFactoryNative.GetVoidAddressOf()).Assert(); + + using ComPtr wrapperUnknown = default; + + // Get or create a WinRT wrapper for the resource + canvasFactoryNative.Get()->GetOrCreate( + device: device, + resource: resource, + dpi: dpi, + wrapper: wrapperUnknown.GetVoidAddressOf()).Assert(); + + // Get the runtime-provided RCW for the resulting WinRT wrapper + object wrapper = Marshal.GetObjectForIUnknown((IntPtr)wrapperUnknown.Get()); + + return (IGraphicsEffectSource)wrapper; + } + } +} diff --git a/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.Properties.Interop.Sources.cs b/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.Properties.Interop.Sources.cs index 16d5dbb8..3d44e0f3 100644 --- a/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.Properties.Interop.Sources.cs +++ b/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.Properties.Interop.Sources.cs @@ -103,7 +103,7 @@ unsafe partial class PixelShaderEffect } // Get or create a wrapper for the input image - return sourceReference.GetOrCreateWrapper(null, d2D1Image.Get()); + return sourceReference.GetOrCreateWrapper(this.canvasDevice.Get(), d2D1Image.Get()); } /// The index of the source to get or set.