From 63ce21aa0650e5d2901441213c83c3e2abe7d396 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 19 Feb 2023 15:57:14 +0100 Subject: [PATCH] Add CanvasAnimatedControl polyfill to WinUI sample Fix --- .../ComputeSharp.SwapChain.WinUI.csproj | 1 + .../CanvasAnimatedControl.cs | 135 ++++++++++++++++++ .../CanvasAnimatedDrawEventArgs.cs | 15 ++ .../ICanvasAnimatedControl.cs | 15 ++ .../CanvasTimingInformation.cs | 15 ++ 5 files changed, 181 insertions(+) create mode 100644 samples/ComputeSharp.SwapChain.WinUI/Controls/Microsoft.Graphics.Canvas.UI.Xaml/CanvasAnimatedControl.cs create mode 100644 samples/ComputeSharp.SwapChain.WinUI/Controls/Microsoft.Graphics.Canvas.UI.Xaml/CanvasAnimatedDrawEventArgs.cs create mode 100644 samples/ComputeSharp.SwapChain.WinUI/Controls/Microsoft.Graphics.Canvas.UI.Xaml/ICanvasAnimatedControl.cs create mode 100644 samples/ComputeSharp.SwapChain.WinUI/Controls/Microsoft.Graphics.Canvas.UI/CanvasTimingInformation.cs diff --git a/samples/ComputeSharp.SwapChain.WinUI/ComputeSharp.SwapChain.WinUI.csproj b/samples/ComputeSharp.SwapChain.WinUI/ComputeSharp.SwapChain.WinUI.csproj index 84899a9f..92d30ae7 100644 --- a/samples/ComputeSharp.SwapChain.WinUI/ComputeSharp.SwapChain.WinUI.csproj +++ b/samples/ComputeSharp.SwapChain.WinUI/ComputeSharp.SwapChain.WinUI.csproj @@ -33,6 +33,7 @@ + diff --git a/samples/ComputeSharp.SwapChain.WinUI/Controls/Microsoft.Graphics.Canvas.UI.Xaml/CanvasAnimatedControl.cs b/samples/ComputeSharp.SwapChain.WinUI/Controls/Microsoft.Graphics.Canvas.UI.Xaml/CanvasAnimatedControl.cs new file mode 100644 index 00000000..f7c98bc1 --- /dev/null +++ b/samples/ComputeSharp.SwapChain.WinUI/Controls/Microsoft.Graphics.Canvas.UI.Xaml/CanvasAnimatedControl.cs @@ -0,0 +1,135 @@ +using System; +using System.Diagnostics; +using System.Threading; +using Microsoft.UI.Xaml.Controls; +using Windows.Foundation; + +namespace Microsoft.Graphics.Canvas.UI.Xaml; + +/// +/// XAML control intended for displaying animating content. +/// +public sealed class CanvasAnimatedControl : Grid, ICanvasAnimatedControl, IDisposable +{ + /// + /// The instance to use. + /// + private readonly CanvasControl canvasControl; + + /// + /// The used to track the elapsed time. + /// + private readonly Stopwatch stopwatch; + + /// + /// The used to render frames. + /// + private readonly Timer timer; + + /// + /// Indicates whether rendering is paused. + /// + private bool isPaused; + + /// + /// Indicates whether a new frame is currently being drawn. + /// + private volatile int isDrawing; + + /// + /// Hook this event to draw the contents of the control. + /// + public TypedEventHandler? Draw; + + /// + /// Creates a new instance. + /// + 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); + } + + /// + /// Gets or sets whether rendering has paused + /// + public bool IsPaused + { + get => this.isPaused; + set + { + if (this.isPaused != value) + { + this.isPaused = 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(); + } + } + } + } + + /// + 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; + } + } + + /// + Size ICanvasAnimatedControl.Size => this.canvasControl.Size; + + /// + int ICanvasAnimatedControl.ConvertDipsToPixels(float dips, CanvasDpiRounding dpiRounding) + { + return this.canvasControl.ConvertDipsToPixels(dips, dpiRounding); + } +} diff --git a/samples/ComputeSharp.SwapChain.WinUI/Controls/Microsoft.Graphics.Canvas.UI.Xaml/CanvasAnimatedDrawEventArgs.cs b/samples/ComputeSharp.SwapChain.WinUI/Controls/Microsoft.Graphics.Canvas.UI.Xaml/CanvasAnimatedDrawEventArgs.cs new file mode 100644 index 00000000..10deefba --- /dev/null +++ b/samples/ComputeSharp.SwapChain.WinUI/Controls/Microsoft.Graphics.Canvas.UI.Xaml/CanvasAnimatedDrawEventArgs.cs @@ -0,0 +1,15 @@ +namespace Microsoft.Graphics.Canvas.UI.Xaml; + +/// +/// Provides data for the event. +/// +public sealed class CanvasAnimatedDrawEventArgs +{ + /// + public required CanvasDrawingSession DrawingSession { get; init; } + + /// + /// Gets the control's timing information for use by the event handler. + /// + public required CanvasTimingInformation Timing { get; init; } +} diff --git a/samples/ComputeSharp.SwapChain.WinUI/Controls/Microsoft.Graphics.Canvas.UI.Xaml/ICanvasAnimatedControl.cs b/samples/ComputeSharp.SwapChain.WinUI/Controls/Microsoft.Graphics.Canvas.UI.Xaml/ICanvasAnimatedControl.cs new file mode 100644 index 00000000..e7eb1646 --- /dev/null +++ b/samples/ComputeSharp.SwapChain.WinUI/Controls/Microsoft.Graphics.Canvas.UI.Xaml/ICanvasAnimatedControl.cs @@ -0,0 +1,15 @@ +using Windows.Foundation; + +namespace Microsoft.Graphics.Canvas.UI.Xaml; + +/// +/// XAML control intended for displaying animating content. +/// +public interface ICanvasAnimatedControl +{ + /// + Size Size { get; } + + /// + int ConvertDipsToPixels(float dips, CanvasDpiRounding dpiRounding); +} diff --git a/samples/ComputeSharp.SwapChain.WinUI/Controls/Microsoft.Graphics.Canvas.UI/CanvasTimingInformation.cs b/samples/ComputeSharp.SwapChain.WinUI/Controls/Microsoft.Graphics.Canvas.UI/CanvasTimingInformation.cs new file mode 100644 index 00000000..54e3a9c8 --- /dev/null +++ b/samples/ComputeSharp.SwapChain.WinUI/Controls/Microsoft.Graphics.Canvas.UI/CanvasTimingInformation.cs @@ -0,0 +1,15 @@ +using System; +using Microsoft.Graphics.Canvas.UI.Xaml; + +namespace Microsoft.Graphics.Canvas.UI; + +/// +/// Contains information about a 's timer. +/// +public readonly struct CanvasTimingInformation +{ + /// + /// Represents the elapsed time, in ticks, for which this control has ever been running. + /// + public required TimeSpan TotalTime { get; init; } +}