using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using ABI.Microsoft.Graphics.Canvas; using Microsoft.Graphics.Canvas; #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 partial 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; /// /// Indicates whether the current state has been invalidated (requiring to be called). /// private bool isInvalidated; /// /// 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 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. /// protected enum InvalidationType { /// /// 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, /// /// 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 } }