mirror of
https://github.com/DarthAffe/RGB.NET.git
synced 2025-12-13 01:58:30 +00:00
(MAJOR) Improved update performance of devices
This commit is contained in:
parent
4216dacf4f
commit
764fcd1b1d
@ -3,6 +3,7 @@
|
|||||||
// ReSharper disable AutoPropertyCanBeMadeGetOnly.Global
|
// ReSharper disable AutoPropertyCanBeMadeGetOnly.Global
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
|
using System.Buffers;
|
||||||
using System.Collections;
|
using System.Collections;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
@ -100,12 +101,7 @@ public abstract class AbstractRGBDevice<TDeviceInfo> : Placeable, IRGBDevice<TDe
|
|||||||
DeviceUpdate();
|
DeviceUpdate();
|
||||||
|
|
||||||
// Send LEDs to SDK
|
// Send LEDs to SDK
|
||||||
List<Led> ledsToUpdate = GetLedsToUpdate(flushLeds).ToList();
|
UpdateLeds(GetLedsToUpdate(flushLeds));
|
||||||
|
|
||||||
foreach (Led led in ledsToUpdate)
|
|
||||||
led.Update();
|
|
||||||
|
|
||||||
UpdateLeds(ledsToUpdate);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -124,37 +120,38 @@ public abstract class AbstractRGBDevice<TDeviceInfo> : Placeable, IRGBDevice<TDe
|
|||||||
/// </remarks>
|
/// </remarks>
|
||||||
/// <param name="leds">The enumerable of leds to convert.</param>
|
/// <param name="leds">The enumerable of leds to convert.</param>
|
||||||
/// <returns>The enumerable of custom data and color tuples for the specified leds.</returns>
|
/// <returns>The enumerable of custom data and color tuples for the specified leds.</returns>
|
||||||
protected virtual IEnumerable<(object key, Color color)> GetUpdateData(IEnumerable<Led> leds)
|
protected (object key, Color color) GetUpdateData(Led led)
|
||||||
{
|
{
|
||||||
if (ColorCorrections.Count > 0)
|
Color color = led.Color;
|
||||||
{
|
object key = led.CustomData ?? led.Id;
|
||||||
foreach (Led led in leds)
|
|
||||||
{
|
|
||||||
Color color = led.Color;
|
|
||||||
object key = led.CustomData ?? led.Id;
|
|
||||||
|
|
||||||
foreach (IColorCorrection colorCorrection in ColorCorrections)
|
// ReSharper disable once ForCanBeConvertedToForeach - This causes an allocation that's not really needed here
|
||||||
colorCorrection.ApplyTo(ref color);
|
for (int i = 0; i < ColorCorrections.Count; i++)
|
||||||
|
ColorCorrections[i].ApplyTo(ref color);
|
||||||
|
|
||||||
yield return (key, color);
|
return (key, color);
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
foreach (Led led in leds)
|
|
||||||
{
|
|
||||||
Color color = led.Color;
|
|
||||||
object key = led.CustomData ?? led.Id;
|
|
||||||
|
|
||||||
yield return (key, color);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Sends all the updated <see cref="Led"/> to the device.
|
/// Sends all the updated <see cref="Led"/> to the device.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
protected virtual void UpdateLeds(IEnumerable<Led> ledsToUpdate) => UpdateQueue.SetData(GetUpdateData(ledsToUpdate));
|
protected virtual void UpdateLeds(IEnumerable<Led> ledsToUpdate)
|
||||||
|
{
|
||||||
|
(object key, Color color)[] buffer = ArrayPool<(object, Color)>.Shared.Rent(LedMapping.Count);
|
||||||
|
|
||||||
|
int counter = 0;
|
||||||
|
foreach (Led led in ledsToUpdate)
|
||||||
|
{
|
||||||
|
led.Update();
|
||||||
|
|
||||||
|
buffer[counter] = GetUpdateData(led);
|
||||||
|
++counter;
|
||||||
|
}
|
||||||
|
|
||||||
|
UpdateQueue.SetData(new ReadOnlySpan<(object, Color)>(buffer)[..counter]);
|
||||||
|
|
||||||
|
ArrayPool<(object, Color)>.Shared.Return(buffer);
|
||||||
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public virtual void Dispose()
|
public virtual void Dispose()
|
||||||
|
|||||||
@ -1,5 +1,4 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
|
||||||
|
|
||||||
namespace RGB.NET.Core;
|
namespace RGB.NET.Core;
|
||||||
|
|
||||||
@ -21,7 +20,7 @@ public interface IUpdateQueue<TIdentifier, TData> : IReferenceCounting, IDisposa
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="dataSet">The set of data.</param>
|
/// <param name="dataSet">The set of data.</param>
|
||||||
// ReSharper disable once MemberCanBeProtected.Global
|
// ReSharper disable once MemberCanBeProtected.Global
|
||||||
void SetData(IEnumerable<(TIdentifier, TData)> dataSet);
|
void SetData(ReadOnlySpan<(TIdentifier, TData)> dataSet);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Resets the current data set.
|
/// Resets the current data set.
|
||||||
|
|||||||
@ -1,7 +1,6 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Buffers;
|
using System.Buffers;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
|
||||||
|
|
||||||
namespace RGB.NET.Core;
|
namespace RGB.NET.Core;
|
||||||
|
|
||||||
@ -88,10 +87,9 @@ public abstract class UpdateQueue<TIdentifier, TData> : AbstractReferenceCountin
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="dataSet">The set of data.</param>
|
/// <param name="dataSet">The set of data.</param>
|
||||||
// ReSharper disable once MemberCanBeProtected.Global
|
// ReSharper disable once MemberCanBeProtected.Global
|
||||||
public virtual void SetData(IEnumerable<(TIdentifier, TData)> dataSet)
|
public virtual void SetData(ReadOnlySpan<(TIdentifier, TData)> data)
|
||||||
{
|
{
|
||||||
IList<(TIdentifier, TData)> data = dataSet.ToList();
|
if (data.Length == 0) return;
|
||||||
if (data.Count == 0) return;
|
|
||||||
|
|
||||||
lock (_dataLock)
|
lock (_dataLock)
|
||||||
{
|
{
|
||||||
|
|||||||
@ -1,6 +1,4 @@
|
|||||||
using System.Collections.Generic;
|
using RGB.NET.Core;
|
||||||
using System.Linq;
|
|
||||||
using RGB.NET.Core;
|
|
||||||
|
|
||||||
namespace RGB.NET.Devices.Logitech;
|
namespace RGB.NET.Devices.Logitech;
|
||||||
|
|
||||||
@ -42,8 +40,5 @@ public class LogitechPerDeviceRGBDevice : LogitechRGBDevice<LogitechRGBDeviceInf
|
|||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
protected override object GetLedCustomData(LedId ledId) => _ledMapping[ledId];
|
protected override object GetLedCustomData(LedId ledId) => _ledMapping[ledId];
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
protected override void UpdateLeds(IEnumerable<Led> ledsToUpdate) => UpdateQueue.SetData(GetUpdateData(ledsToUpdate.Take(1)));
|
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
@ -1,5 +1,4 @@
|
|||||||
using System.Collections.Generic;
|
using RGB.NET.Core;
|
||||||
using RGB.NET.Core;
|
|
||||||
|
|
||||||
namespace RGB.NET.Devices.Logitech;
|
namespace RGB.NET.Devices.Logitech;
|
||||||
|
|
||||||
@ -33,9 +32,6 @@ public class LogitechPerKeyRGBDevice : LogitechRGBDevice<LogitechRGBDeviceInfo>,
|
|||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
protected override object GetLedCustomData(LedId ledId) => _ledMapping.TryGetValue(ledId, out LogitechLedId logitechLedId) ? logitechLedId : -1;
|
protected override object GetLedCustomData(LedId ledId) => _ledMapping.TryGetValue(ledId, out LogitechLedId logitechLedId) ? logitechLedId : -1;
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
protected override void UpdateLeds(IEnumerable<Led> ledsToUpdate) => UpdateQueue.SetData(GetUpdateData(ledsToUpdate));
|
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
@ -1,5 +1,4 @@
|
|||||||
using System.Collections.Generic;
|
using RGB.NET.Core;
|
||||||
using RGB.NET.Core;
|
|
||||||
|
|
||||||
namespace RGB.NET.Devices.Logitech;
|
namespace RGB.NET.Devices.Logitech;
|
||||||
|
|
||||||
@ -41,8 +40,5 @@ public class LogitechZoneRGBDevice : LogitechRGBDevice<LogitechRGBDeviceInfo>, I
|
|||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
protected override object GetLedCustomData(LedId ledId) => _ledMapping[ledId];
|
protected override object GetLedCustomData(LedId ledId) => _ledMapping[ledId];
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
protected override void UpdateLeds(IEnumerable<Led> ledsToUpdate) => UpdateQueue.SetData(GetUpdateData(ledsToUpdate));
|
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
@ -1,5 +1,4 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
|
||||||
using RGB.NET.Core;
|
using RGB.NET.Core;
|
||||||
|
|
||||||
namespace RGB.NET.Devices.Novation;
|
namespace RGB.NET.Devices.Novation;
|
||||||
@ -34,9 +33,6 @@ public abstract class NovationRGBDevice<TDeviceInfo> : AbstractRGBDevice<TDevice
|
|||||||
_ => throw new ArgumentOutOfRangeException()
|
_ => throw new ArgumentOutOfRangeException()
|
||||||
};
|
};
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
protected override void UpdateLeds(IEnumerable<Led> ledsToUpdate) => UpdateQueue.SetData(GetUpdateData(ledsToUpdate));
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Resets the <see cref="NovationRGBDevice{TDeviceInfo}"/> back to default.
|
/// Resets the <see cref="NovationRGBDevice{TDeviceInfo}"/> back to default.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@ -1,5 +1,4 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
|
||||||
using RGB.NET.Core;
|
using RGB.NET.Core;
|
||||||
|
|
||||||
namespace RGB.NET.Devices.Razer;
|
namespace RGB.NET.Devices.Razer;
|
||||||
@ -27,10 +26,7 @@ public abstract class RazerRGBDevice : AbstractRGBDevice<RazerRGBDeviceInfo>, IR
|
|||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Methods
|
#region Methods
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
protected override void UpdateLeds(IEnumerable<Led> ledsToUpdate) => UpdateQueue.SetData(GetUpdateData(ledsToUpdate));
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public override void Dispose()
|
public override void Dispose()
|
||||||
{
|
{
|
||||||
|
|||||||
@ -1,5 +1,4 @@
|
|||||||
using System.Collections.Generic;
|
using RGB.NET.Core;
|
||||||
using RGB.NET.Core;
|
|
||||||
|
|
||||||
namespace RGB.NET.Devices.SteelSeries;
|
namespace RGB.NET.Devices.SteelSeries;
|
||||||
|
|
||||||
@ -43,8 +42,5 @@ public class SteelSeriesRGBDevice : AbstractRGBDevice<SteelSeriesRGBDeviceInfo>,
|
|||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
protected override object GetLedCustomData(LedId ledId) => _ledMapping[ledId];
|
protected override object GetLedCustomData(LedId ledId) => _ledMapping[ledId];
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
protected override void UpdateLeds(IEnumerable<Led> ledsToUpdate) => UpdateQueue.SetData(GetUpdateData(ledsToUpdate));
|
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
@ -53,9 +53,6 @@ public class ArduinoWS2812USBDevice : AbstractRGBDevice<ArduinoWS2812USBDeviceIn
|
|||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
protected override IEnumerable<Led> GetLedsToUpdate(bool flushLeds) => (flushLeds || LedMapping.Values.Any(x => x.IsDirty)) ? LedMapping.Values : Enumerable.Empty<Led>();
|
protected override IEnumerable<Led> GetLedsToUpdate(bool flushLeds) => (flushLeds || LedMapping.Values.Any(x => x.IsDirty)) ? LedMapping.Values : Enumerable.Empty<Led>();
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
protected override void UpdateLeds(IEnumerable<Led> ledsToUpdate) => UpdateQueue.SetData(GetUpdateData(ledsToUpdate));
|
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
@ -1,7 +1,6 @@
|
|||||||
// ReSharper disable MemberCanBePrivate.Global
|
// ReSharper disable MemberCanBePrivate.Global
|
||||||
// ReSharper disable UnusedMember.Global
|
// ReSharper disable UnusedMember.Global
|
||||||
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using RGB.NET.Core;
|
using RGB.NET.Core;
|
||||||
|
|
||||||
namespace RGB.NET.Devices.WS281X.Bitwizard;
|
namespace RGB.NET.Devices.WS281X.Bitwizard;
|
||||||
@ -47,9 +46,6 @@ public class BitwizardWS2812USBDevice : AbstractRGBDevice<BitwizardWS2812USBDevi
|
|||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
protected override object GetLedCustomData(LedId ledId) => _ledOffset + ((int)ledId - (int)LedId.LedStripe1);
|
protected override object GetLedCustomData(LedId ledId) => _ledOffset + ((int)ledId - (int)LedId.LedStripe1);
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
protected override void UpdateLeds(IEnumerable<Led> ledsToUpdate) => UpdateQueue.SetData(GetUpdateData(ledsToUpdate));
|
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
@ -55,9 +55,6 @@ public class NodeMCUWS2812USBDevice : AbstractRGBDevice<NodeMCUWS2812USBDeviceIn
|
|||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
protected override IEnumerable<Led> GetLedsToUpdate(bool flushLeds) => (flushLeds || LedMapping.Values.Any(x => x.IsDirty)) ? LedMapping.Values : Enumerable.Empty<Led>();
|
protected override IEnumerable<Led> GetLedsToUpdate(bool flushLeds) => (flushLeds || LedMapping.Values.Any(x => x.IsDirty)) ? LedMapping.Values : Enumerable.Empty<Led>();
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
protected override void UpdateLeds(IEnumerable<Led> ledsToUpdate) => UpdateQueue.SetData(GetUpdateData(ledsToUpdate));
|
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
@ -47,9 +47,6 @@ public class WootingKeyboardRGBDevice : WootingRGBDevice<WootingKeyboardRGBDevic
|
|||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
protected override object GetLedCustomData(LedId ledId) => WootingKeyboardLedMappings.Mapping[DeviceInfo.WootingDeviceType][ledId];
|
protected override object GetLedCustomData(LedId ledId) => WootingKeyboardLedMappings.Mapping[DeviceInfo.WootingDeviceType][ledId];
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
protected override void UpdateLeds(IEnumerable<Led> ledsToUpdate) => UpdateQueue.SetData(GetUpdateData(ledsToUpdate));
|
|
||||||
|
|
||||||
public override void Dispose()
|
public override void Dispose()
|
||||||
{
|
{
|
||||||
_WootingSDK.SelectDevice(DeviceInfo.WootingDeviceIndex);
|
_WootingSDK.SelectDevice(DeviceInfo.WootingDeviceIndex);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user