Merge pull request #823 from Sergio0694/dev/canvas-animated-control

Use 'CanvasAnimatedControl' from Win2D in WinUI sample
このコミットが含まれているのは:
Sergio Pedri authored and GitHub committed 2024-08-13 21:59:00 -07:00
コミット b79d650636
6ファイル変更
+37 -217

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

+36 -36
ファイルの表示
@@ -12,6 +12,24 @@ namespace ComputeSharp.SwapChain.WinUI.Views;
[TemplatePart(Name = "PART_CanvasAnimatedControl", Type = typeof(CanvasAnimatedControl))]
public sealed class D2D1AnimatedPixelShaderPanel : Control
{
/// <summary>
/// The <see cref="DependencyProperty"/> backing <see cref="PixelShaderEffect"/>.
/// </summary>
public static readonly DependencyProperty PixelShaderEffectProperty = DependencyProperty.Register(
nameof(PixelShaderEffect),
typeof(PixelShaderEffect),
typeof(D2D1AnimatedPixelShaderPanel),
new PropertyMetadata(null, OnPixelShaderEffectPropertyChanged));
/// <summary>
/// The <see cref="DependencyProperty"/> backing <see cref="IsPaused"/>.
/// </summary>
public static readonly DependencyProperty IsPausedProperty = DependencyProperty.Register(
nameof(IsPaused),
typeof(bool),
typeof(D2D1AnimatedPixelShaderPanel),
new PropertyMetadata(false, OnIsPausedPropertyChanged));
/// <summary>
/// The wrapped <see cref="CanvasAnimatedControl"/> instance used to render frames.
/// </summary>
@@ -33,6 +51,24 @@ public sealed class D2D1AnimatedPixelShaderPanel : Control
Unloaded += D2D1AnimatedPixelShaderPanel_Unloaded;
}
/// <summary>
/// Gets or sets the <see cref="Core.Shaders.PixelShaderEffect"/> instance to use to render content.
/// </summary>
public PixelShaderEffect? PixelShaderEffect
{
get => (PixelShaderEffect?)GetValue(PixelShaderEffectProperty);
set => SetValue(PixelShaderEffectProperty, value);
}
/// <summary>
/// Gets or sets whether or not the rendering is paused.
/// </summary>
public bool IsPaused
{
get => (bool)GetValue(IsPausedProperty);
set => SetValue(IsPausedProperty, value);
}
/// <inheritdoc/>
protected override void OnApplyTemplate()
{
@@ -78,24 +114,6 @@ public sealed class D2D1AnimatedPixelShaderPanel : Control
args.DrawingSession.DrawImage(this.pixelShaderEffect);
}
/// <summary>
/// Gets or sets the <see cref="Core.Shaders.PixelShaderEffect"/> instance to use to render content.
/// </summary>
public PixelShaderEffect? PixelShaderEffect
{
get => (PixelShaderEffect?)GetValue(PixelShaderEffectProperty);
set => SetValue(PixelShaderEffectProperty, value);
}
/// <summary>
/// The <see cref="DependencyProperty"/> backing <see cref="PixelShaderEffect"/>.
/// </summary>
public static readonly DependencyProperty PixelShaderEffectProperty = DependencyProperty.Register(
nameof(PixelShaderEffect),
typeof(PixelShaderEffect),
typeof(D2D1AnimatedPixelShaderPanel),
new PropertyMetadata(null, OnPixelShaderEffectPropertyChanged));
/// <inheritdoc cref="DependencyPropertyChangedCallback"/>
private static void OnPixelShaderEffectPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
@@ -114,24 +132,6 @@ public sealed class D2D1AnimatedPixelShaderPanel : Control
}
}
/// <summary>
/// Gets or sets whether or not the rendering is paused.
/// </summary>
public bool IsPaused
{
get => (bool)GetValue(IsPausedProperty);
set => SetValue(IsPausedProperty, value);
}
/// <summary>
/// The <see cref="DependencyProperty"/> backing <see cref="IsPaused"/>.
/// </summary>
public static readonly DependencyProperty IsPausedProperty = DependencyProperty.Register(
nameof(IsPaused),
typeof(bool),
typeof(D2D1AnimatedPixelShaderPanel),
new PropertyMetadata(false, OnIsPausedPropertyChanged));
/// <inheritdoc cref="DependencyPropertyChangedCallback"/>
private static void OnIsPausedPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
@@ -1,135 +0,0 @@
using System;
using System.Diagnostics;
using System.Threading;
using Microsoft.UI.Xaml.Controls;
using Windows.Foundation;
namespace Microsoft.Graphics.Canvas.UI.Xaml;
/// <summary>
/// XAML control intended for displaying animating content.
/// </summary>
public sealed class CanvasAnimatedControl : Grid, ICanvasAnimatedControl, IDisposable
{
/// <summary>
/// The <see cref="CanvasControl"/> instance to use.
/// </summary>
private readonly CanvasControl canvasControl;
/// <summary>
/// The <see cref="Stopwatch"/> used to track the elapsed time.
/// </summary>
private readonly Stopwatch stopwatch;
/// <summary>
/// The <see cref="Timer"/> used to render frames.
/// </summary>
private readonly Timer timer;
/// <summary>
/// Indicates whether rendering is paused.
/// </summary>
private bool paused;
/// <summary>
/// Indicates whether a new frame is currently being drawn.
/// </summary>
private volatile int isDrawing;
/// <summary>
/// Hook this event to draw the contents of the control.
/// </summary>
public TypedEventHandler<ICanvasAnimatedControl, CanvasAnimatedDrawEventArgs>? Draw;
/// <summary>
/// Creates a new <see cref="CanvasAnimatedControl"/> instance.
/// </summary>
public CanvasAnimatedControl()
{
this.canvasControl = new CanvasControl();
this.stopwatch = new Stopwatch();
this.timer = new Timer(
callback: static canvasControl => ((CanvasControl)canvasControl!).Invalidate(),
state: this.canvasControl,
dueTime: TimeSpan.Zero,
period: TimeSpan.FromSeconds(1 / 60.0));
// Register the draw callback for the canvas control
this.canvasControl.Draw += CanvasControl_Draw;
// Start the render stopwatch (the render timer has already started)
this.stopwatch.Start();
// Add the canvas control to the visual tree
Children.Add(this.canvasControl);
}
/// <summary>
/// Gets or sets whether rendering has paused
/// </summary>
public bool Paused
{
get => this.paused;
set
{
if (this.paused != value)
{
this.paused = value;
// Start or stop the timers as needed
if (value)
{
_ = this.timer.Change(Timeout.Infinite, Timeout.Infinite);
this.stopwatch.Stop();
}
else
{
_ = this.timer.Change(TimeSpan.Zero, TimeSpan.FromSeconds(1 / 60.0f));
this.stopwatch.Start();
}
}
}
}
/// <inheritdoc/>
public void Dispose()
{
this.timer.Dispose();
}
// Forward the Draw event as a CanvasAnimatedControl sender
private void CanvasControl_Draw(CanvasControl sender, CanvasDrawEventArgs args)
{
// If another frame is already being concurrently drawn, do nothing
if (Interlocked.CompareExchange(ref this.isDrawing, 1, 0) == 1)
{
return;
}
try
{
this.Draw?.Invoke(
sender: this,
args: new CanvasAnimatedDrawEventArgs
{
DrawingSession = args.DrawingSession,
Timing = new CanvasTimingInformation { TotalTime = this.stopwatch.Elapsed }
});
}
finally
{
this.isDrawing = 0;
}
}
/// <inheritdoc/>
Size ICanvasAnimatedControl.Size => this.canvasControl.Size;
/// <inheritdoc/>
int ICanvasAnimatedControl.ConvertDipsToPixels(float dips, CanvasDpiRounding dpiRounding)
{
return this.canvasControl.ConvertDipsToPixels(dips, dpiRounding);
}
}
@@ -1,15 +0,0 @@
namespace Microsoft.Graphics.Canvas.UI.Xaml;
/// <summary>
/// Provides data for the <see cref="CanvasAnimatedControl.Draw"/> event.
/// </summary>
public sealed class CanvasAnimatedDrawEventArgs
{
/// <inheritdoc cref="CanvasDrawEventArgs.DrawingSession"/>
public required CanvasDrawingSession DrawingSession { get; init; }
/// <summary>
/// Gets the control's timing information for use by the event handler.
/// </summary>
public required CanvasTimingInformation Timing { get; init; }
}
@@ -1,15 +0,0 @@
using Windows.Foundation;
namespace Microsoft.Graphics.Canvas.UI.Xaml;
/// <summary>
/// XAML control intended for displaying animating content.
/// </summary>
public interface ICanvasAnimatedControl
{
/// <inheritdoc cref="CanvasControl.Size"/>
Size Size { get; }
/// <inheritdoc cref="CanvasControl.ConvertDipsToPixels"/>
int ConvertDipsToPixels(float dips, CanvasDpiRounding dpiRounding);
}
@@ -1,15 +0,0 @@
using System;
using Microsoft.Graphics.Canvas.UI.Xaml;
namespace Microsoft.Graphics.Canvas.UI;
/// <summary>
/// Contains information about a <see cref="CanvasAnimatedControl"/>'s timer.
/// </summary>
public readonly struct CanvasTimingInformation
{
/// <summary>
/// Represents the elapsed time, in ticks, for which this control has ever been running.
/// </summary>
public required TimeSpan TotalTime { get; init; }
}
+1 -1
ファイルの表示
@@ -31,7 +31,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Graphics.Win2D" Version="1.2.1-experimental1" />
<PackageReference Include="Microsoft.Graphics.Win2D" Version="1.2.1-experimental2" />
<!--
We need the CsWinRT reference for the generator to kick in and generate all necessary supporting code