diff --git a/src/ComputeSharp.D2D1.Uwp/CanvasEffect.cs b/src/ComputeSharp.D2D1.Uwp/CanvasEffect.cs new file mode 100644 index 00000000..877479dc --- /dev/null +++ b/src/ComputeSharp.D2D1.Uwp/CanvasEffect.cs @@ -0,0 +1,117 @@ +using System.Numerics; +using System.Runtime.InteropServices; +using ABI.Microsoft.Graphics.Canvas; +using ComputeSharp.D2D1.Extensions; +using Microsoft.Graphics.Canvas; +using TerraFX.Interop.DirectX; +using TerraFX.Interop.Windows; +using Windows.Foundation; +using ICanvasResourceCreator = Microsoft.Graphics.Canvas.ICanvasResourceCreator; + +#pragma warning disable CA1063 + +namespace ComputeSharp.D2D1.Uwp; + +/// +/// A base type to implement packaged and easy to use -based effects that can be used with Win2D. +/// +public abstract unsafe class CanvasEffect : ICanvasImage, ICanvasImageInterop.Interface +{ + /// + /// Lock object used to synchronize calls to APIs. + /// + private readonly object lockObject = new(); + + /// + /// The current cached result for , if available. + /// + private ICanvasImage? canvasImage; + + /// + public Rect GetBounds(ICanvasResourceCreator resourceCreator) + { + return GetCanvasImage().GetBounds(resourceCreator); + } + + /// + public Rect GetBounds(ICanvasResourceCreator resourceCreator, Matrix3x2 transform) + { + return GetCanvasImage().GetBounds(resourceCreator, transform); + } + + /// + public void Dispose() + { + } + + /// + unsafe int ICanvasImageInterop.Interface.GetDevice(ICanvasDevice** device) + { + using ComPtr canvasImageInterop = default; + + GetCanvasImageInterop(canvasImageInterop.GetAddressOf()); + + return canvasImageInterop.Get()->GetDevice(device); + } + + /// + unsafe int ICanvasImageInterop.Interface.GetD2DImage(ICanvasDevice* device, ID2D1DeviceContext* deviceContext, WIN2D_GET_D2D_IMAGE_FLAGS flags, float targetDpi, float* realizeDpi, ID2D1Image** ppImage) + { + using ComPtr canvasImageInterop = default; + + GetCanvasImageInterop(canvasImageInterop.GetAddressOf()); + + return canvasImageInterop.Get()->GetD2DImage(device, deviceContext, flags, targetDpi, realizeDpi, ppImage); + } + + /// + /// Creates the resulting representing the output node of the effect graph for this instance. + /// + /// The resulting representing the output node of the effect graph for this instance. + /// + /// This method is only called once and its result is cached until explicitly invalidated by calling . + /// + protected abstract ICanvasImage CreateCanvasImage(); + + /// + /// Invalidates the last returned result from . + /// + /// + /// This method is used to signal when the entire effect graph should be updated. + /// When called, the internal cache for will be reset. + /// + protected void InvalidateCanvasImage() + { + lock (this.lockObject) + { + this.canvasImage?.Dispose(); + + this.canvasImage = null; + } + } + + /// + /// Gets or creates the current instance. + /// + /// The current instance. + private ICanvasImage GetCanvasImage() + { + lock (this.lockObject) + { + return this.canvasImage ??= CreateCanvasImage(); + } + } + + /// + /// Gets the object currently in use. + /// + /// A pointer to the object currently in use. + private void GetCanvasImageInterop(ICanvasImageInterop** canvasImageInterop) + { + using ComPtr canvasImageUnknown = default; + + canvasImageUnknown.Attach((IUnknown*)Marshal.GetIUnknownForObject(GetCanvasImage())); + + canvasImageUnknown.CopyTo(canvasImageInterop).Assert(); + } +} \ No newline at end of file