Merge pull request #500 from Sergio0694/dev/canvas-effect-tweaks
Add "official" Disposable pattern to CanvasEffect
このコミットが含まれているのは:
2ファイル変更
+69
-17
検索条件に一致するファイルはありません
@@ -9,8 +9,6 @@ using TerraFX.Interop.Windows;
|
||||
using Windows.Foundation;
|
||||
using ICanvasResourceCreator = Microsoft.Graphics.Canvas.ICanvasResourceCreator;
|
||||
|
||||
#pragma warning disable CA1063
|
||||
|
||||
namespace ComputeSharp.D2D1.Uwp;
|
||||
|
||||
/// <inheritdoc/>
|
||||
@@ -28,17 +26,6 @@ unsafe partial class CanvasEffect
|
||||
return GetCanvasImage().GetBounds(resourceCreator, transform);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void Dispose()
|
||||
{
|
||||
lock (this.lockObject)
|
||||
{
|
||||
this.canvasImage?.Dispose();
|
||||
this.canvasImage = null;
|
||||
this.isDisposed = true;
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
unsafe int ICanvasImageInterop.Interface.GetDevice(ICanvasDevice** device, WIN2D_GET_DEVICE_ASSOCIATION_TYPE* type)
|
||||
{
|
||||
@@ -50,7 +37,13 @@ unsafe partial class CanvasEffect
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
unsafe int ICanvasImageInterop.Interface.GetD2DImage(ICanvasDevice* device, ID2D1DeviceContext* deviceContext, WIN2D_GET_D2D_IMAGE_FLAGS flags, float targetDpi, float* realizeDpi, ID2D1Image** ppImage)
|
||||
unsafe int ICanvasImageInterop.Interface.GetD2DImage(
|
||||
ICanvasDevice* device,
|
||||
ID2D1DeviceContext* deviceContext,
|
||||
WIN2D_GET_D2D_IMAGE_FLAGS flags,
|
||||
float targetDpi,
|
||||
float* realizeDpi,
|
||||
ID2D1Image** ppImage)
|
||||
{
|
||||
using ComPtr<ICanvasImageInterop> canvasImageInterop = default;
|
||||
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
using System;
|
||||
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;
|
||||
|
||||
/// <summary>
|
||||
@@ -25,13 +24,73 @@ public abstract partial class CanvasEffect : ICanvasImage, ICanvasImageInterop.I
|
||||
/// <summary>
|
||||
/// Indicates whether the current state has been invalidated (requiring <see cref="ConfigureCanvasImage"/> to be called).
|
||||
/// </summary>
|
||||
private bool isInvalidated;
|
||||
/// <remarks>
|
||||
/// This is initially <see langword="true"/> so that <see cref="ConfigureCanvasImage"/> 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;
|
||||
|
||||
/// <summary>
|
||||
/// Indicates whether the effect is disposed.
|
||||
/// </summary>
|
||||
private bool isDisposed;
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(disposing: true);
|
||||
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Disposes any disposable resources in the current instance.
|
||||
/// </summary>
|
||||
/// <param name="disposing">
|
||||
/// Indicates whether the method was called from <see cref="Dispose()"/> of from a finalizer:
|
||||
/// <list type="bullet">
|
||||
/// <item><see langword="true"/>: the method was called from <see cref="Dispose()"/>.</item>
|
||||
/// <item><see langword="false"/>: the method was called from a finalizer.</item>
|
||||
/// </list>
|
||||
/// </param>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// If implementing a type that derives from <see cref="CanvasEffect"/>, this method can be overridden to add logic to dispose
|
||||
/// any additional state. Managed objects should only be disposed when <paramref name="disposing"/> is <see langword="true"/>.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// If the derived type does not need a finalizer (ie. if it doesn't wrap any native resources), this method should not be
|
||||
/// called directly. It will be called automatically by <see cref="Dispose()"/>. If the derived type does instead define a
|
||||
/// finalizer, it should call <see cref="Dispose(bool)"/> with a value of <see langword="false"/>. As mentioned above, when
|
||||
/// implementing <see cref="Dispose(bool)"/> in a derived type, managed objects should only be disposed when
|
||||
/// <paramref name="disposing"/> is <see langword="true"/>, otherwise only unmanaged resorces should be disposed.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// Though the pattern does support this, it is recommended not to add finalizers in types deriving from <see cref="CanvasEffect"/>.
|
||||
/// Rather, if an effect needs to wrap native resources, it should do so via some same managed wrapper type (such as
|
||||
/// <see cref="System.Runtime.InteropServices.SafeHandle"/>), or some other custom unmanaged resource wrapper type.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
/// <seealso href="https://learn.microsoft.com/en-us/dotnet/standard/garbage-collection/implementing-dispose"/>
|
||||
protected virtual void Dispose(bool disposing)
|
||||
{
|
||||
if (disposing)
|
||||
{
|
||||
// Only take a lock and dispose the image if the method was called explicitly from user code. If instead it was
|
||||
// called from the finalizer, we can make no guarantees on the order of objects being collected, meaning that
|
||||
// the lock itself might have already been collected by the time this code runs, which would lead the lock
|
||||
// statement to throw a NullReferenceException. So if a finalizer is running, just let objects be collected
|
||||
// on their own. This is fine here since there are no unmanaged references to free, but just managed wrappers.
|
||||
lock (this.lockObject)
|
||||
{
|
||||
this.canvasImage?.Dispose();
|
||||
this.canvasImage = null;
|
||||
}
|
||||
}
|
||||
|
||||
this.isDisposed = true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates the resulting <see cref="ICanvasImage"/> representing the output node of the effect graph for this <see cref="CanvasEffect"/> instance.
|
||||
/// </summary>
|
||||
|
||||
新しいイシューから参照
ユーザーをブロックする