diff --git a/src/ComputeSharp.D2D1.Uwp/Extensions/ICanvasDeviceExtensions.cs b/src/ComputeSharp.D2D1.Uwp/Extensions/ICanvasDeviceExtensions.cs
index 5be9f5a0..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
}
///
- /// Gets 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 GetD2DDeviceContextLease(this ref ICanvasDevice canvasDevice, ID2D1DeviceContextLease** d2D1DeviceContextLease)
+ /// 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 d2D1DeviceContextPool = 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 ID2D1DeviceContextLease interface reference
- canvasDevice.QueryInterface(
- riid: Win32.__uuidof(),
- ppvObject: d2D1DeviceContextPool.GetVoidAddressOf()).Assert();
+ // Get a new ID2D1DeviceContextLease object from the retrieved pool
+ d2D1DeviceContextPool.Get()->GetDeviceContextLease(d2D1DeviceContextLease).Assert();
+ }
- // Create the new lease
- HRESULT hresult = d2D1DeviceContextPool.Get()->GetDeviceContextLease(d2D1DeviceContextLease);
-
- 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/PixelShaderEffect{T}.ICanvasImageInterop.cs b/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.ICanvasImageInterop.cs
index 88cb9edc..985fbddf 100644
--- a/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.ICanvasImageInterop.cs
+++ b/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.ICanvasImageInterop.cs
@@ -251,24 +251,14 @@ unsafe partial class PixelShaderEffect
{
Guid effectId = PixelShaderEffect.For.Instance.Id;
- using ComPtr d2D1DeviceContextLease = default;
using ComPtr d2D1DeviceContextEffective = default;
+ using ComPtr d2D1DeviceContextLease = default;
- // We need to realize the current effect (ComputeSharp.D2D1's ID2D1Effect), so a device context must
- // be available. If there is no input device context, just create a new one from the realization device.
- if (deviceContext is null)
- {
- // Get the ID2D1DeviceContextLease interface from the input device
- this.canvasDevice.Get()->GetD2DDeviceContextLease(d2D1DeviceContextLease.GetAddressOf()).Assert();
-
- // Rent a new device context from the lease (this is much faster than creating a new device context)
- d2D1DeviceContextLease.Get()->GetD2DDeviceContext(d2D1DeviceContextEffective.GetAddressOf()).Assert();
- }
- else
- {
- // Otherwise, just use the input device context
- *&d2D1DeviceContextEffective = new ComPtr(deviceContext);
- }
+ // 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 = d2D1DeviceContextEffective.Get()->CreateEffect(effectId: &effectId, effect: d2D1Effect);
@@ -283,7 +273,7 @@ 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 _);
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 c15f0c10..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,26 +299,20 @@ unsafe partial class PixelShaderEffect
// Create an ID2D1Effect for the DPI compensation, if there isn't one already
if (!sourceReference.HasDpiCompensationEffect)
{
- using ComPtr d2D1DeviceContextLease = default;
- using ComPtr d2D1DeviceContextEffective = default;
-
- // Just like when realizing the current effect, rent a device context from the pool if one is not available.
- // That is, either get the ID2D1DeviceContextPool interface and rent a device, or just use the input one.
- if (d2D1DeviceContext is null)
+ using (ComPtr d2D1DeviceContextEffective = default)
+ using (ComPtr d2D1DeviceContextLease = default)
{
- this.canvasDevice.Get()->GetD2DDeviceContextLease(d2D1DeviceContextLease.GetAddressOf()).Assert();
+ // 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());
- d2D1DeviceContextLease.Get()->GetD2DDeviceContext(d2D1DeviceContextEffective.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();
}
- else
- {
- *&d2D1DeviceContextEffective = new ComPtr(d2D1DeviceContext);
- }
-
- // 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;