From 2362b32df0fa8054613779080506055a23d071ca Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 18 Oct 2022 00:30:44 +0200 Subject: [PATCH] Switch Win2D bindings to ICanvasImageInterop --- ...lags.cs => CanvasImageGetD2DImageFlags.cs} | 11 +--- ...mageInternal.cs => ICanvasImageInterop.cs} | 38 ++++++----- .../PixelShaderEffect{T}.cs | 63 +++++++++++-------- 3 files changed, 63 insertions(+), 49 deletions(-) rename src/ComputeSharp.D2D1.Uwp/ABI.Microsoft.Graphics.Canvas/{GetImageFlags.cs => CanvasImageGetD2DImageFlags.cs} (75%) rename src/ComputeSharp.D2D1.Uwp/ABI.Microsoft.Graphics.Canvas/{ICanvasImageInternal.cs => ICanvasImageInterop.cs} (53%) diff --git a/src/ComputeSharp.D2D1.Uwp/ABI.Microsoft.Graphics.Canvas/GetImageFlags.cs b/src/ComputeSharp.D2D1.Uwp/ABI.Microsoft.Graphics.Canvas/CanvasImageGetD2DImageFlags.cs similarity index 75% rename from src/ComputeSharp.D2D1.Uwp/ABI.Microsoft.Graphics.Canvas/GetImageFlags.cs rename to src/ComputeSharp.D2D1.Uwp/ABI.Microsoft.Graphics.Canvas/CanvasImageGetD2DImageFlags.cs index af94310b..613d6221 100644 --- a/src/ComputeSharp.D2D1.Uwp/ABI.Microsoft.Graphics.Canvas/GetImageFlags.cs +++ b/src/ComputeSharp.D2D1.Uwp/ABI.Microsoft.Graphics.Canvas/CanvasImageGetD2DImageFlags.cs @@ -3,10 +3,10 @@ using System; namespace ABI.Microsoft.Graphics.Canvas; /// -/// Options for fine-tuning the behavior of ICanvasImageInternal::GetD2DImage. +/// Options for fine-tuning the behavior of . /// [Flags] -internal enum GetImageFlags +internal enum CanvasImageGetD2DImageFlags { /// /// No options specified. @@ -36,10 +36,5 @@ internal enum GetImageFlags /// /// Allow partially configured effect graphs where some inputs are . /// - AllowNullEffectInputs = 16, - - /// - /// If an input is invalid, unrealize the effect and return rather than throwing. - /// - UnrealizeOnFailure = 32 + AllowNullEffectInputs = 16 } \ No newline at end of file diff --git a/src/ComputeSharp.D2D1.Uwp/ABI.Microsoft.Graphics.Canvas/ICanvasImageInternal.cs b/src/ComputeSharp.D2D1.Uwp/ABI.Microsoft.Graphics.Canvas/ICanvasImageInterop.cs similarity index 53% rename from src/ComputeSharp.D2D1.Uwp/ABI.Microsoft.Graphics.Canvas/ICanvasImageInternal.cs rename to src/ComputeSharp.D2D1.Uwp/ABI.Microsoft.Graphics.Canvas/ICanvasImageInterop.cs index d9d5db96..cdc4e71c 100644 --- a/src/ComputeSharp.D2D1.Uwp/ABI.Microsoft.Graphics.Canvas/ICanvasImageInternal.cs +++ b/src/ComputeSharp.D2D1.Uwp/ABI.Microsoft.Graphics.Canvas/ICanvasImageInterop.cs @@ -7,21 +7,29 @@ using TerraFX.Interop; namespace ABI.Microsoft.Graphics.Canvas; /// -/// Internal Win2D interface for all images and effects that can be drawn. +/// An interop Win2D interface for all images and effects that can be drawn. /// [ComImport] -[Guid("2F434224-053C-4978-87C4-CFAAFA2F4FAC")] +[Guid("E042D1F7-F9AD-4479-A713-67627EA31863")] [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] -internal unsafe interface ICanvasImageInternal +internal unsafe interface ICanvasImageInterop { /// - /// Gets an from an instance. + /// Gets the device that the effect is currently realized on, if any. /// - /// A pointer to the return buffer to write the result into (needed for ABI matching). - /// The input canvas device (as pointer to internal interface). + /// The resulting device, if available ((as a marshalled )). + /// The for the operation. + [PreserveSig] + [return: NativeTypeName("HRESULT")] + int GetDevice([NativeTypeName("ICanvasDevice**")] IUnknown** device); + + /// + /// Gets an from an instance. + /// + /// The input canvas device (as a marshalled ). /// /// The device context in use. This value is is optional (but recommended), except when the - /// flag is specified. This is because + /// flag is specified. This is because /// not all callers of have easy access to a context. It is always /// possible to get a resource creation context from the device, but the context is only /// actually necessary if a new effect realization needs to be created, so it is more efficient @@ -31,20 +39,22 @@ internal unsafe interface ICanvasImageInternal /// /// The DPI of the target device context. This is used to determine when a D2D1DpiCompensation /// effect needs to be inserted. Behavior of this parameter can be overridden by the flag values - /// , - /// or + /// , + /// or /// /// /// The DPI of a source bitmap, or zero if the image does not have a fixed DPI. A D2D1DpiCompensation effect /// will be inserted if and are different (flags permitting). /// - /// A pointer to the resulting image (this value is the same as ). + /// The resulting for the effect. + /// The for the operation. [PreserveSig] - ID2D1Image** GetD2DImage( - ID2D1Image** retBuf, + [return: NativeTypeName("HRESULT")] + int GetD2DImage( [NativeTypeName("ICanvasDevice*")] IUnknown* device, ID2D1DeviceContext* deviceContext, - GetImageFlags flags, + CanvasImageGetD2DImageFlags flags, float targetDpi, - float* realizeDpi); + float* realizeDpi, + ID2D1Image** ppImage); } \ No newline at end of file diff --git a/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.cs b/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.cs index 6e727c0b..f6f7613e 100644 --- a/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.cs +++ b/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.cs @@ -14,7 +14,7 @@ namespace ComputeSharp.D2D1.Uwp; /// A custom implementation powered by a supplied shader type. /// /// The type of shader to use to render frames. -public sealed class PixelShaderEffect : ICanvasImage, ICanvasImageInternal +public sealed class PixelShaderEffect : ICanvasImage, ICanvasImageInterop where T : unmanaged, ID2D1PixelShader { /// @@ -42,52 +42,61 @@ public sealed class PixelShaderEffect : ICanvasImage, ICanvasImageInternal } /// - unsafe ID2D1Image** ICanvasImageInternal.GetD2DImage( - ID2D1Image** retBuf, + unsafe int ICanvasImageInterop.GetDevice(IUnknown** device) + { + return S.S_OK; + } + + /// + unsafe int ICanvasImageInterop.GetD2DImage( IUnknown* device, ID2D1DeviceContext* deviceContext, - GetImageFlags flags, + CanvasImageGetD2DImageFlags flags, float targetDpi, - float* realizeDpi) + float* realizeDpi, + ID2D1Image** ppImage) { if (realizeDpi is not null) { - *realizeDpi = targetDpi; + *realizeDpi = 0; } - using ComPtr d2D1Image = default; - - if (this.effect.Get() is null) + try { - using ComPtr d2D1Factory = default; + if (this.effect.Get() is null) + { + using ComPtr d2D1Factory = default; - deviceContext->GetFactory(d2D1Factory.GetAddressOf()); + deviceContext->GetFactory(d2D1Factory.GetAddressOf()); - using ComPtr d2D1Factory1 = default; + using ComPtr d2D1Factory1 = default; - d2D1Factory.CopyTo(d2D1Factory1.GetAddressOf()).Assert(); + d2D1Factory.CopyTo(d2D1Factory1.GetAddressOf()).Assert(); - D2D1PixelShaderEffect.RegisterForD2D1Factory1(d2D1Factory1.Get(), out _); + D2D1PixelShaderEffect.RegisterForD2D1Factory1(d2D1Factory1.Get(), out _); - using ComPtr d2D1Effect = default; + using ComPtr d2D1Effect = default; - D2D1PixelShaderEffect.CreateFromD2D1DeviceContext(deviceContext, (void**)d2D1Effect.GetAddressOf()); + D2D1PixelShaderEffect.CreateFromD2D1DeviceContext(deviceContext, (void**)d2D1Effect.GetAddressOf()); - D2D1PixelShaderEffect.SetConstantBufferForD2D1Effect(in this.Value, d2D1Effect.Get()); + D2D1PixelShaderEffect.SetConstantBufferForD2D1Effect(in this.Value, d2D1Effect.Get()); - this.effect = new(d2D1Effect.Get()); + this.effect = new(d2D1Effect.Get()); - d2D1Effect.CopyTo(d2D1Image.GetAddressOf()).Assert(); + d2D1Effect.CopyTo(ppImage).Assert(); + } + else + { + D2D1PixelShaderEffect.SetConstantBufferForD2D1Effect(in this.Value, this.effect.Get()); + + this.effect.CopyTo(ppImage).Assert(); + } + + return S.S_OK; } - else + catch (Exception e) { - D2D1PixelShaderEffect.SetConstantBufferForD2D1Effect(in this.Value, this.effect.Get()); - - this.effect.CopyTo(d2D1Image.GetAddressOf()).Assert(); + return e.HResult; } - - *retBuf = d2D1Image.Detach(); - - return retBuf; } } \ No newline at end of file