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

View File

@ -34,7 +34,7 @@ namespace Artemis.Core.Services
/// <summary> /// <summary>
/// Gets or sets whether rendering should be paused /// Gets or sets whether rendering should be paused
/// </summary> /// </summary>
bool IsRenderPaused { get; set; } bool IsRenderPaused { get; }
/// <summary> /// <summary>
/// Gets a boolean indicating whether the render pipeline is open /// Gets a boolean indicating whether the render pipeline is open
@ -138,6 +138,13 @@ namespace Artemis.Core.Services
/// <param name="device">The device to disable</param> /// <param name="device">The device to disable</param>
void DisableDevice(ArtemisDevice device); 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> /// <summary>
/// Occurs when a single device was added /// Occurs when a single device was added
/// </summary> /// </summary>

View File

@ -27,7 +27,6 @@ namespace Artemis.Core.Services
private readonly PluginSetting<int> _targetFrameRateSetting; private readonly PluginSetting<int> _targetFrameRateSetting;
private readonly TextureBrush _textureBrush = new(ITexture.Empty) {CalculationMode = RenderMode.Absolute}; private readonly TextureBrush _textureBrush = new(ITexture.Empty) {CalculationMode = RenderMode.Absolute};
private Dictionary<Led, ArtemisLed> _ledMap; private Dictionary<Led, ArtemisLed> _ledMap;
private bool _modifyingProviders;
private ListLedGroup? _surfaceLedGroup; private ListLedGroup? _surfaceLedGroup;
private SKTexture? _texture; private SKTexture? _texture;
@ -51,6 +50,7 @@ namespace Artemis.Core.Services
_ledMap = new Dictionary<Led, ArtemisLed>(); _ledMap = new Dictionary<Led, ArtemisLed>();
UpdateTrigger = new TimerUpdateTrigger {UpdateFrequency = 1.0 / _targetFrameRateSetting.Value}; UpdateTrigger = new TimerUpdateTrigger {UpdateFrequency = 1.0 / _targetFrameRateSetting.Value};
SetRenderPaused(true);
Surface.RegisterUpdateTrigger(UpdateTrigger); Surface.RegisterUpdateTrigger(UpdateTrigger);
Utilities.ShutdownRequested += UtilitiesOnShutdownRequested; Utilities.ShutdownRequested += UtilitiesOnShutdownRequested;
@ -81,16 +81,14 @@ namespace Artemis.Core.Services
private void UpdateLedGroup() 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)); _ledMap = new Dictionary<Led, ArtemisLed>(_devices.SelectMany(d => d.Leds).ToDictionary(l => l.RgbLed));
if (_surfaceLedGroup == null) 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(); OnLedsChanged();
return; return;
} }
@ -101,10 +99,16 @@ namespace Artemis.Core.Services
_surfaceLedGroup.Detach(); _surfaceLedGroup.Detach();
// Apply the application wide brush and decorator // 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(); OnLedsChanged();
} }
} }
finally
{
if (changedRenderPaused)
SetRenderPaused(false);
}
} }
private void TargetFrameRateSettingOnSettingChanged(object? sender, EventArgs e) private void TargetFrameRateSettingOnSettingChanged(object? sender, EventArgs e)
@ -141,15 +145,10 @@ namespace Artemis.Core.Services
public void AddDeviceProvider(IRGBDeviceProvider deviceProvider) public void AddDeviceProvider(IRGBDeviceProvider deviceProvider)
{ {
if (RenderOpen) bool changedRenderPaused = SetRenderPaused(true);
throw new ArtemisCoreException("Cannot add a device provider while rendering");
lock (_devices)
{
try try
{ {
_modifyingProviders = true;
List<ArtemisDevice> toRemove = _devices.Where(a => deviceProvider.Devices.Any(d => a.RgbDevice == d)).ToList(); List<ArtemisDevice> toRemove = _devices.Where(a => deviceProvider.Devices.Any(d => a.RgbDevice == d)).ToList();
Surface.Detach(toRemove.Select(d => d.RgbDevice)); Surface.Detach(toRemove.Select(d => d.RgbDevice));
foreach (ArtemisDevice device in toRemove) foreach (ArtemisDevice device in toRemove)
@ -196,23 +195,18 @@ namespace Artemis.Core.Services
} }
finally finally
{ {
_modifyingProviders = false;
UpdateLedGroup(); UpdateLedGroup();
} if (changedRenderPaused)
SetRenderPaused(false);
} }
} }
public void RemoveDeviceProvider(IRGBDeviceProvider deviceProvider) public void RemoveDeviceProvider(IRGBDeviceProvider deviceProvider)
{ {
if (RenderOpen) bool changedRenderPaused = SetRenderPaused(true);
throw new ArtemisCoreException("Cannot update the remove device provider while rendering");
lock (_devices)
{
try try
{ {
_modifyingProviders = true;
List<ArtemisDevice> toRemove = _devices.Where(a => deviceProvider.Devices.Any(d => a.RgbDevice == d)).ToList(); List<ArtemisDevice> toRemove = _devices.Where(a => deviceProvider.Devices.Any(d => a.RgbDevice == d)).ToList();
Surface.Detach(toRemove.Select(d => d.RgbDevice)); Surface.Detach(toRemove.Select(d => d.RgbDevice));
foreach (ArtemisDevice device in toRemove) foreach (ArtemisDevice device in toRemove)
@ -227,9 +221,9 @@ namespace Artemis.Core.Services
} }
finally finally
{ {
_modifyingProviders = false;
UpdateLedGroup(); UpdateLedGroup();
} if (changedRenderPaused)
SetRenderPaused(false);
} }
} }
@ -241,6 +235,24 @@ namespace Artemis.Core.Services
Surface.Dispose(); 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>? DeviceAdded;
public event EventHandler<DeviceEventArgs>? DeviceRemoved; public event EventHandler<DeviceEventArgs>? DeviceRemoved;
public event EventHandler? LedsChanged; public event EventHandler? LedsChanged;
@ -325,6 +337,10 @@ namespace Artemis.Core.Services
#region EnabledDevices #region EnabledDevices
public void AutoArrangeDevices() public void AutoArrangeDevices()
{
bool changedRenderPaused = SetRenderPaused(true);
try
{ {
SurfaceArrangement surfaceArrangement = SurfaceArrangement.GetDefaultArrangement(); SurfaceArrangement surfaceArrangement = SurfaceArrangement.GetDefaultArrangement();
surfaceArrangement.Arrange(_devices); surfaceArrangement.Arrange(_devices);
@ -333,6 +349,12 @@ namespace Artemis.Core.Services
SaveDevices(); SaveDevices();
} }
finally
{
if (changedRenderPaused)
SetRenderPaused(false);
}
}
public ArtemisLayout? ApplyBestDeviceLayout(ArtemisDevice device) public ArtemisLayout? ApplyBestDeviceLayout(ArtemisDevice device)
{ {

View File

@ -92,7 +92,10 @@ namespace Artemis.UI
using HttpClient client = new(); using HttpClient client = new();
try 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(); httpResponseMessage.EnsureSuccessStatusCode();
return true; return true;
} }

View File

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

View File

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