Add EffectGraph checks for invalid state (default)

このコミットが含まれているのは:
Sergio Pedri committed 2023-04-30 16:18:57 +02:00
1 親 0f1e6dc11a
コミット a24f49c0e2
2ファイル変更
+51 -2

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

+12 -2
ファイルの表示
@@ -38,10 +38,12 @@ partial class CanvasEffect
/// <returns>The <see cref="ICanvasImage"/> object associated with <paramref name="effectNode"/> in the effect graph.</returns>
/// <exception cref="ArgumentNullException">Thrown if <paramref name="effectNode"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentException">Thrown if <paramref name="effectNode"/> is not currently registered in the effect graph.</exception>
/// <exception cref="InvalidOperationException">Thrown if the current <see cref="EffectGraph"/> instance is not valid.</exception>
public T GetNode<T>(EffectNode<T> effectNode)
where T : class, ICanvasImage
{
default(ArgumentNullException).ThrowIfNull(effectNode);
default(InvalidOperationException).ThrowIf(this.owner is null);
// Try to get the canvas image associated with the input effect node marker.
// This must have been previously registered in a call to BuildEffectGraph.
@@ -60,7 +62,9 @@ partial class CanvasEffect
/// </summary>
/// <param name="canvasImage">The <see cref="ICanvasImage"/> object to register in the effect graph.</param>
/// <exception cref="ArgumentNullException">Thrown if <paramref name="canvasImage"/> is <see langword="null"/>.</exception>
/// <exception cref="InvalidOperationException">Thrown if the effect graph does not support modifications at this time.</exception>
/// <exception cref="InvalidOperationException">
/// Thrown if the current <see cref="EffectGraph"/> instance is not valid or if the effect graph does not support modifications at this time.
/// </exception>
/// <remarks>
/// <para><inheritdoc cref="RegisterNode{T}(EffectNode{T}, T)" path="/remarks/node()"/></para>
/// <para>
@@ -75,6 +79,7 @@ partial class CanvasEffect
public void RegisterNode(ICanvasImage canvasImage)
{
default(ArgumentNullException).ThrowIfNull(canvasImage);
default(InvalidOperationException).ThrowIf(this.owner is null);
default(InvalidOperationException).ThrowIf(!this.owner.isBuildingEffectGraph);
// Use a new dummy object as key to register the anonymous effect node. This is cheaper than having to maintain a different
@@ -90,7 +95,9 @@ partial class CanvasEffect
/// <param name="effectNode">The <see cref="EffectNode{T}"/> instance to use to register <paramref name="canvasImage"/>.</param>
/// <param name="canvasImage">The <see cref="ICanvasImage"/> object to register in the effect graph.</param>
/// <exception cref="ArgumentNullException">Thrown if <paramref name="effectNode"/> or <paramref name="canvasImage"/> are <see langword="null"/>.</exception>
/// <exception cref="InvalidOperationException">Thrown if the effect graph does not support modifications at this time.</exception>
/// <exception cref="InvalidOperationException">
/// Thrown if the current <see cref="EffectGraph"/> instance is not valid or if the effect graph does not support modifications at this time.
/// </exception>
/// <exception cref="ArgumentException">Thrown if <paramref name="effectNode"/> is already registered in the effect graph.</exception>
/// <remarks>
/// This method can only be called from an <see cref="EffectGraph"/> value being passed to <see cref="BuildEffectGraph"/>. If this
@@ -101,6 +108,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);
// Try to add the new canvas image associated with the input effect node marker.
@@ -129,6 +137,7 @@ partial class CanvasEffect
public void RegisterAndSetOutputNode(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);
@@ -149,6 +158,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.canvasImage is not null);
+39
ファイルの表示
@@ -146,6 +146,17 @@ public partial class CanvasEffectTests
drawingSession.DrawImage(effect);
}
[TestMethod]
public unsafe void CanvasEffect_EffectUsingEffectGraphInInvalidState()
{
EffectUsingEffectGraphInInvalidState effect = new();
using CanvasRenderTarget renderTarget = new(new CanvasDevice(), 128, 128, 96.0f);
using CanvasDrawingSession drawingSession = renderTarget.CreateDrawingSession();
drawingSession.DrawImage(effect);
}
private sealed class EffectWithNoInputs : CanvasEffect
{
private static readonly EffectNode<PixelShaderEffect<ShaderWithNoInputs>> Effect = new();
@@ -258,6 +269,16 @@ public partial class CanvasEffectTests
{
protected override unsafe void BuildEffectGraph(EffectGraph effectGraph)
{
// Verify that if the effect graph is invalid, arguments are validated first
_ = Assert.ThrowsException<ArgumentNullException>(() => default(EffectGraph).RegisterNode(null!));
_ = Assert.ThrowsException<ArgumentNullException>(() => default(EffectGraph).RegisterNode(null!, new ColorSourceEffect()));
_ = Assert.ThrowsException<ArgumentNullException>(() => default(EffectGraph).RegisterNode(new EffectNode<ColorSourceEffect>(), null!));
_ = Assert.ThrowsException<ArgumentNullException>(() => default(EffectGraph).RegisterNode(null!, (ColorSourceEffect?)null!));
_ = Assert.ThrowsException<ArgumentNullException>(() => default(EffectGraph).RegisterAndSetOutputNode(null!));
_ = Assert.ThrowsException<ArgumentNullException>(() => default(EffectGraph).RegisterAndSetOutputNode(null!, new ColorSourceEffect()));
_ = Assert.ThrowsException<ArgumentNullException>(() => default(EffectGraph).RegisterAndSetOutputNode(new EffectNode<ColorSourceEffect>(), null!));
_ = Assert.ThrowsException<ArgumentNullException>(() => default(EffectGraph).RegisterAndSetOutputNode(null!, (ColorSourceEffect)null!));
void* ptr = &effectGraph;
_ = Assert.ThrowsException<ArgumentNullException>(() => (*(EffectGraph*)ptr).RegisterNode(null!));
@@ -324,6 +345,24 @@ public partial class CanvasEffectTests
}
}
private sealed class EffectUsingEffectGraphInInvalidState : CanvasEffect
{
protected override void BuildEffectGraph(EffectGraph effectGraph)
{
_ = Assert.ThrowsException<InvalidOperationException>(() => default(EffectGraph).RegisterNode(new ColorSourceEffect()));
_ = Assert.ThrowsException<InvalidOperationException>(() => default(EffectGraph).RegisterNode(new EffectNode<ColorSourceEffect>(), new ColorSourceEffect()));
_ = Assert.ThrowsException<InvalidOperationException>(() => default(EffectGraph).RegisterAndSetOutputNode(new ColorSourceEffect()));
_ = Assert.ThrowsException<InvalidOperationException>(() => default(EffectGraph).RegisterAndSetOutputNode(new EffectNode<ColorSourceEffect>(), new ColorSourceEffect()));
effectGraph.RegisterAndSetOutputNode(new EffectNode<ColorSourceEffect>(), new ColorSourceEffect());
}
protected override void ConfigureEffectGraph(EffectGraph effectGraph)
{
_ = Assert.ThrowsException<InvalidOperationException>(() => default(EffectGraph).GetNode(new EffectNode<ColorSourceEffect>()));
}
}
[D2DInputCount(0)]
[AutoConstructor]
private partial struct ShaderWithNoInputs : ID2D1PixelShader