diff --git a/src/ComputeSharp.D2D1.Uwp/ABI.Microsoft.Graphics.Canvas/ID2D1DeviceContextLease.cs b/src/ComputeSharp.D2D1.Uwp/ABI.Microsoft.Graphics.Canvas/ID2D1DeviceContextLease.cs new file mode 100644 index 00000000..c0e19200 --- /dev/null +++ b/src/ComputeSharp.D2D1.Uwp/ABI.Microsoft.Graphics.Canvas/ID2D1DeviceContextLease.cs @@ -0,0 +1,60 @@ +using System; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using TerraFX.Interop; +using TerraFX.Interop.DirectX; +using TerraFX.Interop.Windows; + +#pragma warning disable CS0649, IDE1006 + +namespace ABI.Microsoft.Graphics.Canvas; + +/// +/// An interface for a COM object that provides access to a pooled object. +/// +[Guid("A0928F38-F7D5-44DD-A5C9-E23D94734BBB")] +[NativeTypeName("class ID2D1DeviceContextLease : IUnknown")] +[NativeInheritance("IUnknown")] +internal unsafe struct ID2D1DeviceContextLease +{ + public void** lpVtbl; + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [VtblIndex(0)] + public HRESULT QueryInterface([NativeTypeName("const IID &")] Guid* riid, void** ppvObject) + { + return ((delegate* unmanaged[Stdcall])this.lpVtbl[0])((ID2D1DeviceContextLease*)Unsafe.AsPointer(ref this), riid, ppvObject); + } + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [VtblIndex(1)] + [return: NativeTypeName("ULONG")] + public uint AddRef() + { + return ((delegate* unmanaged[Stdcall])this.lpVtbl[1])((ID2D1DeviceContextLease*)Unsafe.AsPointer(ref this)); + } + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [VtblIndex(2)] + [return: NativeTypeName("ULONG")] + public uint Release() + { + return ((delegate* unmanaged[Stdcall])this.lpVtbl[2])((ID2D1DeviceContextLease*)Unsafe.AsPointer(ref this)); + } + + /// + /// Provides access to the object wrapped by the current instance. + /// + /// The resulting object wrapped by the current instance. + /// The for the operation. + /// The returned cannot be used once the owning lease has been released. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [VtblIndex(3)] + public HRESULT GetD2DDeviceContext(ID2D1DeviceContext** deviceContext) + { + return ((delegate* unmanaged[Stdcall])this.lpVtbl[3])((ID2D1DeviceContextLease*)Unsafe.AsPointer(ref this), deviceContext); + } +} \ No newline at end of file diff --git a/src/ComputeSharp.D2D1.Uwp/ABI.Microsoft.Graphics.Canvas/ID2D1DeviceContextPool.cs b/src/ComputeSharp.D2D1.Uwp/ABI.Microsoft.Graphics.Canvas/ID2D1DeviceContextPool.cs new file mode 100644 index 00000000..500b65ab --- /dev/null +++ b/src/ComputeSharp.D2D1.Uwp/ABI.Microsoft.Graphics.Canvas/ID2D1DeviceContextPool.cs @@ -0,0 +1,58 @@ +using System; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using TerraFX.Interop; +using TerraFX.Interop.Windows; + +#pragma warning disable CS0649, IDE1006 + +namespace ABI.Microsoft.Graphics.Canvas; + +/// +/// An interface for an object that provides access to pooled objects. +/// +[Guid("454A82A1-F024-40DB-BD5B-8F527FD58AD0")] +[NativeTypeName("class ID2D1DeviceContextPool : IUnknown")] +[NativeInheritance("IUnknown")] +internal unsafe struct ID2D1DeviceContextPool +{ + public void** lpVtbl; + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [VtblIndex(0)] + public HRESULT QueryInterface([NativeTypeName("const IID &")] Guid* riid, void** ppvObject) + { + return ((delegate* unmanaged[Stdcall])this.lpVtbl[0])((ID2D1DeviceContextPool*)Unsafe.AsPointer(ref this), riid, ppvObject); + } + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [VtblIndex(1)] + [return: NativeTypeName("ULONG")] + public uint AddRef() + { + return ((delegate* unmanaged[Stdcall])this.lpVtbl[1])((ID2D1DeviceContextPool*)Unsafe.AsPointer(ref this)); + } + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [VtblIndex(2)] + [return: NativeTypeName("ULONG")] + public uint Release() + { + return ((delegate* unmanaged[Stdcall])this.lpVtbl[2])((ID2D1DeviceContextPool*)Unsafe.AsPointer(ref this)); + } + + /// + /// Retrieves a new object from the current instance. + /// + /// The resulting object to use. + /// The for the operation. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [VtblIndex(3)] + public HRESULT GetDeviceContextLease(ID2D1DeviceContextLease** lease) + { + return ((delegate* unmanaged[Stdcall])this.lpVtbl[3])((ID2D1DeviceContextPool*)Unsafe.AsPointer(ref this), lease); + } +} \ No newline at end of file diff --git a/src/ComputeSharp.D2D1.Uwp/Extensions/ICanvasDeviceExtensions.cs b/src/ComputeSharp.D2D1.Uwp/Extensions/ICanvasDeviceExtensions.cs index 94857be8..bea0e059 100644 --- a/src/ComputeSharp.D2D1.Uwp/Extensions/ICanvasDeviceExtensions.cs +++ b/src/ComputeSharp.D2D1.Uwp/Extensions/ICanvasDeviceExtensions.cs @@ -37,23 +37,43 @@ internal static unsafe class ICanvasDeviceExtensions } /// - /// Creates a new from an input . + /// Retrieves a usable from an input if one is not available. + /// If a new one had to be rented from the pool, it also returns the owning instance. /// /// The input object. - /// The resulting object. - /// The for the operation. - public static HRESULT CreateD2DDeviceContext(this ref ICanvasDevice canvasDevice, ID2D1DeviceContext** d2D1DeviceContext) + /// The current object, if available. + /// The resulting instance to use. + /// The resulting object, if used. + /// + /// When this method returns, is guaranteed to not be . + /// + public static void GetEffectiveD2DDeviceContextWithOptionalLease( + this ref ICanvasDevice canvasDevice, + ID2D1DeviceContext* d2D1DeviceContext, + ID2D1DeviceContext** d2D1DeviceContextEffective, + ID2D1DeviceContextLease** d2D1DeviceContextLease) { - using ComPtr d2D1Device1 = default; + // If there is no input device context, just create a new one from the input canvas device + if (d2D1DeviceContext is null) + { + using (ComPtr d2D1DeviceContextPool = default) + { + // Get the ID2D1DeviceContextPool interface reference + canvasDevice.QueryInterface( + riid: Win32.__uuidof(), + ppvObject: d2D1DeviceContextPool.GetVoidAddressOf()).Assert(); - // Get the underlying ID2D1Device1 object - canvasDevice.GetD2DDevice(d2D1Device1.GetAddressOf()).Assert(); + // Get a new ID2D1DeviceContextLease object from the retrieved pool + d2D1DeviceContextPool.Get()->GetDeviceContextLease(d2D1DeviceContextLease).Assert(); + } - // Create the new device context - HRESULT hresult = d2D1Device1.Get()->CreateDeviceContext( - options: D2D1_DEVICE_CONTEXT_OPTIONS.D2D1_DEVICE_CONTEXT_OPTIONS_NONE, - deviceContext: d2D1DeviceContext); - - return hresult; + // Get the underlying device context from the lease (this is much faster than creating a new device context) + (*d2D1DeviceContextLease)->GetD2DDeviceContext(d2D1DeviceContextEffective).Assert(); + } + else + { + // Otherwise, just use the input device context + *d2D1DeviceContextEffective = new ComPtr(d2D1DeviceContext); + } } } \ No newline at end of file diff --git a/src/ComputeSharp.D2D1.Uwp/Helpers/ResourceManager.cs b/src/ComputeSharp.D2D1.Uwp/Helpers/ResourceManager.cs index 77dd939a..bd37235c 100644 --- a/src/ComputeSharp.D2D1.Uwp/Helpers/ResourceManager.cs +++ b/src/ComputeSharp.D2D1.Uwp/Helpers/ResourceManager.cs @@ -19,7 +19,7 @@ internal static class ResourceManager /// /// Gets or creates an instance for a native resource. /// - /// The input canvas device (as a marshalled ). + /// The input canvas device (as a marshalled ). /// The input native resource to create a wrapper for. /// The realization DPIs for /// The resulting wrapper instance. diff --git a/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.ICanvasImageInterop.cs b/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.ICanvasImageInterop.cs index a482d837..985fbddf 100644 --- a/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.ICanvasImageInterop.cs +++ b/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.ICanvasImageInterop.cs @@ -251,8 +251,17 @@ unsafe partial class PixelShaderEffect { Guid effectId = PixelShaderEffect.For.Instance.Id; + using ComPtr d2D1DeviceContextEffective = default; + using ComPtr d2D1DeviceContextLease = default; + + // We need to realize the current effect (ComputeSharp.D2D1's ID2D1Effect), which needs a device context + this.canvasDevice.Get()->GetEffectiveD2DDeviceContextWithOptionalLease( + d2D1DeviceContext: deviceContext, + d2D1DeviceContextEffective: d2D1DeviceContextEffective.GetAddressOf(), + d2D1DeviceContextLease: d2D1DeviceContextLease.GetAddressOf()); + // Try to create an instance of the effect in use and store it in the current object - HRESULT hresult = deviceContext->CreateEffect(effectId: &effectId, effect: d2D1Effect); + HRESULT hresult = d2D1DeviceContextEffective.Get()->CreateEffect(effectId: &effectId, effect: d2D1Effect); // Check if creation failed due to the effect not being registered. In that case, register // it and then try again. This is much faster than check whether the effect is registered @@ -264,13 +273,13 @@ unsafe partial class PixelShaderEffect using ComPtr d2D1Factory1 = default; // Get the ID2D1Factory1 object to register the effect (required by D2D1PixelShaderEffect) - deviceContext->GetFactory1(d2D1Factory1.GetAddressOf()).Assert(); + d2D1DeviceContextEffective.Get()->GetFactory1(d2D1Factory1.GetAddressOf()).Assert(); // Register the effect with the factory (pass the same D2D1 draw transform mapper factory that was used before) D2D1PixelShaderEffect.RegisterForD2D1Factory1(d2D1Factory1.Get(), out _); // Try to create the effect again - hresult = deviceContext->CreateEffect(effectId: &effectId, effect: d2D1Effect); + hresult = d2D1DeviceContextEffective.Get()->CreateEffect(effectId: &effectId, effect: d2D1Effect); } // Rethrow any other errors 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 2f524503..1505c86e 100644 --- a/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.Properties.Interop.Sources.cs +++ b/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.Properties.Interop.Sources.cs @@ -299,24 +299,20 @@ unsafe partial class PixelShaderEffect // Create an ID2D1Effect for the DPI compensation, if there isn't one already if (!sourceReference.HasDpiCompensationEffect) { - using ComPtr d2D1DeviceContextEffective = default; - - // We need to create a DPI compensation effect, so a device context must be available. - // If there is no input device context, just create a new one from the realization device. - if (d2D1DeviceContext is null) + using (ComPtr d2D1DeviceContextEffective = default) + using (ComPtr d2D1DeviceContextLease = default) { - this.canvasDevice.Get()->CreateD2DDeviceContext(d2D1DeviceContextEffective.GetAddressOf()).Assert(); - } - else - { - // Otherwise, just use the input device context - *&d2D1DeviceContextEffective = new ComPtr(d2D1DeviceContext); - } + // Just like when realizing the current effect, rent a device context from the pool if one is not available + this.canvasDevice.Get()->GetEffectiveD2DDeviceContextWithOptionalLease( + d2D1DeviceContext: d2D1DeviceContext, + d2D1DeviceContextEffective: d2D1DeviceContextEffective.GetAddressOf(), + d2D1DeviceContextLease: d2D1DeviceContextLease.GetAddressOf()); - // Create the DPI compensation effect - d2D1DeviceContextEffective.Get()->CreateEffect( - effectId: (Guid*)Unsafe.AsPointer(ref Unsafe.AsRef(in CLSID.CLSID_D2D1DpiCompensation)), - effect: d2D1EffectDpiCompensation.GetAddressOf()).Assert(); + // Create the DPI compensation effect + d2D1DeviceContextEffective.Get()->CreateEffect( + effectId: (Guid*)Unsafe.AsPointer(ref Unsafe.AsRef(in CLSID.CLSID_D2D1DpiCompensation)), + effect: d2D1EffectDpiCompensation.GetAddressOf()).Assert(); + } D2D1_BORDER_MODE d2D1BorderMode = D2D1_BORDER_MODE_HARD; D2D1_DPICOMPENSATION_INTERPOLATION_MODE d2D1DpiCompensationInterpolationMode = D2D1_DPICOMPENSATION_INTERPOLATION_MODE_LINEAR; diff --git a/src/TerraFX.Interop.Windows.D2D1/DirectX/um/d2d1_1/D2D1_DEVICE_CONTEXT_OPTIONS.cs b/src/TerraFX.Interop.Windows.D2D1/DirectX/um/d2d1_1/D2D1_DEVICE_CONTEXT_OPTIONS.cs deleted file mode 100644 index bd22e599..00000000 --- a/src/TerraFX.Interop.Windows.D2D1/DirectX/um/d2d1_1/D2D1_DEVICE_CONTEXT_OPTIONS.cs +++ /dev/null @@ -1,16 +0,0 @@ -// Copyright © Tanner Gooding and Contributors. Licensed under the MIT License (MIT). See License.md in the repository root for more information. - -// Ported from um/d2d1_1.h in the Windows SDK for Windows 10.0.22621.0 -// Original source is Copyright © Microsoft. All rights reserved. - -using System; - -namespace TerraFX.Interop.DirectX; - -[Flags] -internal enum D2D1_DEVICE_CONTEXT_OPTIONS : uint -{ - D2D1_DEVICE_CONTEXT_OPTIONS_NONE = 0, - D2D1_DEVICE_CONTEXT_OPTIONS_ENABLE_MULTITHREADED_OPTIMIZATIONS = 1, - D2D1_DEVICE_CONTEXT_OPTIONS_FORCE_DWORD = 0xffffffff, -} \ No newline at end of file diff --git a/src/TerraFX.Interop.Windows.D2D1/DirectX/um/d2d1_2/ID2D1Device1.cs b/src/TerraFX.Interop.Windows.D2D1/DirectX/um/d2d1_2/ID2D1Device1.cs index 1a374296..193e8b8c 100644 --- a/src/TerraFX.Interop.Windows.D2D1/DirectX/um/d2d1_2/ID2D1Device1.cs +++ b/src/TerraFX.Interop.Windows.D2D1/DirectX/um/d2d1_2/ID2D1Device1.cs @@ -49,11 +49,4 @@ internal unsafe partial struct ID2D1Device1 { ((delegate* unmanaged[Stdcall])(lpVtbl[3]))((ID2D1Device1*)Unsafe.AsPointer(ref this), factory); } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - [VtblIndex(4)] - public HRESULT CreateDeviceContext(D2D1_DEVICE_CONTEXT_OPTIONS options, ID2D1DeviceContext** deviceContext) - { - return ((delegate* unmanaged[Stdcall])(lpVtbl[4]))((ID2D1Device1*)Unsafe.AsPointer(ref this), options, deviceContext); - } } \ No newline at end of file diff --git a/src/TerraFX.Interop.Windows.D2D1/TerraFX.Interop.Windows.D2D1.projitems b/src/TerraFX.Interop.Windows.D2D1/TerraFX.Interop.Windows.D2D1.projitems index 51cb9906..cfa82c7b 100644 --- a/src/TerraFX.Interop.Windows.D2D1/TerraFX.Interop.Windows.D2D1.projitems +++ b/src/TerraFX.Interop.Windows.D2D1/TerraFX.Interop.Windows.D2D1.projitems @@ -35,7 +35,6 @@ -