1
0
mirror of https://github.com/Artemis-RGB/Artemis synced 2025-12-13 05:48:35 +00:00

Core - Kill zombies after 2 seconds 🔫

Core - Fix an access violation on surface changes (when loading device providers/moving devices)
This commit is contained in:
Robert 2021-08-23 20:41:07 +02:00
parent c08e4a49a3
commit ca453f2052
6 changed files with 127 additions and 95 deletions

View File

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using Artemis.Core.Ninject;
using Artemis.Core.ScriptingProviders;
@ -58,7 +59,6 @@ namespace Artemis.Core.Services
_frameStopWatch = new Stopwatch();
StartupArguments = new List<string>();
_rgbService.IsRenderPaused = true;
_rgbService.Surface.Updating += SurfaceOnUpdating;
_loggingLevel.SettingChanged += (sender, args) => ApplyLoggingLevel();
}
@ -222,7 +222,7 @@ namespace Artemis.Core.Services
_pluginManagementService.CopyBuiltInPlugins();
_pluginManagementService.LoadPlugins(StartupArguments, IsElevated);
_rgbService.IsRenderPaused = false;
_rgbService.SetRenderPaused(false);
OnInitialized();
}

View File

@ -34,13 +34,13 @@ namespace Artemis.Core.Services
/// <summary>
/// Gets or sets whether rendering should be paused
/// </summary>
bool IsRenderPaused { get; set; }
bool IsRenderPaused { get; }
/// <summary>
/// Gets a boolean indicating whether the render pipeline is open
/// </summary>
bool RenderOpen { get; }
/// <summary>
/// Gets or sets a boolean indicating whether to flush the RGB.NET LEDs during next update
/// </summary>
@ -138,6 +138,13 @@ namespace Artemis.Core.Services
/// <param name="device">The device to disable</param>
void DisableDevice(ArtemisDevice device);
/// <summary>
/// Pauses or resumes rendering, method won't return until the current frame finished rendering
/// </summary>
/// <param name="paused"></param>
/// <returns><see langword="true"/> if the pause state was changed; otherwise <see langword="false"/>.</returns>
bool SetRenderPaused(bool paused);
/// <summary>
/// Occurs when a single device was added
/// </summary>

View File

@ -27,7 +27,6 @@ namespace Artemis.Core.Services
private readonly PluginSetting<int> _targetFrameRateSetting;
private readonly TextureBrush _textureBrush = new(ITexture.Empty) {CalculationMode = RenderMode.Absolute};
private Dictionary<Led, ArtemisLed> _ledMap;
private bool _modifyingProviders;
private ListLedGroup? _surfaceLedGroup;
private SKTexture? _texture;
@ -51,6 +50,7 @@ namespace Artemis.Core.Services
_ledMap = new Dictionary<Led, ArtemisLed>();
UpdateTrigger = new TimerUpdateTrigger {UpdateFrequency = 1.0 / _targetFrameRateSetting.Value};
SetRenderPaused(true);
Surface.RegisterUpdateTrigger(UpdateTrigger);
Utilities.ShutdownRequested += UtilitiesOnShutdownRequested;
@ -81,16 +81,14 @@ namespace Artemis.Core.Services
private void UpdateLedGroup()
{
lock (_devices)
bool changedRenderPaused = SetRenderPaused(true);
try
{
if (_modifyingProviders)
return;
_ledMap = new Dictionary<Led, ArtemisLed>(_devices.SelectMany(d => d.Leds).ToDictionary(l => l.RgbLed));
if (_surfaceLedGroup == null)
{
_surfaceLedGroup = new ListLedGroup(Surface, LedMap.Select(l => l.Key)) {Brush = _textureBrush};
_surfaceLedGroup = new ListLedGroup(Surface, LedMap.Select(l => l.Key)) { Brush = _textureBrush };
OnLedsChanged();
return;
}
@ -101,10 +99,16 @@ namespace Artemis.Core.Services
_surfaceLedGroup.Detach();
// Apply the application wide brush and decorator
_surfaceLedGroup = new ListLedGroup(Surface, LedMap.Select(l => l.Key)) {Brush = _textureBrush};
_surfaceLedGroup = new ListLedGroup(Surface, LedMap.Select(l => l.Key)) { Brush = _textureBrush };
OnLedsChanged();
}
}
finally
{
if (changedRenderPaused)
SetRenderPaused(false);
}
}
private void TargetFrameRateSettingOnSettingChanged(object? sender, EventArgs e)
@ -141,95 +145,85 @@ namespace Artemis.Core.Services
public void AddDeviceProvider(IRGBDeviceProvider deviceProvider)
{
if (RenderOpen)
throw new ArtemisCoreException("Cannot add a device provider while rendering");
bool changedRenderPaused = SetRenderPaused(true);
lock (_devices)
try
{
try
List<ArtemisDevice> toRemove = _devices.Where(a => deviceProvider.Devices.Any(d => a.RgbDevice == d)).ToList();
Surface.Detach(toRemove.Select(d => d.RgbDevice));
foreach (ArtemisDevice device in toRemove)
RemoveDevice(device);
List<Exception> providerExceptions = new();
void DeviceProviderOnException(object? sender, ExceptionEventArgs e)
{
_modifyingProviders = true;
List<ArtemisDevice> toRemove = _devices.Where(a => deviceProvider.Devices.Any(d => a.RgbDevice == d)).ToList();
Surface.Detach(toRemove.Select(d => d.RgbDevice));
foreach (ArtemisDevice device in toRemove)
RemoveDevice(device);
List<Exception> providerExceptions = new();
void DeviceProviderOnException(object? sender, ExceptionEventArgs e)
{
if (e.IsCritical)
providerExceptions.Add(e.Exception);
else
_logger.Warning(e.Exception, "Device provider {deviceProvider} threw non-critical exception", deviceProvider.GetType().Name);
}
deviceProvider.Exception += DeviceProviderOnException;
deviceProvider.Initialize();
Surface.Attach(deviceProvider.Devices);
deviceProvider.Exception -= DeviceProviderOnException;
if (providerExceptions.Count == 1)
throw new ArtemisPluginException("RGB.NET threw exception: " + providerExceptions.First().Message, providerExceptions.First());
if (providerExceptions.Count > 1)
throw new ArtemisPluginException("RGB.NET threw multiple exceptions", new AggregateException(providerExceptions));
if (!deviceProvider.Devices.Any())
{
_logger.Warning("Device provider {deviceProvider} has no devices", deviceProvider.GetType().Name);
return;
}
foreach (IRGBDevice rgbDevice in deviceProvider.Devices)
{
ArtemisDevice artemisDevice = GetArtemisDevice(rgbDevice);
AddDevice(artemisDevice);
_logger.Debug("Device provider {deviceProvider} added {deviceName}", deviceProvider.GetType().Name, rgbDevice.DeviceInfo.DeviceName);
}
_devices.Sort((a, b) => a.ZIndex - b.ZIndex);
if (e.IsCritical)
providerExceptions.Add(e.Exception);
else
_logger.Warning(e.Exception, "Device provider {deviceProvider} threw non-critical exception", deviceProvider.GetType().Name);
}
catch (Exception e)
deviceProvider.Exception += DeviceProviderOnException;
deviceProvider.Initialize();
Surface.Attach(deviceProvider.Devices);
deviceProvider.Exception -= DeviceProviderOnException;
if (providerExceptions.Count == 1)
throw new ArtemisPluginException("RGB.NET threw exception: " + providerExceptions.First().Message, providerExceptions.First());
if (providerExceptions.Count > 1)
throw new ArtemisPluginException("RGB.NET threw multiple exceptions", new AggregateException(providerExceptions));
if (!deviceProvider.Devices.Any())
{
_logger.Error(e, "Exception during device loading for device provider {deviceProvider}", deviceProvider.GetType().Name);
throw;
_logger.Warning("Device provider {deviceProvider} has no devices", deviceProvider.GetType().Name);
return;
}
finally
foreach (IRGBDevice rgbDevice in deviceProvider.Devices)
{
_modifyingProviders = false;
UpdateLedGroup();
ArtemisDevice artemisDevice = GetArtemisDevice(rgbDevice);
AddDevice(artemisDevice);
_logger.Debug("Device provider {deviceProvider} added {deviceName}", deviceProvider.GetType().Name, rgbDevice.DeviceInfo.DeviceName);
}
_devices.Sort((a, b) => a.ZIndex - b.ZIndex);
}
catch (Exception e)
{
_logger.Error(e, "Exception during device loading for device provider {deviceProvider}", deviceProvider.GetType().Name);
throw;
}
finally
{
UpdateLedGroup();
if (changedRenderPaused)
SetRenderPaused(false);
}
}
public void RemoveDeviceProvider(IRGBDeviceProvider deviceProvider)
{
if (RenderOpen)
throw new ArtemisCoreException("Cannot update the remove device provider while rendering");
bool changedRenderPaused = SetRenderPaused(true);
lock (_devices)
try
{
try
{
_modifyingProviders = true;
List<ArtemisDevice> toRemove = _devices.Where(a => deviceProvider.Devices.Any(d => a.RgbDevice == d)).ToList();
Surface.Detach(toRemove.Select(d => d.RgbDevice));
foreach (ArtemisDevice device in toRemove)
RemoveDevice(device);
List<ArtemisDevice> toRemove = _devices.Where(a => deviceProvider.Devices.Any(d => a.RgbDevice == d)).ToList();
Surface.Detach(toRemove.Select(d => d.RgbDevice));
foreach (ArtemisDevice device in toRemove)
RemoveDevice(device);
_devices.Sort((a, b) => a.ZIndex - b.ZIndex);
}
catch (Exception e)
{
_logger.Error(e, "Exception during device removal for device provider {deviceProvider}", deviceProvider.GetType().Name);
throw;
}
finally
{
_modifyingProviders = false;
UpdateLedGroup();
}
_devices.Sort((a, b) => a.ZIndex - b.ZIndex);
}
catch (Exception e)
{
_logger.Error(e, "Exception during device removal for device provider {deviceProvider}", deviceProvider.GetType().Name);
throw;
}
finally
{
UpdateLedGroup();
if (changedRenderPaused)
SetRenderPaused(false);
}
}
@ -241,6 +235,24 @@ namespace Artemis.Core.Services
Surface.Dispose();
}
public bool SetRenderPaused(bool paused)
{
if (IsRenderPaused == paused)
return false;
if (paused)
{
UpdateTrigger.Stop();
}
else
{
UpdateTrigger.Start();
}
IsRenderPaused = paused;
return true;
}
public event EventHandler<DeviceEventArgs>? DeviceAdded;
public event EventHandler<DeviceEventArgs>? DeviceRemoved;
public event EventHandler? LedsChanged;
@ -326,12 +338,22 @@ namespace Artemis.Core.Services
public void AutoArrangeDevices()
{
SurfaceArrangement surfaceArrangement = SurfaceArrangement.GetDefaultArrangement();
surfaceArrangement.Arrange(_devices);
foreach (ArtemisDevice artemisDevice in _devices)
artemisDevice.ApplyDefaultCategories();
bool changedRenderPaused = SetRenderPaused(true);
SaveDevices();
try
{
SurfaceArrangement surfaceArrangement = SurfaceArrangement.GetDefaultArrangement();
surfaceArrangement.Arrange(_devices);
foreach (ArtemisDevice artemisDevice in _devices)
artemisDevice.ApplyDefaultCategories();
SaveDevices();
}
finally
{
if (changedRenderPaused)
SetRenderPaused(false);
}
}
public ArtemisLayout? ApplyBestDeviceLayout(ArtemisDevice device)
@ -402,7 +424,7 @@ namespace Artemis.Core.Services
_deviceRepository.Save(device.DeviceEntity);
DeviceProvider deviceProvider = device.DeviceProvider;
// Feels bad but need to in order to get the initial LEDs back
_pluginManagementService.DisablePluginFeature(deviceProvider, false);
Thread.Sleep(500);

View File

@ -92,7 +92,10 @@ namespace Artemis.UI
using HttpClient client = new();
try
{
HttpResponseMessage httpResponseMessage = client.Send(new HttpRequestMessage(HttpMethod.Post, url + "remote/bring-to-foreground"));
CancellationTokenSource cts = new();
cts.CancelAfter(2000);
HttpResponseMessage httpResponseMessage = client.Send(new HttpRequestMessage(HttpMethod.Post, url + "remote/bring-to-foreground"), cts.Token);
httpResponseMessage.EnsureSuccessStatusCode();
return true;
}

View File

@ -31,14 +31,14 @@ namespace Artemis.UI.Screens.StartupWizard.Steps
/// <inheritdoc />
protected override void OnActivate()
{
_rgbService.IsRenderPaused = true;
_rgbService.SetRenderPaused(true);
base.OnActivate();
}
/// <inheritdoc />
protected override void OnDeactivate()
{
_rgbService.IsRenderPaused = false;
_rgbService.SetRenderPaused(false);
base.OnDeactivate();
}

View File

@ -333,7 +333,7 @@ namespace Artemis.UI.Screens.SurfaceEditor
SurfaceDeviceViewModel device = HitTestUtilities.GetHitViewModels<SurfaceDeviceViewModel>((Visual) sender, selectedRect).FirstOrDefault();
if (device != null)
{
_rgbService.IsRenderPaused = true;
_rgbService.SetRenderPaused(true);
_mouseDragStatus = MouseDragStatus.Dragging;
// If the device is not selected, deselect others and select only this one (if shift not held)
if (device.SelectionStatus != SelectionStatus.Selected)
@ -378,7 +378,7 @@ namespace Artemis.UI.Screens.SurfaceEditor
}
_mouseDragStatus = MouseDragStatus.None;
_rgbService.IsRenderPaused = false;
_rgbService.SetRenderPaused(false);
ApplySurfaceSelection();
}