diff --git a/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.ICanvasImageInterop.cs b/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.ICanvasImageInterop.cs index a482d837..88cb9edc 100644 --- a/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.ICanvasImageInterop.cs +++ b/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.ICanvasImageInterop.cs @@ -251,8 +251,27 @@ unsafe partial class PixelShaderEffect { Guid effectId = PixelShaderEffect.For.Instance.Id; + using ComPtr d2D1DeviceContextLease = default; + using ComPtr d2D1DeviceContextEffective = 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); + } + // 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 @@ -270,7 +289,7 @@ unsafe partial class PixelShaderEffect 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 3e06cb86..c15f0c10 100644 --- a/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.Properties.Interop.Sources.cs +++ b/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.Properties.Interop.Sources.cs @@ -302,19 +302,16 @@ unsafe partial class PixelShaderEffect using ComPtr d2D1DeviceContextLease = default; 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. + // 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) { - // 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(d2D1DeviceContext); }