Improve CanvasEffect public API
このコミットが含まれているのは:
1 親
24b54273c8
コミット
4cc15152fe
2ファイル変更
+203
-62
検索条件に一致するファイルはありません
@@ -0,0 +1,111 @@
|
||||
using System;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Numerics;
|
||||
using System.Runtime.InteropServices;
|
||||
using ABI.Microsoft.Graphics.Canvas;
|
||||
using ComputeSharp.D2D1.Extensions;
|
||||
using Microsoft.Graphics.Canvas;
|
||||
using TerraFX.Interop.DirectX;
|
||||
using TerraFX.Interop.Windows;
|
||||
using Windows.Foundation;
|
||||
using ICanvasResourceCreator = Microsoft.Graphics.Canvas.ICanvasResourceCreator;
|
||||
|
||||
#pragma warning disable CA1063
|
||||
|
||||
namespace ComputeSharp.D2D1.Uwp;
|
||||
|
||||
/// <inheritdoc/>
|
||||
unsafe partial class CanvasEffect
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
public Rect GetBounds(ICanvasResourceCreator resourceCreator)
|
||||
{
|
||||
return GetCanvasImage().GetBounds(resourceCreator);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public Rect GetBounds(ICanvasResourceCreator resourceCreator, Matrix3x2 transform)
|
||||
{
|
||||
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)
|
||||
{
|
||||
using ComPtr<ICanvasImageInterop> canvasImageInterop = default;
|
||||
|
||||
GetCanvasImageInterop(canvasImageInterop.GetAddressOf());
|
||||
|
||||
return canvasImageInterop.Get()->GetDevice(device);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
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;
|
||||
|
||||
GetCanvasImageInterop(canvasImageInterop.GetAddressOf());
|
||||
|
||||
return canvasImageInterop.Get()->GetD2DImage(device, deviceContext, flags, targetDpi, realizeDpi, ppImage);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or creates the current <see cref="ICanvasImage"/> instance.
|
||||
/// </summary>
|
||||
/// <returns>The current <see cref="ICanvasImage"/> instance.</returns>
|
||||
/// <exception cref="ObjectDisposedException">Thrown if the current instance is disposed.</exception>
|
||||
private ICanvasImage GetCanvasImage()
|
||||
{
|
||||
lock (this.lockObject)
|
||||
{
|
||||
if (this.isDisposed)
|
||||
{
|
||||
ThrowObjectDisposedException();
|
||||
}
|
||||
|
||||
this.canvasImage ??= CreateCanvasImage();
|
||||
|
||||
if (this.isInvalidated)
|
||||
{
|
||||
ConfigureCanvasImage();
|
||||
|
||||
this.isInvalidated = false;
|
||||
}
|
||||
|
||||
return this.canvasImage;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the <see cref="ICanvasImageInterop"/> object currently in use.
|
||||
/// </summary>
|
||||
/// <param name="canvasImageInterop">A pointer to the <see cref="ICanvasImageInterop"/> object currently in use.</param>
|
||||
private void GetCanvasImageInterop(ICanvasImageInterop** canvasImageInterop)
|
||||
{
|
||||
using ComPtr<IUnknown> canvasImageUnknown = default;
|
||||
|
||||
canvasImageUnknown.Attach((IUnknown*)Marshal.GetIUnknownForObject(GetCanvasImage()));
|
||||
|
||||
canvasImageUnknown.CopyTo(canvasImageInterop).Assert();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws a new <see cref="ObjectDisposedException"/>.
|
||||
/// </summary>
|
||||
[DoesNotReturn]
|
||||
public void ThrowObjectDisposedException()
|
||||
{
|
||||
throw new ObjectDisposedException(ToString(), "The current effect has been disposed and can no longer be used.");
|
||||
}
|
||||
}
|
||||
@@ -1,12 +1,6 @@
|
||||
using System.Numerics;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Collections.Generic;
|
||||
using ABI.Microsoft.Graphics.Canvas;
|
||||
using ComputeSharp.D2D1.Extensions;
|
||||
using Microsoft.Graphics.Canvas;
|
||||
using TerraFX.Interop.DirectX;
|
||||
using TerraFX.Interop.Windows;
|
||||
using Windows.Foundation;
|
||||
using ICanvasResourceCreator = Microsoft.Graphics.Canvas.ICanvasResourceCreator;
|
||||
|
||||
#pragma warning disable CA1063
|
||||
|
||||
@@ -15,7 +9,7 @@ namespace ComputeSharp.D2D1.Uwp;
|
||||
/// <summary>
|
||||
/// A base type to implement packaged and easy to use <see cref="ICanvasImage"/>-based effects that can be used with Win2D.
|
||||
/// </summary>
|
||||
public abstract unsafe class CanvasEffect : ICanvasImage, ICanvasImageInterop.Interface
|
||||
public abstract partial class CanvasEffect : ICanvasImage, ICanvasImageInterop.Interface
|
||||
{
|
||||
/// <summary>
|
||||
/// Lock object used to synchronize calls to <see cref="ICanvasImageInterop"/> APIs.
|
||||
@@ -27,91 +21,127 @@ public abstract unsafe class CanvasEffect : ICanvasImage, ICanvasImageInterop.In
|
||||
/// </summary>
|
||||
private ICanvasImage? canvasImage;
|
||||
|
||||
/// <inheritdoc/>
|
||||
public Rect GetBounds(ICanvasResourceCreator resourceCreator)
|
||||
{
|
||||
return GetCanvasImage().GetBounds(resourceCreator);
|
||||
}
|
||||
/// <summary>
|
||||
/// Indicates whether the current state has been invalidated (requiring <see cref="ConfigureCanvasImage"/> to be called).
|
||||
/// </summary>
|
||||
private bool isInvalidated;
|
||||
|
||||
/// <inheritdoc/>
|
||||
public Rect GetBounds(ICanvasResourceCreator resourceCreator, Matrix3x2 transform)
|
||||
{
|
||||
return GetCanvasImage().GetBounds(resourceCreator, transform);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void Dispose()
|
||||
{
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
unsafe int ICanvasImageInterop.Interface.GetDevice(ICanvasDevice** device)
|
||||
{
|
||||
using ComPtr<ICanvasImageInterop> canvasImageInterop = default;
|
||||
|
||||
GetCanvasImageInterop(canvasImageInterop.GetAddressOf());
|
||||
|
||||
return canvasImageInterop.Get()->GetDevice(device);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
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;
|
||||
|
||||
GetCanvasImageInterop(canvasImageInterop.GetAddressOf());
|
||||
|
||||
return canvasImageInterop.Get()->GetD2DImage(device, deviceContext, flags, targetDpi, realizeDpi, ppImage);
|
||||
}
|
||||
/// <summary>
|
||||
/// Indicates whether the effect is disposed.
|
||||
/// </summary>
|
||||
private bool isDisposed;
|
||||
|
||||
/// <summary>
|
||||
/// Creates the resulting <see cref="ICanvasImage"/> representing the output node of the effect graph for this <see cref="CanvasEffect"/> instance.
|
||||
/// </summary>
|
||||
/// <returns>The resulting <see cref="ICanvasImage"/> representing the output node of the effect graph for this <see cref="CanvasEffect"/> instance.</returns>
|
||||
/// <remarks>
|
||||
/// This method is only called once and its result is cached until explicitly invalidated by calling <see cref="InvalidateCanvasImage"/>.
|
||||
/// <para>
|
||||
/// This method is only called once and its result is cached until explicitly invalidated by calling <see cref="InvalidateCanvasImage"/> and related methods.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// This method should never be called directly. Instead, it is used internally to produce an <see cref="ICanvasImage"/> instance when the effect is used.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
protected abstract ICanvasImage CreateCanvasImage();
|
||||
|
||||
/// <summary>
|
||||
/// Invalidates the last returned result from <see cref="GetCanvasImage"/>.
|
||||
/// Configures the current <see cref="ICanvasImage"/> instance when the effect is used.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This method is used to signal when the entire effect graph should be updated.
|
||||
/// When called, the internal cache for <see cref="GetCanvasImage"/> will be reset.
|
||||
/// <para>
|
||||
/// This method is guaranteed to be called after <see cref="CreateCanvasImage"/> has been invoked already.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// This method should never be called directly. Instead, it is used internally to configure the current <see cref="ICanvasImage"/> instance when the effect is used.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
protected void InvalidateCanvasImage()
|
||||
protected abstract void ConfigureCanvasImage();
|
||||
|
||||
/// <summary>
|
||||
/// Invalidates the last returned result from <see cref="GetCanvasImage"/>.
|
||||
/// </summary>
|
||||
/// <param name="invalidationType">The invalidation type to request.</param>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// 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 <see cref="GetCanvasImage"/>),
|
||||
/// or whether it simply needs to refresh its internal state (by calling <see cref="ConfigureCanvasImage"/>).
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// If <see cref="InvalidateCanvasImage"/> is called with <see cref="InvalidationType.Update"/>, 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 <see cref="ConfigureCanvasImage"/>.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
protected void InvalidateCanvasImage(InvalidationType invalidationType = InvalidationType.Update)
|
||||
{
|
||||
lock (this.lockObject)
|
||||
{
|
||||
this.canvasImage?.Dispose();
|
||||
// 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.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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or creates the current <see cref="ICanvasImage"/> instance.
|
||||
/// 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.
|
||||
/// </summary>
|
||||
/// <returns>The current <see cref="ICanvasImage"/> instance.</returns>
|
||||
private ICanvasImage GetCanvasImage()
|
||||
/// <typeparam name="T">The type of the property that changed.</typeparam>
|
||||
/// <param name="storage">The storage for the effect property value.</param>
|
||||
/// <param name="value">The new effect property value to set.</param>
|
||||
/// <param name="invalidationType">The invalidation type to request.</param>
|
||||
protected void SetAndInvalidateCanvasImage<T>(/*[NotNullIfNotNull(nameof(newValue))]*/ ref T storage, T value, InvalidationType invalidationType = InvalidationType.Update)
|
||||
{
|
||||
lock (this.lockObject)
|
||||
if (EqualityComparer<T>.Default.Equals(storage, value))
|
||||
{
|
||||
return this.canvasImage ??= CreateCanvasImage();
|
||||
return;
|
||||
}
|
||||
|
||||
storage = value;
|
||||
|
||||
InvalidateCanvasImage(invalidationType);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the <see cref="ICanvasImageInterop"/> object currently in use.
|
||||
/// Indicates the invalidation type to request when invoking <see cref="InvalidateCanvasImage"/> and related methods.
|
||||
/// </summary>
|
||||
/// <param name="canvasImageInterop">A pointer to the <see cref="ICanvasImageInterop"/> object currently in use.</param>
|
||||
private void GetCanvasImageInterop(ICanvasImageInterop** canvasImageInterop)
|
||||
protected enum InvalidationType
|
||||
{
|
||||
using ComPtr<IUnknown> canvasImageUnknown = default;
|
||||
/// <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="CreateCanvasImage"/> to be called again the next time the effect is used for drawing.
|
||||
/// </remarks>
|
||||
Creation,
|
||||
|
||||
canvasImageUnknown.Attach((IUnknown*)Marshal.GetIUnknownForObject(GetCanvasImage()));
|
||||
|
||||
canvasImageUnknown.CopyTo(canvasImageInterop).Assert();
|
||||
/// <summary>
|
||||
/// Invalidates the state of the effect graph, causing it to be configured again the next time it is used.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// This will preserve the last returned <see cref="ICanvasImage"/> instance, if available, and it will only
|
||||
/// mark the internal state as being out of date, resulting in <see cref="ConfigureCanvasImage"/> to be called
|
||||
/// the next time the effect is used for drawing.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// This is much less expensive than creating the effect graph again, and should be preferred if possible.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
Update
|
||||
}
|
||||
}
|
||||
新しいイシューから参照
ユーザーをブロックする