1
0
mirror of https://github.com/DarthAffe/RGB.NET.git synced 2025-12-12 17:48:31 +00:00

(MAJOR) Improved update performance of devices

This commit is contained in:
Darth Affe 2023-04-12 22:25:24 +02:00
parent 4216dacf4f
commit 764fcd1b1d
13 changed files with 38 additions and 82 deletions

View File

@ -3,6 +3,7 @@
// ReSharper disable AutoPropertyCanBeMadeGetOnly.Global
using System;
using System.Buffers;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
@ -100,12 +101,7 @@ public abstract class AbstractRGBDevice<TDeviceInfo> : Placeable, IRGBDevice<TDe
DeviceUpdate();
// Send LEDs to SDK
List<Led> ledsToUpdate = GetLedsToUpdate(flushLeds).ToList();
foreach (Led led in ledsToUpdate)
led.Update();
UpdateLeds(ledsToUpdate);
UpdateLeds(GetLedsToUpdate(flushLeds));
}
/// <summary>
@ -124,37 +120,38 @@ public abstract class AbstractRGBDevice<TDeviceInfo> : Placeable, IRGBDevice<TDe
/// </remarks>
/// <param name="leds">The enumerable of leds to convert.</param>
/// <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)
{
foreach (Led led in leds)
{
Color color = led.Color;
object key = led.CustomData ?? led.Id;
Color color = led.Color;
object key = led.CustomData ?? led.Id;
foreach (IColorCorrection colorCorrection in ColorCorrections)
colorCorrection.ApplyTo(ref color);
// ReSharper disable once ForCanBeConvertedToForeach - This causes an allocation that's not really needed here
for (int i = 0; i < ColorCorrections.Count; i++)
ColorCorrections[i].ApplyTo(ref color);
yield return (key, color);
}
}
else
{
foreach (Led led in leds)
{
Color color = led.Color;
object key = led.CustomData ?? led.Id;
yield return (key, color);
}
}
return (key, color);
}
/// <summary>
/// Sends all the updated <see cref="Led"/> to the device.
/// </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 />
public virtual void Dispose()

View File

@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
namespace RGB.NET.Core;
@ -21,7 +20,7 @@ public interface IUpdateQueue<TIdentifier, TData> : IReferenceCounting, IDisposa
/// </summary>
/// <param name="dataSet">The set of data.</param>
// ReSharper disable once MemberCanBeProtected.Global
void SetData(IEnumerable<(TIdentifier, TData)> dataSet);
void SetData(ReadOnlySpan<(TIdentifier, TData)> dataSet);
/// <summary>
/// Resets the current data set.

View File

@ -1,7 +1,6 @@
using System;
using System.Buffers;
using System.Collections.Generic;
using System.Linq;
namespace RGB.NET.Core;
@ -88,10 +87,9 @@ public abstract class UpdateQueue<TIdentifier, TData> : AbstractReferenceCountin
/// </summary>
/// <param name="dataSet">The set of data.</param>
// 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.Count == 0) return;
if (data.Length == 0) return;
lock (_dataLock)
{

View File

@ -1,6 +1,4 @@
using System.Collections.Generic;
using System.Linq;
using RGB.NET.Core;
using RGB.NET.Core;
namespace RGB.NET.Devices.Logitech;
@ -42,8 +40,5 @@ public class LogitechPerDeviceRGBDevice : LogitechRGBDevice<LogitechRGBDeviceInf
/// <inheritdoc />
protected override object GetLedCustomData(LedId ledId) => _ledMapping[ledId];
/// <inheritdoc />
protected override void UpdateLeds(IEnumerable<Led> ledsToUpdate) => UpdateQueue.SetData(GetUpdateData(ledsToUpdate.Take(1)));
#endregion
}

View File

@ -1,5 +1,4 @@
using System.Collections.Generic;
using RGB.NET.Core;
using RGB.NET.Core;
namespace RGB.NET.Devices.Logitech;
@ -33,9 +32,6 @@ public class LogitechPerKeyRGBDevice : LogitechRGBDevice<LogitechRGBDeviceInfo>,
/// <inheritdoc />
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
}

View File

@ -1,5 +1,4 @@
using System.Collections.Generic;
using RGB.NET.Core;
using RGB.NET.Core;
namespace RGB.NET.Devices.Logitech;
@ -41,8 +40,5 @@ public class LogitechZoneRGBDevice : LogitechRGBDevice<LogitechRGBDeviceInfo>, I
/// <inheritdoc />
protected override object GetLedCustomData(LedId ledId) => _ledMapping[ledId];
/// <inheritdoc />
protected override void UpdateLeds(IEnumerable<Led> ledsToUpdate) => UpdateQueue.SetData(GetUpdateData(ledsToUpdate));
#endregion
}

View File

@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using RGB.NET.Core;
namespace RGB.NET.Devices.Novation;
@ -34,9 +33,6 @@ public abstract class NovationRGBDevice<TDeviceInfo> : AbstractRGBDevice<TDevice
_ => throw new ArgumentOutOfRangeException()
};
/// <inheritdoc />
protected override void UpdateLeds(IEnumerable<Led> ledsToUpdate) => UpdateQueue.SetData(GetUpdateData(ledsToUpdate));
/// <summary>
/// Resets the <see cref="NovationRGBDevice{TDeviceInfo}"/> back to default.
/// </summary>

View File

@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using RGB.NET.Core;
namespace RGB.NET.Devices.Razer;
@ -27,10 +26,7 @@ public abstract class RazerRGBDevice : AbstractRGBDevice<RazerRGBDeviceInfo>, IR
#endregion
#region Methods
/// <inheritdoc />
protected override void UpdateLeds(IEnumerable<Led> ledsToUpdate) => UpdateQueue.SetData(GetUpdateData(ledsToUpdate));
/// <inheritdoc />
public override void Dispose()
{

View File

@ -1,5 +1,4 @@
using System.Collections.Generic;
using RGB.NET.Core;
using RGB.NET.Core;
namespace RGB.NET.Devices.SteelSeries;
@ -43,8 +42,5 @@ public class SteelSeriesRGBDevice : AbstractRGBDevice<SteelSeriesRGBDeviceInfo>,
/// <inheritdoc />
protected override object GetLedCustomData(LedId ledId) => _ledMapping[ledId];
/// <inheritdoc />
protected override void UpdateLeds(IEnumerable<Led> ledsToUpdate) => UpdateQueue.SetData(GetUpdateData(ledsToUpdate));
#endregion
}

View File

@ -53,9 +53,6 @@ public class ArduinoWS2812USBDevice : AbstractRGBDevice<ArduinoWS2812USBDeviceIn
/// <inheritdoc />
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
}

View File

@ -1,7 +1,6 @@
// ReSharper disable MemberCanBePrivate.Global
// ReSharper disable UnusedMember.Global
using System.Collections.Generic;
using RGB.NET.Core;
namespace RGB.NET.Devices.WS281X.Bitwizard;
@ -47,9 +46,6 @@ public class BitwizardWS2812USBDevice : AbstractRGBDevice<BitwizardWS2812USBDevi
/// <inheritdoc />
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
}

View File

@ -55,9 +55,6 @@ public class NodeMCUWS2812USBDevice : AbstractRGBDevice<NodeMCUWS2812USBDeviceIn
/// <inheritdoc />
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
}

View File

@ -47,9 +47,6 @@ public class WootingKeyboardRGBDevice : WootingRGBDevice<WootingKeyboardRGBDevic
/// <inheritdoc />
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()
{
_WootingSDK.SelectDevice(DeviceInfo.WootingDeviceIndex);