using System;
using System.Diagnostics.CodeAnalysis;
using Microsoft.Graphics.Canvas;
using Windows.Graphics.Display;
using Windows.Graphics.Imaging;
using Windows.UI.Core;
namespace ComputeSharp.SwapChain.D2D1.Backend;
///
/// A simple application handling a D2D swapchain.
///
internal sealed class CoreWindowApplication
{
///
/// The instance to use.
///
private CanvasDevice? canvasDevice;
///
/// The instance to use to render frames.
///
private CanvasSwapChain? canvasSwapChain;
///
/// Whether or not the window has been resized and requires the buffers to be updated.
///
private volatile bool isResizePending;
///
/// The current screen width in raw pixels.
///
private uint screenWidth;
///
/// The current screen height in raw pixels.
///
private uint screenHeight;
///
/// Raised whenever a draw operation can be performed.
///
public event EventHandler? Draw;
///
/// Initializes the current application.
///
/// The instance.
[MemberNotNull(nameof(canvasDevice))]
[MemberNotNull(nameof(canvasSwapChain))]
public unsafe void OnInitialize(CoreWindow window)
{
// Create a new canvas device, which will handle DX11/D2D initialization
CanvasDevice canvasDevice = new();
// Create the swapchain for rendering (it is automatically tied to the window)
CanvasSwapChain canvasSwapChain = CanvasSwapChain.CreateForCoreWindow(
resourceCreator: canvasDevice,
coreWindow: window,
dpi: DisplayInformation.GetForCurrentView().LogicalDpi);
// Save the Win2D objects for later use
this.canvasDevice = canvasDevice;
this.canvasSwapChain = canvasSwapChain;
// Store the initial size of the screen
this.screenWidth = canvasSwapChain.SizeInPixels.Width;
this.screenHeight = canvasSwapChain.SizeInPixels.Height;
}
///
/// Resizes the current application.
///
public void OnResize()
{
this.isResizePending = true;
}
///
/// Updates the current application.
///
/// The current time since the start of the application.
public void OnUpdate(TimeSpan time)
{
if (this.isResizePending)
{
// Resize the swapchain if needed (the size is calculated automatically)
this.canvasSwapChain!.ResizeBuffers(0, 0);
BitmapSize bitmapSize = this.canvasSwapChain!.SizeInPixels;
this.screenWidth = bitmapSize.Width;
this.screenHeight = bitmapSize.Height;
this.isResizePending = false;
}
// Create the drawing session and invoke all registered draw handler
using (CanvasDrawingSession canvasDrawingSession = this.canvasSwapChain!.CreateDrawingSession(default))
{
Draw?.Invoke(this, new DrawEventArgs
{
ScreenWidth = this.screenWidth,
ScreenHeight = this.screenHeight,
TotalTime = time,
DrawingSession = canvasDrawingSession
});
}
// Wait for v-sync
this.canvasSwapChain.WaitForVerticalBlank();
// Present the new frame
this.canvasSwapChain.Present(syncInterval: 1);
}
///
/// Arguments for .
///
public sealed class DrawEventArgs : EventArgs
{
///
/// Gets the screen width in raw pixels.
///
public required uint ScreenWidth { get; init; }
///
/// Gets the screen height in raw pixels.
///
public required uint ScreenHeight { get; init; }
///
/// Gets the total time for the rendering loop.
///
public required TimeSpan TotalTime { get; init; }
///
/// Gets the instance to use.
///
public required CanvasDrawingSession DrawingSession { get; init; }
}
}