From 70b63d994242f3649771d6c2077366e6af42bfbe Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 12 Mar 2023 02:43:32 +0100 Subject: [PATCH] Rename CanvasEffect setup and update methods --- .../CanvasEffect.Interop.cs | 4 +- src/ComputeSharp.D2D1.Uwp/CanvasEffect.cs | 210 ++++++++++-------- 2 files changed, 123 insertions(+), 91 deletions(-) diff --git a/src/ComputeSharp.D2D1.Uwp/CanvasEffect.Interop.cs b/src/ComputeSharp.D2D1.Uwp/CanvasEffect.Interop.cs index 9c63e38f..875e71a3 100644 --- a/src/ComputeSharp.D2D1.Uwp/CanvasEffect.Interop.cs +++ b/src/ComputeSharp.D2D1.Uwp/CanvasEffect.Interop.cs @@ -63,11 +63,11 @@ unsafe partial class CanvasEffect { default(ObjectDisposedException).ThrowIf(this.isDisposed, this); - this.canvasImage ??= CreateCanvasImage(); + this.canvasImage ??= BuildEffectGraph(); if (this.isInvalidated) { - ConfigureCanvasImage(); + ConfigureEffectGraph(); this.isInvalidated = false; } diff --git a/src/ComputeSharp.D2D1.Uwp/CanvasEffect.cs b/src/ComputeSharp.D2D1.Uwp/CanvasEffect.cs index 7439f19e..96bf107c 100644 --- a/src/ComputeSharp.D2D1.Uwp/CanvasEffect.cs +++ b/src/ComputeSharp.D2D1.Uwp/CanvasEffect.cs @@ -22,10 +22,10 @@ public abstract partial class CanvasEffect : ICanvasImage, ICanvasImageInterop.I private ICanvasImage? canvasImage; /// - /// Indicates whether the current state has been invalidated (requiring to be called). + /// Indicates whether the current state has been invalidated (requiring to be called). /// /// - /// This is initially so that will always be called when + /// This is initially so that will always be called when /// first creating the image, even if the effect has not bee invalidated. This ensures default parameters are set. /// private bool isInvalidated = true; @@ -35,6 +35,122 @@ public abstract partial class CanvasEffect : ICanvasImage, ICanvasImageInterop.I /// private bool isDisposed; + /// + /// Builds the effect graph for the current instance, and returns an object + /// representing the output node of the graph. This image will then be passed to Win2D to perform the actual drawing, when needed. + /// + /// The resulting object representing the output node of the effect graph. + /// + /// + /// This method is called once before the current effect is drawn, and the resulting image is automatically cached and reused. + /// It will remain in use until is called with . + /// + /// + /// If the effect is invalidated with , only will be + /// called. As such, derived types should save any effect graph nodes that might need updates into instance fields, for later use. + /// + /// + /// For instance, consider a FrostedGlassEffect type deriving from , with this effect graph: + /// + /// ┌────────┐ ┌──────┐ ┌──────┐ ┌───────┐ ┌────────┐ + /// │ source ├──►│ blur ├──►│ tint ├──►│ noise ├──►│ output │ + /// └────────┘ └──────┘ └──────┘ └───────┘ └────────┘ + /// + /// + /// + /// In this example, blur is a instance, tint + /// is a . and noise is some other custom effect. In this case, + /// the instance returned by will be noise, as that's the output + /// node for the effect graph. At the same time, both blur and tint will also need to be saved as instance fields, + /// so that will be able to set properties on them when needed. For instance, the effect might + /// expose a property to control the blur amount, as well as the tint color and opacity. + /// + /// + /// Generally speaking, an implementation of will consist of these steps: + /// + /// Create an instance of all necessary nodes in the effect graph. + /// Connect the effect nodes as needed to build the connected graph. + /// Save all effects that can be configured as instance fields in the effect type. + /// Return the instance for the output node of the graph. + /// + /// + /// + /// This method should never be called directly. It is automatically invoked when an is needed. + /// + /// + protected abstract ICanvasImage BuildEffectGraph(); + + /// + /// Configures the current effect graph whenever it is invalidated. + /// + /// + /// + /// This method is guaranteed to be called after has been invoked already. As such, any instance + /// fields that are set by can be assumed to never be when this method runs. + /// + /// + /// This method should never be called directly. It is used internally to configure the current instance. + /// + /// + protected abstract void ConfigureEffectGraph(); + + /// + /// 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 InvalidateEffectGraph(InvalidationType invalidationType = InvalidationType.Update) + { + lock (this.lockObject) + { + // 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.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; + } + } + } + + /// + /// 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 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 SetAndInvalidateEffectGraph([NotNullIfNotNull(nameof(value))] ref T storage, T value, InvalidationType invalidationType = InvalidationType.Update) + { + if (EqualityComparer.Default.Equals(storage, value)) + { + return; + } + + storage = value; + + InvalidateEffectGraph(invalidationType); + } + /// public void Dispose() { @@ -92,91 +208,7 @@ public abstract partial class CanvasEffect : ICanvasImage, ICanvasImageInterop.I } /// - /// 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 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(); - - /// - /// Configures the current instance when the effect is used. - /// - /// - /// - /// 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 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) - { - // 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.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; - } - } - } - - /// - /// 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 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(value))] ref T storage, T value, InvalidationType invalidationType = InvalidationType.Update) - { - if (EqualityComparer.Default.Equals(storage, value)) - { - return; - } - - storage = value; - - InvalidateCanvasImage(invalidationType); - } - - /// - /// Indicates the invalidation type to request when invoking and related methods. + /// Indicates the invalidation type to request when invoking and related methods. /// protected enum InvalidationType { @@ -185,7 +217,7 @@ public abstract partial class CanvasEffect : ICanvasImage, ICanvasImageInterop.I /// /// /// 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. + /// and to be called again the next time the effect is used for drawing. /// Creation, @@ -195,7 +227,7 @@ public abstract partial class CanvasEffect : ICanvasImage, ICanvasImageInterop.I /// /// /// 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 + /// mark the internal state as being out of date, resulting in to be called /// the next time the effect is used for drawing. /// ///