using ComputeSharp.SwapChain.Core.Shaders;
using Microsoft.Graphics.Canvas;
using Microsoft.Graphics.Canvas.UI.Xaml;
#if WINDOWS_UWP
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
#else
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
#endif
#nullable enable
#if WINDOWS_UWP
namespace ComputeSharp.SwapChain.Uwp.Views;
#else
namespace ComputeSharp.SwapChain.WinUI.Views;
#endif
///
/// A custom that can be used to render animated backgrounds via Win2D.
///
[TemplatePart(Name = "PART_CanvasAnimatedControl", Type = typeof(CanvasAnimatedControl))]
public sealed class D2D1AnimatedPixelShaderPanel : Control
{
///
/// The wrapped instance used to render frames.
///
private CanvasAnimatedControl? canvasAnimatedControl;
///
/// The instance currently in use, if any.
///
private volatile PixelShaderEffect? pixelShaderEffect;
///
/// Creates a new instance.
///
public D2D1AnimatedPixelShaderPanel()
{
DefaultStyleKey = typeof(D2D1AnimatedPixelShaderPanel);
Loaded += D2D1AnimatedPixelShaderPanel_Loaded;
Unloaded += D2D1AnimatedPixelShaderPanel_Unloaded;
}
///
protected override void OnApplyTemplate()
{
base.OnApplyTemplate();
this.canvasAnimatedControl = (CanvasAnimatedControl)GetTemplateChild("PART_CanvasAnimatedControl")!;
this.canvasAnimatedControl.Draw += CanvasAnimatedControl_Draw;
}
// Ensure the panel is not paused when the control is loaded
private void D2D1AnimatedPixelShaderPanel_Loaded(object sender, RoutedEventArgs e)
{
if (this.canvasAnimatedControl is { } canvasAnimatedControl &&
PixelShaderEffect is not null &&
!IsPaused)
{
canvasAnimatedControl.Paused = false;
}
}
// Always pause rendering when the control is unloaded
private void D2D1AnimatedPixelShaderPanel_Unloaded(object sender, RoutedEventArgs e)
{
if (this.canvasAnimatedControl is { } canvasAnimatedControl)
{
canvasAnimatedControl.Paused = true;
}
}
///
/// Draws a new frame on the wrapped instance.
///
/// The source instance.
/// The object to perform drawing with.
private void CanvasAnimatedControl_Draw(ICanvasAnimatedControl sender, CanvasAnimatedDrawEventArgs args)
{
// Set the effect properties
this.pixelShaderEffect!.ElapsedTime = args.Timing.TotalTime;
this.pixelShaderEffect.ScreenWidth = sender.ConvertDipsToPixels((float)sender.Size.Width, CanvasDpiRounding.Round);
this.pixelShaderEffect.ScreenHeight = sender.ConvertDipsToPixels((float)sender.Size.Height, CanvasDpiRounding.Round);
// Draw the effect
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)
{
D2D1AnimatedPixelShaderPanel @this = (D2D1AnimatedPixelShaderPanel)d;
PixelShaderEffect? pixelShaderEffect = (PixelShaderEffect?)e.NewValue;
// Save the new pixel shader effect for later (as the dependency property cannot be accessed by the render thread)
@this.pixelShaderEffect = pixelShaderEffect;
// Pause or start the render thread if an effect is available
if (@this.canvasAnimatedControl is { } canvasAnimatedControl)
{
bool shouldRender = @this.IsLoaded && !@this.IsPaused && pixelShaderEffect is not null;
canvasAnimatedControl.Paused = !shouldRender;
}
}
///
/// 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)
{
D2D1AnimatedPixelShaderPanel @this = (D2D1AnimatedPixelShaderPanel)d;
bool isPaused = (bool)e.NewValue;
if (@this.canvasAnimatedControl is { } canvasAnimatedControl)
{
bool shouldRender = @this.IsLoaded && !isPaused && @this.PixelShaderEffect is not null;
canvasAnimatedControl.Paused = !shouldRender;
}
}
}