From c33877d3826a98291ee5592d00565c31ffb386a8 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 10 Jan 2023 17:18:44 +0100 Subject: [PATCH] Defer getting D2D1 factory to when realization fails --- .../ID2D1DeviceContextExtensions.cs | 20 +++++++++++++++++++ ...ixelShaderEffect{T}.ICanvasImageInterop.cs | 15 +++++--------- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/src/ComputeSharp.D2D1.Uwp/Extensions/ID2D1DeviceContextExtensions.cs b/src/ComputeSharp.D2D1.Uwp/Extensions/ID2D1DeviceContextExtensions.cs index 5e6b46fb..c2304839 100644 --- a/src/ComputeSharp.D2D1.Uwp/Extensions/ID2D1DeviceContextExtensions.cs +++ b/src/ComputeSharp.D2D1.Uwp/Extensions/ID2D1DeviceContextExtensions.cs @@ -57,4 +57,24 @@ internal static unsafe class ID2D1DeviceContextExtensions return dpiX; } + + /// + /// Gets an object from a given . + /// + /// The input instance. + /// The resulting object. + /// The for the operation. + /// + /// The APIs specifically need an (as is used). + /// + public static HRESULT GetFactory1(this ref ID2D1DeviceContext deviceContext, ID2D1Factory1** d2D1Factory1) + { + using ComPtr d2D1Factory = default; + + // Get the underlying ID2D1Factory from the input context, which can be used to register effects + deviceContext.GetFactory(d2D1Factory.GetAddressOf()); + + // QueryInterface for the ID2D1Factory1 object (which should always be present) + return d2D1Factory.CopyTo(d2D1Factory1); + } } \ 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 048b8c97..33d14648 100644 --- a/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.ICanvasImageInterop.cs +++ b/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.ICanvasImageInterop.cs @@ -224,16 +224,6 @@ unsafe partial class PixelShaderEffect /// Whether the effect was realized correctly. private bool Realize(WIN2D_GET_D2D_IMAGE_FLAGS flags, float targetDpi, ID2D1DeviceContext* deviceContext) { - using ComPtr d2D1Factory = default; - - // Get the underlying ID2D1Factory from the input context, so we can register and create the effect - deviceContext->GetFactory(d2D1Factory.GetAddressOf()); - - using ComPtr d2D1Factory1 = default; - - // D2D1PixelShaderEffect APIs specifically need an ID2D1Factory1 (as ID2D1Factory1::RegisterEffectFromString is used) - d2D1Factory.CopyTo(d2D1Factory1.GetAddressOf()).Assert(); - fixed (ID2D1Effect** d2D1Effect = this.d2D1Effect) { Guid effectId = PixelShaderEffect.For.Instance.Id; @@ -248,6 +238,11 @@ unsafe partial class PixelShaderEffect if (hresult == D2DERR.D2DERR_EFFECT_IS_NOT_REGISTERED || hresult == E.E_NOTFOUND) { + using ComPtr d2D1Factory1 = default; + + // Get the ID2D1Factory1 object to register the effect (required by D2D1PixelShaderEffect) + deviceContext->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 _);