using System; using System.Diagnostics.CodeAnalysis; using Microsoft.UI.Windowing; namespace ComputeSharp.SwapChain.D2D1.Backend; using BitmapSize = Windows.Graphics.Imaging.BitmapSize; using CanvasDevice = Microsoft.Graphics.Canvas.CanvasDevice; using CanvasDrawingSession = Microsoft.Graphics.Canvas.CanvasDrawingSession; using CanvasSwapChain = Microsoft.Graphics.Canvas.CanvasSwapChain; using DirectXPixelFormat = Windows.Graphics.DirectX.DirectXPixelFormat; /// /// A simple Win32 application handling a D2D swapchain. /// internal sealed class Win32Application { /// /// 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 window to show. [MemberNotNull(nameof(canvasDevice))] [MemberNotNull(nameof(canvasSwapChain))] public unsafe void OnInitialize(AppWindow appWindow) { // Create a new canvas device, which will handle DX11/D2D initialization CanvasDevice canvasDevice = new(); CanvasSwapChain canvasSwapChain = CanvasSwapChain.CreateForWindowId( resourceCreator: canvasDevice, windowId: appWindow.Id, width: this.screenWidth = 1, height: this.screenHeight = 1, dpi: 96.0f); // Save the Win2D objects for later use this.canvasDevice = canvasDevice; this.canvasSwapChain = canvasSwapChain; // Request a resize before the first draw operation this.isResizePending = true; } /// /// 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( newWidth: 0, newHeight: 0, newDpi: 96.0f, newFormat: DirectXPixelFormat.Unknown, bufferCount: 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; } } }