From 24b54273c8654476ad0a57f701214243ef2aff4f Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Fri, 9 Dec 2022 14:07:01 +0100 Subject: [PATCH 1/3] Add initial draft of base CanvasEffect type --- src/ComputeSharp.D2D1.Uwp/CanvasEffect.cs | 117 ++++++++++++++++++++++ 1 file changed, 117 insertions(+) create mode 100644 src/ComputeSharp.D2D1.Uwp/CanvasEffect.cs 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 From 4cc15152fee67b95a63f913ee4d159942f16b14c Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 10 Dec 2022 01:39:42 +0100 Subject: [PATCH 2/3] Improve CanvasEffect public API --- .../CanvasEffect.Interop.cs | 111 +++++++++++++ src/ComputeSharp.D2D1.Uwp/CanvasEffect.cs | 154 +++++++++++------- 2 files changed, 203 insertions(+), 62 deletions(-) create mode 100644 src/ComputeSharp.D2D1.Uwp/CanvasEffect.Interop.cs diff --git a/src/ComputeSharp.D2D1.Uwp/CanvasEffect.Interop.cs b/src/ComputeSharp.D2D1.Uwp/CanvasEffect.Interop.cs new file mode 100644 index 00000000..a0d1552d --- /dev/null +++ b/src/ComputeSharp.D2D1.Uwp/CanvasEffect.Interop.cs @@ -0,0 +1,111 @@ +using System; +using System.Diagnostics.CodeAnalysis; +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; + +/// +unsafe partial class CanvasEffect +{ + /// + public Rect GetBounds(ICanvasResourceCreator resourceCreator) + { + return GetCanvasImage().GetBounds(resourceCreator); + } + + /// + public Rect GetBounds(ICanvasResourceCreator resourceCreator, Matrix3x2 transform) + { + return GetCanvasImage().GetBounds(resourceCreator, transform); + } + + /// + public void Dispose() + { + lock (this.lockObject) + { + this.canvasImage?.Dispose(); + this.canvasImage = null; + this.isDisposed = true; + } + } + + /// + 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); + } + + /// + /// Gets or creates the current instance. + /// + /// The current instance. + /// Thrown if the current instance is disposed. + private ICanvasImage GetCanvasImage() + { + lock (this.lockObject) + { + if (this.isDisposed) + { + ThrowObjectDisposedException(); + } + + this.canvasImage ??= CreateCanvasImage(); + + if (this.isInvalidated) + { + ConfigureCanvasImage(); + + this.isInvalidated = false; + } + + return this.canvasImage; + } + } + + /// + /// 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(); + } + + /// + /// Throws a new . + /// + [DoesNotReturn] + public void ThrowObjectDisposedException() + { + throw new ObjectDisposedException(ToString(), "The current effect has been disposed and can no longer be used."); + } +} \ No newline at end of file diff --git a/src/ComputeSharp.D2D1.Uwp/CanvasEffect.cs b/src/ComputeSharp.D2D1.Uwp/CanvasEffect.cs index 877479dc..38accdbd 100644 --- a/src/ComputeSharp.D2D1.Uwp/CanvasEffect.cs +++ b/src/ComputeSharp.D2D1.Uwp/CanvasEffect.cs @@ -1,12 +1,6 @@ -using System.Numerics; -using System.Runtime.InteropServices; +using System.Collections.Generic; 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 @@ -15,7 +9,7 @@ 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 +public abstract partial class CanvasEffect : ICanvasImage, ICanvasImageInterop.Interface { /// /// Lock object used to synchronize calls to APIs. @@ -27,91 +21,127 @@ public abstract unsafe class CanvasEffect : ICanvasImage, ICanvasImageInterop.In /// private ICanvasImage? canvasImage; - /// - public Rect GetBounds(ICanvasResourceCreator resourceCreator) - { - return GetCanvasImage().GetBounds(resourceCreator); - } + /// + /// Indicates whether the current state has been invalidated (requiring to be called). + /// + private bool isInvalidated; - /// - 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); - } + /// + /// Indicates whether the effect is disposed. + /// + private bool isDisposed; /// /// 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 . + /// + /// This method is only called once and its result is cached until explicitly invalidated by calling and related methods. + /// + /// + /// This method should never be called directly. Instead, it is used internally to produce an instance when the effect is used. + /// /// protected abstract ICanvasImage CreateCanvasImage(); /// - /// Invalidates the last returned result from . + /// Configures the current instance when the effect is used. /// /// - /// This method is used to signal when the entire effect graph should be updated. - /// When called, the internal cache for will be reset. + /// + /// This method is guaranteed to be called after has been invoked already. + /// + /// + /// This method should never be called directly. Instead, it is used internally to configure the current instance when the effect is used. + /// /// - protected void InvalidateCanvasImage() + protected abstract void ConfigureCanvasImage(); + + /// + /// Invalidates the last returned result from . + /// + /// The invalidation type to request. + /// + /// + /// This method is used to signal when the effect graph should be updated, and it can indicate + /// that either the entire graph should be created again (by calling ), + /// or whether it simply needs to refresh its internal state (by calling ). + /// + /// + /// If is called with , the effect + /// graph will only be refreshed the next time the image is actually requested. That is, repeated requests + /// for updates do not result in unnecessarily calls to . + /// + /// + protected void InvalidateCanvasImage(InvalidationType invalidationType = InvalidationType.Update) { lock (this.lockObject) { - this.canvasImage?.Dispose(); + // If the effect graph should be created again, dispose the image and throw it away + if (invalidationType == InvalidationType.Creation) + { + this.canvasImage?.Dispose(); - this.canvasImage = null; + this.canvasImage = null; + this.isInvalidated = false; + } + else + { + // Otherwise, just mark the internal state as not being valid anymore. The next + // time the image is requested, the effect graph will be configured if needed. + this.isInvalidated = true; + } } } /// - /// Gets or creates the current instance. + /// Updates the backing storage for an effect property and checks if it has changed. + /// If so, it invalidates the effect graph as well with the requested invalidation type. /// - /// The current instance. - private ICanvasImage GetCanvasImage() + /// The type of the property that changed. + /// The storage for the effect property value. + /// The new effect property value to set. + /// The invalidation type to request. + protected void SetAndInvalidateCanvasImage(/*[NotNullIfNotNull(nameof(newValue))]*/ ref T storage, T value, InvalidationType invalidationType = InvalidationType.Update) { - lock (this.lockObject) + if (EqualityComparer.Default.Equals(storage, value)) { - return this.canvasImage ??= CreateCanvasImage(); + return; } + + storage = value; + + InvalidateCanvasImage(invalidationType); } /// - /// Gets the object currently in use. + /// Indicates the invalidation type to request when invoking and related methods. /// - /// A pointer to the object currently in use. - private void GetCanvasImageInterop(ICanvasImageInterop** canvasImageInterop) + protected enum InvalidationType { - using ComPtr canvasImageUnknown = default; + /// + /// Fully invalidates the effect graph, causing it to be created again the next time it is needed. + /// + /// + /// This will cause the last returned instance to be disposed and discarded, + /// and to be called again the next time the effect is used for drawing. + /// + Creation, - canvasImageUnknown.Attach((IUnknown*)Marshal.GetIUnknownForObject(GetCanvasImage())); - - canvasImageUnknown.CopyTo(canvasImageInterop).Assert(); + /// + /// Invalidates the state of the effect graph, causing it to be configured again the next time it is used. + /// + /// + /// + /// This will preserve the last returned instance, if available, and it will only + /// mark the internal state as being out of date, resulting in to be called + /// the next time the effect is used for drawing. + /// + /// + /// This is much less expensive than creating the effect graph again, and should be preferred if possible. + /// + /// + Update } } \ No newline at end of file From 21723238b74072ca1c47a9df8f84fb358875213b Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 10 Dec 2022 05:37:46 +0100 Subject: [PATCH 3/3] Add [NotNullIfNotNull] polyfill for CanvasEffect --- .../ComputeSharp.D2D1.NetStandard.projitems | 1 + .../NotNullIfNotNullAttribute.cs | 29 +++++++++++++++++++ src/ComputeSharp.D2D1.Uwp/CanvasEffect.cs | 3 +- 3 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 src/ComputeSharp.D2D1.NetStandard/System/Diagnostics.CodeAnalysis/NotNullIfNotNullAttribute.cs diff --git a/src/ComputeSharp.D2D1.NetStandard/ComputeSharp.D2D1.NetStandard.projitems b/src/ComputeSharp.D2D1.NetStandard/ComputeSharp.D2D1.NetStandard.projitems index d973ffe7..4e744618 100644 --- a/src/ComputeSharp.D2D1.NetStandard/ComputeSharp.D2D1.NetStandard.projitems +++ b/src/ComputeSharp.D2D1.NetStandard/ComputeSharp.D2D1.NetStandard.projitems @@ -13,6 +13,7 @@ + diff --git a/src/ComputeSharp.D2D1.NetStandard/System/Diagnostics.CodeAnalysis/NotNullIfNotNullAttribute.cs b/src/ComputeSharp.D2D1.NetStandard/System/Diagnostics.CodeAnalysis/NotNullIfNotNullAttribute.cs new file mode 100644 index 00000000..4629b739 --- /dev/null +++ b/src/ComputeSharp.D2D1.NetStandard/System/Diagnostics.CodeAnalysis/NotNullIfNotNullAttribute.cs @@ -0,0 +1,29 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +namespace System.Diagnostics.CodeAnalysis; + +/// +/// Specifies that the output will be non-null if the named parameter is non-null. +/// +[AttributeUsage( + AttributeTargets.Parameter | + AttributeTargets.Property | + AttributeTargets.ReturnValue, + AllowMultiple = true, Inherited = false)] +internal sealed class NotNullIfNotNullAttribute : Attribute +{ + /// + /// Initializes the attribute with the associated parameter name. + /// + /// The associated parameter name. The output will be non-null if the argument to the parameter specified is non-null. + public NotNullIfNotNullAttribute(string parameterName) + { + ParameterName = parameterName; + } + + /// + /// Gets the associated parameter name. + /// + public string ParameterName { get; } +} \ No newline at end of file diff --git a/src/ComputeSharp.D2D1.Uwp/CanvasEffect.cs b/src/ComputeSharp.D2D1.Uwp/CanvasEffect.cs index 38accdbd..68f39a7f 100644 --- a/src/ComputeSharp.D2D1.Uwp/CanvasEffect.cs +++ b/src/ComputeSharp.D2D1.Uwp/CanvasEffect.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; using ABI.Microsoft.Graphics.Canvas; using Microsoft.Graphics.Canvas; @@ -103,7 +104,7 @@ public abstract partial class CanvasEffect : ICanvasImage, ICanvasImageInterop.I /// The storage for the effect property value. /// The new effect property value to set. /// The invalidation type to request. - protected void SetAndInvalidateCanvasImage(/*[NotNullIfNotNull(nameof(newValue))]*/ ref T storage, T value, InvalidationType invalidationType = InvalidationType.Update) + protected void SetAndInvalidateCanvasImage([NotNullIfNotNull(nameof(value))] ref T storage, T value, InvalidationType invalidationType = InvalidationType.Update) { if (EqualityComparer.Default.Equals(storage, value)) {