From 480004be21ab7a7da5f7220a317c08a5592bc860 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 31 Jul 2022 18:07:17 +0200 Subject: [PATCH] Add DeviceLostEventArgs type --- .../Graphics/DeviceLostEventArgs.cs | 23 +++++++++++++++++++ .../Graphics/GraphicsDevice.DeviceLost.cs | 2 +- src/ComputeSharp/Graphics/GraphicsDevice.cs | 2 +- .../DeviceLostTests.cs | 6 ++--- 4 files changed, 28 insertions(+), 5 deletions(-) create mode 100644 src/ComputeSharp/Graphics/DeviceLostEventArgs.cs diff --git a/src/ComputeSharp/Graphics/DeviceLostEventArgs.cs b/src/ComputeSharp/Graphics/DeviceLostEventArgs.cs new file mode 100644 index 00000000..f817501f --- /dev/null +++ b/src/ComputeSharp/Graphics/DeviceLostEventArgs.cs @@ -0,0 +1,23 @@ +using System; + +namespace ComputeSharp; + +/// +/// The arguments for the event. +/// +public sealed class DeviceLostEventArgs : EventArgs +{ + /// + /// Creates a new instance with the specified arguments. + /// + /// The value for the event. + internal DeviceLostEventArgs(DeviceLostReason reason) + { + Reason = reason; + } + + /// + /// Gets the reason that caused the device to be lost. + /// + public DeviceLostReason Reason { get; } +} diff --git a/src/ComputeSharp/Graphics/GraphicsDevice.DeviceLost.cs b/src/ComputeSharp/Graphics/GraphicsDevice.DeviceLost.cs index 2950963f..1bd8209a 100644 --- a/src/ComputeSharp/Graphics/GraphicsDevice.DeviceLost.cs +++ b/src/ComputeSharp/Graphics/GraphicsDevice.DeviceLost.cs @@ -96,7 +96,7 @@ unsafe partial class GraphicsDevice _ => DeviceLostReason.UnspecifiedError }; - device.DeviceLost?.Invoke(device, reason); + device.DeviceLost?.Invoke(device, new DeviceLostEventArgs(reason)); } ThreadPool.QueueUserWorkItem(static state => RaiseDeviceLostEvent((GraphicsDevice)state!), this); diff --git a/src/ComputeSharp/Graphics/GraphicsDevice.cs b/src/ComputeSharp/Graphics/GraphicsDevice.cs index 2847205e..53f6af10 100644 --- a/src/ComputeSharp/Graphics/GraphicsDevice.cs +++ b/src/ComputeSharp/Graphics/GraphicsDevice.cs @@ -137,7 +137,7 @@ public sealed unsafe partial class GraphicsDevice : NativeObject /// the event is raised asynchronously with respect to the device being lost, and on a thread pool thread. /// /// - public event EventHandler? DeviceLost; + public event EventHandler? DeviceLost; /// /// Creates a new instance for the input . diff --git a/tests/ComputeSharp.Tests.DeviceLost/DeviceLostTests.cs b/tests/ComputeSharp.Tests.DeviceLost/DeviceLostTests.cs index 17139d71..58895f94 100644 --- a/tests/ComputeSharp.Tests.DeviceLost/DeviceLostTests.cs +++ b/tests/ComputeSharp.Tests.DeviceLost/DeviceLostTests.cs @@ -18,7 +18,7 @@ public class DeviceLostTests { using GraphicsDevice graphicsDevice = device.Get(); - List<(object? Sender, DeviceLostReason Reason)> args = new(); + List<(object? Sender, DeviceLostEventArgs Args)> args = new(); // Register the device lost callback graphicsDevice.DeviceLost += (s, e) => args.Add((s, e)); @@ -27,9 +27,9 @@ public class DeviceLostTests Assert.AreEqual(1, args.Count); Assert.IsNotNull(args[0].Sender); - Assert.IsNotNull(args[0].Reason); + Assert.IsNotNull(args[0].Args); Assert.AreSame(args[0].Sender, graphicsDevice); - Assert.AreEqual(args[0].Reason, DeviceLostReason.DeviceRemoved); + Assert.AreEqual(args[0].Args.Reason, DeviceLostReason.DeviceRemoved); // Trying to remove the device again does nothing GraphicsDeviceHelper.RemoveDevice(graphicsDevice);