Skip to content

Commit

Permalink
prevent weak even subscriptions in temporary EffectLayers
Browse files Browse the repository at this point in the history
  • Loading branch information
Aytackydln committed Sep 1, 2023
1 parent 14c42be commit 1f34368
Show file tree
Hide file tree
Showing 13 changed files with 60 additions and 147 deletions.
9 changes: 7 additions & 2 deletions Project-Aurora/Project-Aurora/EffectsEngine/EffectFrame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@ public sealed class EffectFrame : IDisposable
public void AddLayers(IEnumerable<EffectLayer> effectLayers)
{
foreach (var layer in effectLayers)
_layers.Enqueue(layer);
AddLayer(layer);
}

public void AddLayer(EffectLayer layer)
{
_layers.Enqueue(layer);
}

/// <summary>
Expand All @@ -28,7 +33,7 @@ public void AddLayers(IEnumerable<EffectLayer> effectLayers)
public void AddOverlayLayers(IEnumerable<EffectLayer> effectLayers)
{
foreach(var layer in effectLayers)
_overLayers.Enqueue(layer);
AddOverlayLayer(layer);
}

/// <summary>
Expand Down
20 changes: 18 additions & 2 deletions Project-Aurora/Project-Aurora/EffectsEngine/EffectLayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace Aurora.EffectsEngine
public sealed class EffectLayer : IDisposable
{
private static readonly Lazy<EffectLayer> EmptyLayerFactory = new(
() => new EffectLayer("EmptyLayer"), LazyThreadSafetyMode.PublicationOnly);
() => new EffectLayer("EmptyLayer", true), LazyThreadSafetyMode.PublicationOnly);
public static EffectLayer EmptyLayer => EmptyLayerFactory.Value;

private readonly string _name;
Expand Down Expand Up @@ -56,21 +56,36 @@ internal TextureBrush TextureBrush
}
}

[Obsolete("This creates too much garbage memory")]
public EffectLayer(string name) : this(name, Color.FromArgb(0, 1, 1, 1))
{
}

[Obsolete("This creates too much garbage memory")]
public EffectLayer(string name, Color color)
{
_name = name;
WeakEventManager<Effects, EventArgs>.AddHandler(null, nameof(Effects.CanvasChanged), InvalidateColorMap);
_colormap = new Bitmap(Effects.CanvasWidth, Effects.CanvasHeight);
_textureBrush = new TextureBrush(_colormap);
Dimension = new Rectangle(0, 0, Effects.CanvasWidth, Effects.CanvasHeight);

FillOver(color);
}

public EffectLayer(string name, bool persistent) : this(name)
{
if (!persistent)
return;
WeakEventManager<Effects, EventArgs>.AddHandler(null, nameof(Effects.CanvasChanged), InvalidateColorMap);
}

public EffectLayer(string name, Color color, bool persistent) : this(name, color)
{
if (!persistent)
return;
WeakEventManager<Effects, EventArgs>.AddHandler(null, nameof(Effects.CanvasChanged), InvalidateColorMap);
}

/// <summary>
/// Creates a new instance of the EffectLayer class with a specified layer name. And applies a LayerEffect onto this EffectLayer instance.
/// Using the parameters from LayerEffectConfig and a specified region in RectangleF
Expand Down Expand Up @@ -1085,6 +1100,7 @@ private void InvalidateColorMap(object? sender, EventArgs args)
}

private KeySequence _excludeSequence = new();

/// <summary>
/// Excludes provided sequence from the layer (Applies a mask)
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion Project-Aurora/Project-Aurora/EffectsEngine/Effects.cs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ private set

private readonly Dictionary<DeviceKeys, Color> _keyColors = new(MaxDeviceId, EnumHashGetter.Instance as IEqualityComparer<DeviceKeys>);

private readonly Lazy<EffectLayer> _effectLayerFactory = new(() => new EffectLayer("Global Background", Color.Black));
private readonly Lazy<EffectLayer> _effectLayerFactory = new(() => new EffectLayer("Global Background", Color.Black, true));
private EffectLayer Background => _effectLayerFactory.Value;

private readonly Task<DeviceManager> _deviceManager;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using Aurora.Devices;
using Aurora.EffectsEngine;
using Aurora.Utils;
using LedCSharp;

namespace Aurora.Profiles.Aurora_Wrapper;

Expand All @@ -22,79 +21,8 @@ public class GameEvent_Aurora_Wrapper : LightEvent
private readonly float _colorEnhanceColorHsvSine = 0.1f;
private readonly float _colorEnhanceColorHsvGamma = 2.5f;

protected virtual void UpdateExtraLights(Queue<EffectLayer> layers)
{

}

public sealed override void UpdateLights(EffectFrame frame)
{
UpdateWrapperLights(frame);

var layers = new Queue<EffectLayer>();

if (Application != null)
{
foreach (var layer in Application.Profile.Layers.Reverse().ToArray())
{
if (layer.Enabled)
layers.Enqueue(layer.Render(_game_state));
}

//No need to repeat the code around this everytime this is inherited
UpdateExtraLights(layers);
}

frame.AddLayers(layers.ToArray());
}

protected virtual void UpdateWrapperLights(EffectFrame frame)
{
var layers = new Queue<EffectLayer>();

var colorFillLayer = new EffectLayer("Aurora Wrapper - Color Fill", GetBoostedColor(_lastFillColor));

layers.Enqueue(colorFillLayer);

var bitmapLayer = new EffectLayer("Aurora Wrapper - Bitmap");

var allKeys = Enum.GetValues(typeof(DeviceKeys)).Cast<DeviceKeys>().ToArray();
foreach (var key in allKeys)
{
if(_extraKeys.TryGetValue(key, out var extraKey))
bitmapLayer.Set(key, GetBoostedColor(extraKey));
else
{
var logiKey = DeviceKeysUtils.ToLogitechBitmap(key);

if (logiKey != Logitech_keyboardBitmapKeys.UNKNOWN && _bitmap.Length > 0)
bitmapLayer.Set(key, GetBoostedColor(ColorUtils.GetColorFromInt(_bitmap[(int)logiKey / 4])));
}
}

layers.Enqueue(bitmapLayer);

var effectsLayer = new EffectLayer("Aurora Wrapper - Effects");

var currentTime = Time.GetMillisecondsSinceEpoch();

layers.Enqueue(effectsLayer);

var entireEffectLayer = new EffectLayer("Aurora Wrapper - EntireKB effect");

if (_currentEffect != null)
{
switch (_currentEffect)
{
default:
_currentEffect.SetEffect(entireEffectLayer, currentTime - _currentEffect.TimeStarted);
break;
}
}

layers.Enqueue(entireEffectLayer);

frame.AddLayers(layers.ToArray());
}

public override void SetGameState(IGameState newGameState)
Expand Down
49 changes: 12 additions & 37 deletions Project-Aurora/Project-Aurora/Profiles/Desktop/Event_Desktop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,45 +10,20 @@
using System.Timers;
using System.Windows.Forms;

namespace Aurora.Profiles.Desktop
{
public class Event_Desktop : LightEvent
{
private long _internalCounter;

public Event_Desktop()
{
_internalCounter = 0;
}

public override void UpdateLights(EffectFrame frame)
{
var layers = new Queue<EffectLayer>(
Application.Profile.Layers.Where(l => l.Enabled).Reverse().Select(l => l.Render(_game_state))
);

//Scripts before interactive and shortcut assistant layers
//ProfilesManager.DesktopProfile.UpdateEffectScripts(layers);

if (Global.Configuration.TimeBasedDimmingEnabled)
{
if (Utils.Time.IsCurrentTimeBetween(Global.Configuration.TimeBasedDimmingStartHour, Global.Configuration.TimeBasedDimmingEndHour))
{
layers.Clear();
namespace Aurora.Profiles.Desktop;

var timeBasedDimLayer = new EffectLayer("Time Based Dim");
timeBasedDimLayer.FillOver(Color.Black);

layers.Enqueue(timeBasedDimLayer);
}
}
public class Event_Desktop : LightEvent
{
private readonly EffectLayer _timeBasedDimLayer = new("Time Based Dim", Color.Black, true);

frame.AddLayers(layers.ToArray());
}
public override void UpdateLights(EffectFrame frame)
{
var layers = Application.Profile.Layers.Where(l => l.Enabled).Reverse().Select(l => l.Render(_game_state));
frame.AddLayers(layers);
}

public override void SetGameState(IGameState newGameState)
{
public override void SetGameState(IGameState newGameState)
{

}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace Aurora.Profiles.Desktop;

public sealed class EventIdle : LightEvent
{
private readonly EffectLayer _layer = new("IDLE");
private readonly EffectLayer _layer = new("IDLE", true);

private long _previousTime = Time.GetMillisecondsSinceEpoch();
internal long CurrentTime = Time.GetMillisecondsSinceEpoch();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,8 @@ private void Update()
if (Global.Configuration.TimeBasedDimmingEnabled &&
Time.IsCurrentTimeBetween(dimmingStartTime, dimmingEndTime))
{
var blackFrame = new EffectsEngine.EffectFrame();
Global.effengine.PushFrame(blackFrame);
StopUnUpdatedEvents();
return;
}
Expand Down
2 changes: 1 addition & 1 deletion Project-Aurora/Project-Aurora/Project-Aurora.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Aurora</RootNamespace>
<AssemblyName>Aurora</AssemblyName>
<LangVersion>10</LangVersion>
<LangVersion>11</LangVersion>
<NeutralLanguage>en</NeutralLanguage>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public override void Default()
[LogicOverrideIgnoreProperty("_SecondaryColor")]
public class GradientLayerHandler : LayerHandler<GradientLayerHandlerProperties>
{
private readonly EffectLayer _tempLayerBitmap = new("GradientLayer - Colors");
private readonly EffectLayer _tempLayerBitmap = new("GradientLayer - Colors", true);
private bool _invalidated;

public GradientLayerHandler(): base("GradientLayer")
Expand Down
4 changes: 2 additions & 2 deletions Project-Aurora/Project-Aurora/Settings/Layers/LayerHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ protected LayerHandler(string name)
_secondPrevImageAttributes = new();
_secondPrevImageAttributes.SetColorMatrix(colorMatrix2);

_effectLayer = new(() => new EffectLayer(name));
_effectLayer = new(() => new EffectLayer(name, true));
_ExclusionMask = new KeySequence();
Properties.PropertyChanged += PropertiesChanged;
WeakEventManager<Effects, EventArgs>.AddHandler(null, nameof(Effects.CanvasChanged), PropertiesChanged);
Expand All @@ -323,7 +323,7 @@ public virtual void SetGameState(IGameState gamestate)

}

private readonly Lazy<EffectLayer> _postfxLayer = new(() => new EffectLayer("PostFXLayer"));
private readonly Lazy<EffectLayer> _postfxLayer = new(() => new EffectLayer("PostFXLayer", true));
private readonly ImageAttributes _prevImageAttributes;
private readonly ImageAttributes _secondPrevImageAttributes;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ public class WrapperLightsLayerHandler : LayerHandler<WrapperLightsLayerHandlerP
private readonly Dictionary<DeviceKeys, Color> _extraKeys = new();
private Color _lastFillColor = Color.Black;
private EntireEffect? _currentEffect;
private readonly SolidBrush _fillBrush = new(Color.Transparent);

public WrapperLightsLayerHandler() : base("Aurora Wrapper")
{
}

protected override UserControl CreateControl()
{
Expand All @@ -107,13 +112,8 @@ public override EffectLayer Render(IGameState gamestate)
if (gamestate is not GameState_Wrapper)
return EffectLayer.EmptyLayer;

var layers = new Queue<EffectLayer>();

var colorFillLayer = new EffectLayer("Aurora Wrapper - Color Fill", GetBoostedColor(_lastFillColor));

layers.Enqueue(colorFillLayer);

var bitmapLayer = new EffectLayer("Aurora Wrapper - Bitmap");
_fillBrush.Color = GetBoostedColor(_lastFillColor);
EffectLayer.Fill(_fillBrush);

var allKeys = Enum.GetValues(typeof(DeviceKeys)).Cast<DeviceKeys>().ToArray();
foreach (var key in allKeys)
Expand All @@ -125,43 +125,30 @@ public override EffectLayer Render(IGameState gamestate)

if (_extraKeys.ContainsKey(key))
{
bitmapLayer.Set(key, GetBoostedColor(_extraKeys[key]));
EffectLayer.Set(key, GetBoostedColor(_extraKeys[key]));

// Do the key cloning
if (Properties.CloningMap.TryGetValue(key, out var targetKey))
bitmapLayer.Set(targetKey, GetBoostedColor(_extraKeys[key]));
EffectLayer.Set(targetKey, GetBoostedColor(_extraKeys[key]));
}
else
{
var logiKey = DeviceKeysUtils.ToLogitechBitmap(key);

if (logiKey == Logitech_keyboardBitmapKeys.UNKNOWN || _bitmap.Length <= 0) continue;
var color = GetBoostedColor(ColorUtils.GetColorFromInt(_bitmap[(int)logiKey / 4]));
bitmapLayer.Set(key, color);
EffectLayer.Set(key, color);

// Key cloning
if (Properties.CloningMap.TryGetValue(key, out var targetKey))
bitmapLayer.Set(targetKey, color);
EffectLayer.Set(targetKey, color);
}
}

layers.Enqueue(bitmapLayer);
var effectsLayer = new EffectLayer("Aurora Wrapper - Effects");
layers.Enqueue(effectsLayer);

var entireEffectLayer = new EffectLayer("Aurora Wrapper - EntireKB effect");

var currentTime = Time.GetMillisecondsSinceEpoch();
_currentEffect?.SetEffect(entireEffectLayer, currentTime - _currentEffect.TimeStarted);

layers.Enqueue(entireEffectLayer);

var layersArray = layers.ToArray();
var finalLayer = layersArray[0];
for (var i = 1; i < layersArray.Length; i++)
finalLayer += layersArray[i];
_currentEffect?.SetEffect(EffectLayer, currentTime - _currentEffect.TimeStarted);

return finalLayer;
return EffectLayer;
}

public override void SetGameState(IGameState gamestate)
Expand Down
2 changes: 1 addition & 1 deletion Project-Aurora/Project-Aurora/Vorons/PerfEffect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public PerformanceEffect()

Properties.RegProp("Cycled Gradient Shift Full Speed", 100L, "Cycled gradient shifting speed at 100%", -1000L, 1000L);

_effectLayer = new EffectLayer(ID);
_effectLayer = new EffectLayer(ID, true);
}

private static readonly MathParser MathParser = new();
Expand Down
2 changes: 1 addition & 1 deletion Project-Aurora/Project-Aurora/Vorons/PingEffect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public PingEffect()
Properties.RegProp("Number of Pings in graph mode", 10L, "Amount of last pings that will be used to display graph.", 1, 50);

//Properties.RegProp("BrightMode", false);
_layer = new EffectLayer(ID);
_layer = new EffectLayer(ID, true);
}

private static readonly ConcurrentDictionary<Tuple<KeySequence, string, string>, KeyValuePair<AnimationData, Pinger>> PingAnimations = new();
Expand Down

0 comments on commit 1f34368

Please sign in to comment.