diff --git a/samples/ComputeSharp.SwapChain.WinUI/Controls/D2D1AnimatedPixelShaderPanel.cs b/samples/ComputeSharp.SwapChain.WinUI/Controls/D2D1AnimatedPixelShaderPanel.cs index c1af1335..bf90dc09 100644 --- a/samples/ComputeSharp.SwapChain.WinUI/Controls/D2D1AnimatedPixelShaderPanel.cs +++ b/samples/ComputeSharp.SwapChain.WinUI/Controls/D2D1AnimatedPixelShaderPanel.cs @@ -12,6 +12,24 @@ namespace ComputeSharp.SwapChain.WinUI.Views; [TemplatePart(Name = "PART_CanvasAnimatedControl", Type = typeof(CanvasAnimatedControl))] public sealed class D2D1AnimatedPixelShaderPanel : Control { + /// + /// The backing . + /// + public static readonly DependencyProperty PixelShaderEffectProperty = DependencyProperty.Register( + nameof(PixelShaderEffect), + typeof(PixelShaderEffect), + typeof(D2D1AnimatedPixelShaderPanel), + new PropertyMetadata(null, OnPixelShaderEffectPropertyChanged)); + + /// + /// The backing . + /// + public static readonly DependencyProperty IsPausedProperty = DependencyProperty.Register( + nameof(IsPaused), + typeof(bool), + typeof(D2D1AnimatedPixelShaderPanel), + new PropertyMetadata(false, OnIsPausedPropertyChanged)); + /// /// The wrapped instance used to render frames. /// @@ -33,6 +51,24 @@ public sealed class D2D1AnimatedPixelShaderPanel : Control Unloaded += D2D1AnimatedPixelShaderPanel_Unloaded; } + /// + /// Gets or sets the instance to use to render content. + /// + public PixelShaderEffect? PixelShaderEffect + { + get => (PixelShaderEffect?)GetValue(PixelShaderEffectProperty); + set => SetValue(PixelShaderEffectProperty, value); + } + + /// + /// Gets or sets whether or not the rendering is paused. + /// + public bool IsPaused + { + get => (bool)GetValue(IsPausedProperty); + set => SetValue(IsPausedProperty, value); + } + /// protected override void OnApplyTemplate() { @@ -78,24 +114,6 @@ public sealed class D2D1AnimatedPixelShaderPanel : Control args.DrawingSession.DrawImage(this.pixelShaderEffect); } - /// - /// Gets or sets the instance to use to render content. - /// - public PixelShaderEffect? PixelShaderEffect - { - get => (PixelShaderEffect?)GetValue(PixelShaderEffectProperty); - set => SetValue(PixelShaderEffectProperty, value); - } - - /// - /// The backing . - /// - public static readonly DependencyProperty PixelShaderEffectProperty = DependencyProperty.Register( - nameof(PixelShaderEffect), - typeof(PixelShaderEffect), - typeof(D2D1AnimatedPixelShaderPanel), - new PropertyMetadata(null, OnPixelShaderEffectPropertyChanged)); - /// private static void OnPixelShaderEffectPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { @@ -114,24 +132,6 @@ public sealed class D2D1AnimatedPixelShaderPanel : Control } } - /// - /// Gets or sets whether or not the rendering is paused. - /// - public bool IsPaused - { - get => (bool)GetValue(IsPausedProperty); - set => SetValue(IsPausedProperty, value); - } - - /// - /// The backing . - /// - public static readonly DependencyProperty IsPausedProperty = DependencyProperty.Register( - nameof(IsPaused), - typeof(bool), - typeof(D2D1AnimatedPixelShaderPanel), - new PropertyMetadata(false, OnIsPausedPropertyChanged)); - /// private static void OnIsPausedPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { 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 deleted file mode 100644 index bab1abb3..00000000 --- a/samples/ComputeSharp.SwapChain.WinUI/Controls/Microsoft.Graphics.Canvas.UI.Xaml/CanvasAnimatedControl.cs +++ /dev/null @@ -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; - -/// -/// 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 paused; - - /// - /// 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 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(); - } - } - } - } - - /// - 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); - } -} \ No newline at end of file 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 deleted file mode 100644 index 7c8898cc..00000000 --- a/samples/ComputeSharp.SwapChain.WinUI/Controls/Microsoft.Graphics.Canvas.UI.Xaml/CanvasAnimatedDrawEventArgs.cs +++ /dev/null @@ -1,15 +0,0 @@ -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; } -} \ No newline at end of file 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 deleted file mode 100644 index 27092438..00000000 --- a/samples/ComputeSharp.SwapChain.WinUI/Controls/Microsoft.Graphics.Canvas.UI.Xaml/ICanvasAnimatedControl.cs +++ /dev/null @@ -1,15 +0,0 @@ -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); -} \ No newline at end of file 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 deleted file mode 100644 index 43b1b363..00000000 --- a/samples/ComputeSharp.SwapChain.WinUI/Controls/Microsoft.Graphics.Canvas.UI/CanvasTimingInformation.cs +++ /dev/null @@ -1,15 +0,0 @@ -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; } -} \ No newline at end of file diff --git a/src/ComputeSharp.D2D1.WinUI/ComputeSharp.D2D1.WinUI.csproj b/src/ComputeSharp.D2D1.WinUI/ComputeSharp.D2D1.WinUI.csproj index bf13a149..37d0259d 100644 --- a/src/ComputeSharp.D2D1.WinUI/ComputeSharp.D2D1.WinUI.csproj +++ b/src/ComputeSharp.D2D1.WinUI/ComputeSharp.D2D1.WinUI.csproj @@ -31,7 +31,7 @@ - +