Add EffectGraph.SetOutputNode API to change output

このコミットが含まれているのは:
Sergio Pedri committed 2023-04-30 21:18:51 +02:00
1 親 a24f49c0e2
コミット 296ca9f6de
8ファイル変更
+435 -100

検索条件に一致するファイルはありません

+36 -9
ファイルの表示
@@ -80,7 +80,7 @@ partial class CanvasEffect
{
default(ArgumentNullException).ThrowIfNull(canvasImage);
default(InvalidOperationException).ThrowIf(this.owner is null);
default(InvalidOperationException).ThrowIf(!this.owner.isBuildingEffectGraph);
default(InvalidOperationException).ThrowIf(this.owner.invalidationType != InvalidationType.Creation);
// Use a new dummy object as key to register the anonymous effect node. This is cheaper than having to maintain a different
// data structure (eg. a HashSet<ICanvasImage> instance) just to hold the anonymous effects that may be registered. This way,
@@ -109,7 +109,7 @@ partial class CanvasEffect
default(ArgumentNullException).ThrowIfNull(effectNode);
default(ArgumentNullException).ThrowIfNull(canvasImage);
default(InvalidOperationException).ThrowIf(this.owner is null);
default(InvalidOperationException).ThrowIf(!this.owner.isBuildingEffectGraph);
default(InvalidOperationException).ThrowIf(this.owner.invalidationType != InvalidationType.Creation);
// Try to add the new canvas image associated with the input effect node marker.
// This must not have been previously added from another RegisterNode call.
@@ -130,16 +130,15 @@ partial class CanvasEffect
/// correctly track the image ownership, so that it can be disposed when the current effect instance is disposed.
/// </para>
/// <para>
/// If performing lookups on <paramref name="canvasImage"/> is required, use the <see cref="RegisterAndSetOutputNode{T}(EffectNode{T}, T)"/> overload.
/// If performing lookups on <paramref name="canvasImage"/> is required, use the <see cref="RegisterOutputNode{T}(EffectNode{T}, T)"/> overload.
/// </para>
/// </remarks>
/// <inheritdoc cref="RegisterNode(ICanvasImage)"/>
public void RegisterAndSetOutputNode(ICanvasImage canvasImage)
public void RegisterOutputNode(ICanvasImage canvasImage)
{
default(ArgumentNullException).ThrowIfNull(canvasImage);
default(InvalidOperationException).ThrowIf(this.owner is null);
default(InvalidOperationException).ThrowIf(!this.owner.isBuildingEffectGraph);
default(InvalidOperationException).ThrowIf(this.owner.canvasImage is not null);
default(InvalidOperationException).ThrowIf(this.owner.invalidationType != InvalidationType.Creation);
// Register the anonymous effect node with a dummy object, same as above
_ = this.owner.transformNodes.TryAdd(new object(), canvasImage);
@@ -153,14 +152,13 @@ partial class CanvasEffect
/// Additionally, it also marks the input <see cref="ICanvasImage"/> object as the output node for the effect graph being built.
/// </summary>
/// <inheritdoc cref="RegisterNode"/>
public void RegisterAndSetOutputNode<T>(EffectNode<T> effectNode, T canvasImage)
public void RegisterOutputNode<T>(EffectNode<T> effectNode, T canvasImage)
where T : class, ICanvasImage
{
default(ArgumentNullException).ThrowIfNull(effectNode);
default(ArgumentNullException).ThrowIfNull(canvasImage);
default(InvalidOperationException).ThrowIf(this.owner is null);
default(InvalidOperationException).ThrowIf(!this.owner.isBuildingEffectGraph);
default(InvalidOperationException).ThrowIf(this.owner.canvasImage is not null);
default(InvalidOperationException).ThrowIf(this.owner.invalidationType != InvalidationType.Creation);
// Try to add the output node as in the method above (but with an extra check before doing so)
if (!this.owner.transformNodes.TryAdd(effectNode, canvasImage))
@@ -171,6 +169,35 @@ partial class CanvasEffect
// Store the output node for later use
this.owner.canvasImage = canvasImage;
}
/// <summary>
/// Sets a previously registered <see cref="EffectNode{T}"/> instance as the output node for the effect graph.
/// </summary>
/// <typeparam name="T">The type of <see cref="ICanvasImage"/> object that will be used as output image.</typeparam>
/// <param name="effectNode">The <see cref="EffectNode{T}"/> instance to set as output node for the effect graph.</param>
/// <exception cref="ArgumentNullException">Thrown if <paramref name="effectNode"/> is <see langword="null"/>.</exception>
/// <exception cref="InvalidOperationException">Thrown if the current <see cref="EffectGraph"/> instance is not valid.</exception>
/// <exception cref="ArgumentException">Thrown if <paramref name="effectNode"/> is not registered in the effect graph.</exception>
/// <remarks>
/// This method can be called from both <see cref="BuildEffectGraph"/> and <see cref="ConfigureEffectGraph"/>. It can be used for
/// efficiently changing the output node of a graph, without having to rebuild it. For instance, this can be used for effects that
/// have multiple paths that can be taken for their inputs, depending on the value of some property describing the effect behavior.
/// </remarks>
public void SetOutputNode<T>(EffectNode<T> effectNode)
where T : class, ICanvasImage
{
default(ArgumentNullException).ThrowIfNull(effectNode);
default(InvalidOperationException).ThrowIf(this.owner is null);
// Get the canvas image for the registered effect node (which has to exist at this point)
if (!this.owner.transformNodes.TryGetValue(effectNode, out ICanvasImage? canvasImage))
{
default(ArgumentException).Throw(nameof(effectNode), "The specified output node is not registered in the effect graph.");
}
// Store the new output node
this.owner.canvasImage = canvasImage;
}
}
/// <summary>
+14 -9
ファイルの表示
@@ -73,27 +73,32 @@ unsafe partial class CanvasEffect
{
default(ObjectDisposedException).ThrowIf(this.isDisposed, this);
// Build the effect graph and get the output node, if missing
if (this.canvasImage is null)
// Build the effect graph (the output node might not have been set yet)
if (this.invalidationType == InvalidationType.Creation)
{
this.isBuildingEffectGraph = true;
DisposeEffectGraph();
BuildEffectGraph(new EffectGraph(this));
this.isBuildingEffectGraph = false;
default(InvalidOperationException).ThrowIf(this.canvasImage is null);
// We successfully got past the effect graph creation, so update the current
// invalidation state. This ensures that even if the configuration failed, the
// next time the effect is drawn again the graph won't be created again too.
this.invalidationType = InvalidationType.Update;
}
// Configure the effect graph, if the effect is invalidated
if (this.isInvalidated)
if (this.invalidationType == InvalidationType.Update)
{
ConfigureEffectGraph(new EffectGraph(this));
this.isInvalidated = false;
// The effect graph is now ready to go: no further work will be done before drawing
// unless it is explicitly invalidated again, using either creation or update mode.
this.invalidationType = default;
}
// At this point, there must be an output canvas image being set.
// If not, it means a derived type has forgot to set an output node.
default(InvalidOperationException).ThrowIf(this.canvasImage is null);
return this.canvasImage;
}
}
+20 -38
ファイルの表示
@@ -29,22 +29,9 @@ public abstract partial class CanvasEffect : ICanvasImage, ICanvasImageInterop.I
private ICanvasImage? canvasImage;
/// <summary>
/// Indicates whether the current instance is within a call to <see cref="BuildEffectGraph"/>.
/// Indicates the current invalidation state for the effect graph (which controls the logic in <see cref="GetCanvasImage"/>).
/// </summary>
/// <remarks>
/// This is used to validate method calls from within <see cref="EffectGraph"/>. That is, the
/// <see cref="EffectGraph.RegisterNode"/> APIs will use this to detect whether they can be called.
/// </remarks>
private bool isBuildingEffectGraph;
/// <summary>
/// Indicates whether the current state has been invalidated (requiring <see cref="ConfigureEffectGraph"/> to be called).
/// </summary>
/// <remarks>
/// This is initially <see langword="true"/> so that <see cref="ConfigureEffectGraph"/> will always be called when
/// first creating the image, even if the effect has not bee invalidated. This ensures default parameters are set.
/// </remarks>
private bool isInvalidated = true;
private InvalidationType invalidationType = InvalidationType.Creation;
/// <summary>
/// Indicates whether the effect is disposed.
@@ -134,18 +121,13 @@ public abstract partial class CanvasEffect : ICanvasImage, ICanvasImageInterop.I
{
lock (this.transformNodes)
{
// 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;
}
// For both invalidation types, always 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 matches the default value for the field (which is needed for the first realization).
this.isInvalidated = true;
// This method only updates the invalidation type. The next time the effect is drawn,
// the current graph (if existing) will be disposed automatically before building a
// new one. If the invalidation mode just needs an update, no disposal will happen.
// The new invalidation type is always set to the maximum between the current one and
// the requested one. This means that eg. if the current invalidation type is creation,
// and a property only needing an update is set, the creation request will persist.
this.invalidationType = (InvalidationType)Math.Max((byte)this.invalidationType, (byte)invalidationType);
}
}
@@ -227,17 +209,8 @@ public abstract partial class CanvasEffect : ICanvasImage, ICanvasImageInterop.I
/// <summary>
/// Indicates the invalidation type to request when invoking <see cref="InvalidateEffectGraph"/> and related methods.
/// </summary>
protected enum InvalidationType
protected enum InvalidationType : byte
{
/// <summary>
/// Fully invalidates the effect graph, causing it to be created again the next time it is needed.
/// </summary>
/// <remarks>
/// This will cause the last returned <see cref="ICanvasImage"/> instance to be disposed and discarded,
/// and <see cref="BuildEffectGraph"/> to be called again the next time the effect is used for drawing.
/// </remarks>
Creation,
/// <summary>
/// Invalidates the state of the effect graph, causing it to be configured again the next time it is used.
/// </summary>
@@ -251,6 +224,15 @@ public abstract partial class CanvasEffect : ICanvasImage, ICanvasImageInterop.I
/// This is much less expensive than creating the effect graph again, and should be preferred if possible.
/// </para>
/// </remarks>
Update
Update,
/// <summary>
/// Fully invalidates the effect graph, causing it to be created again the next time it is needed.
/// </summary>
/// <remarks>
/// This will cause the last returned <see cref="ICanvasImage"/> instance to be disposed and discarded,
/// and <see cref="BuildEffectGraph"/> to be called again the next time the effect is used for drawing.
/// </remarks>
Creation
}
}