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,7 +34,7 @@ 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
@ -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,15 +145,10 @@ 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
{
_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)
@ -196,23 +195,18 @@ namespace Artemis.Core.Services
}
finally
{
_modifyingProviders = false;
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
{
_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)
@ -227,9 +221,9 @@ namespace Artemis.Core.Services
}
finally
{
_modifyingProviders = false;
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;
@ -325,6 +337,10 @@ namespace Artemis.Core.Services
#region EnabledDevices
public void AutoArrangeDevices()
{
bool changedRenderPaused = SetRenderPaused(true);
try
{
SurfaceArrangement surfaceArrangement = SurfaceArrangement.GetDefaultArrangement();
surfaceArrangement.Arrange(_devices);
@ -333,6 +349,12 @@ namespace Artemis.Core.Services
SaveDevices();
}
finally
{
if (changedRenderPaused)
SetRenderPaused(false);
}
}
public ArtemisLayout? ApplyBestDeviceLayout(ArtemisDevice device)
{

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();
}