1
0
mirror of https://github.com/Artemis-RGB/Artemis synced 2025-12-13 05:48:35 +00:00

Major upgrade of RGB.NET to v3

This commit is contained in:
Darth Affe 2024-07-22 23:26:53 +02:00
parent 7f5bb589af
commit db84f1dc75
4 changed files with 34 additions and 58 deletions

View File

@ -39,6 +39,7 @@
<PackageReference Include="DryIoc.dll" /> <PackageReference Include="DryIoc.dll" />
<PackageReference Include="EmbedIO" /> <PackageReference Include="EmbedIO" />
<PackageReference Include="HidSharp" /> <PackageReference Include="HidSharp" />
<PackageReference Include="HPPH.SkiaSharp" />
<PackageReference Include="Humanizer.Core" /> <PackageReference Include="Humanizer.Core" />
<PackageReference Include="JetBrains.Annotations" PrivateAssets="All" /> <PackageReference Include="JetBrains.Annotations" PrivateAssets="All" />
<PackageReference Include="McMaster.NETCore.Plugins" /> <PackageReference Include="McMaster.NETCore.Plugins" />

View File

@ -1,10 +1,10 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using Artemis.Core.SkiaSharp; using Artemis.Core.SkiaSharp;
using HPPH;
using HPPH.SkiaSharp;
using RGB.NET.Core; using RGB.NET.Core;
using RGB.NET.Presets.Textures.Sampler; using RGB.NET.Presets.Extensions;
using SkiaSharp; using SkiaSharp;
namespace Artemis.Core; namespace Artemis.Core;
@ -12,26 +12,20 @@ namespace Artemis.Core;
/// <summary> /// <summary>
/// Represents a SkiaSharp-based RGB.NET PixelTexture /// Represents a SkiaSharp-based RGB.NET PixelTexture
/// </summary> /// </summary>
public sealed class SKTexture : PixelTexture<byte>, IDisposable public sealed class SKTexture : ITexture, IDisposable
{ {
private readonly Dictionary<Led, SKRectI> _ledRects;
private readonly SKPixmap _pixelData;
private readonly IntPtr _pixelDataPtr;
#region Constructors #region Constructors
internal SKTexture(IManagedGraphicsContext? graphicsContext, int width, int height, float scale, IReadOnlyCollection<ArtemisDevice> devices) : base(width, height, DATA_PER_PIXEL, internal SKTexture(IManagedGraphicsContext? graphicsContext, int width, int height, float scale, IReadOnlyCollection<ArtemisDevice> devices)
new AverageByteSampler())
{ {
RenderScale = scale;
Size = new Size(width, height);
ImageInfo = new SKImageInfo(width, height); ImageInfo = new SKImageInfo(width, height);
Surface = graphicsContext == null Surface = graphicsContext == null
? SKSurface.Create(ImageInfo) ? SKSurface.Create(ImageInfo)
: SKSurface.Create(graphicsContext.GraphicsContext, true, ImageInfo); : SKSurface.Create(graphicsContext.GraphicsContext, true, ImageInfo);
RenderScale = scale;
_pixelDataPtr = Marshal.AllocHGlobal(ImageInfo.BytesSize);
_pixelData = new SKPixmap(ImageInfo, _pixelDataPtr, ImageInfo.RowBytes);
_ledRects = new Dictionary<Led, SKRectI>();
foreach (ArtemisDevice artemisDevice in devices) foreach (ArtemisDevice artemisDevice in devices)
{ {
foreach (ArtemisLed artemisLed in artemisDevice.Leds) foreach (ArtemisLed artemisLed in artemisDevice.Leds)
@ -50,47 +44,29 @@ public sealed class SKTexture : PixelTexture<byte>, IDisposable
internal Color GetColorAtRenderTarget(in RenderTarget renderTarget) internal Color GetColorAtRenderTarget(in RenderTarget renderTarget)
{ {
if (Data.Length == 0) return Color.Transparent; if (_image == null) return Color.Transparent;
SKRectI skRectI = _ledRects[renderTarget.Led]; SKRectI skRectI = _ledRects[renderTarget.Led];
if (skRectI.Width <= 0 || skRectI.Height <= 0) if (skRectI.Width <= 0 || skRectI.Height <= 0)
return Color.Transparent; return Color.Transparent;
SamplerInfo<byte> samplerInfo = new(skRectI.Left, skRectI.Top, skRectI.Width, skRectI.Height, Stride, DataPerPixel, Data); return _image[skRectI.Left, skRectI.Top, skRectI.Width, skRectI.Height].Average().ToColor();
Span<byte> pixelData = stackalloc byte[DATA_PER_PIXEL];
Sampler.Sample(samplerInfo, pixelData);
return GetColor(pixelData);
}
private void ReleaseUnmanagedResources()
{
Marshal.FreeHGlobal(_pixelDataPtr);
} }
/// <inheritdoc /> /// <inheritdoc />
~SKTexture() ~SKTexture()
{ {
ReleaseUnmanagedResources(); Dispose();
} }
/// <inheritdoc /> /// <inheritdoc />
public void Dispose() public void Dispose()
{ {
Surface.Dispose(); Surface.Dispose();
_pixelData.Dispose();
ReleaseUnmanagedResources();
GC.SuppressFinalize(this); GC.SuppressFinalize(this);
} }
#region Constants
private const int DATA_PER_PIXEL = 4;
#endregion
#region Methods #region Methods
/// <summary> /// <summary>
@ -104,20 +80,23 @@ public sealed class SKTexture : PixelTexture<byte>, IDisposable
internal void CopyPixelData() internal void CopyPixelData()
{ {
using SKImage skImage = Surface.Snapshot(); using SKImage skImage = Surface.Snapshot();
skImage.ReadPixels(_pixelData); _image = skImage.ToImage();
} }
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected override Color GetColor(in ReadOnlySpan<byte> pixel) => new(pixel[2], pixel[1], pixel[0]);
/// <inheritdoc />
public override Color this[in Rectangle rectangle] => Color.Transparent;
#endregion #endregion
#region Properties & Fields #region Properties & Fields
private IImage? _image;
private readonly Dictionary<Led, SKRectI> _ledRects = [];
/// <inheritdoc />
public Size Size { get; }
/// <inheritdoc />
public Color this[Point point] => Color.Transparent;
/// <inheritdoc />
public Color this[Rectangle rectangle] => Color.Transparent;
/// <summary> /// <summary>
/// Gets the SKBitmap backing this texture /// Gets the SKBitmap backing this texture
/// </summary> /// </summary>
@ -128,11 +107,6 @@ public sealed class SKTexture : PixelTexture<byte>, IDisposable
/// </summary> /// </summary>
public SKImageInfo ImageInfo { get; } public SKImageInfo ImageInfo { get; }
/// <summary>
/// Gets the color data in RGB format
/// </summary>
protected override ReadOnlySpan<byte> Data => _pixelData.GetPixelSpan();
/// <summary> /// <summary>
/// Gets the render scale of the texture /// Gets the render scale of the texture
/// </summary> /// </summary>

View File

@ -20,7 +20,7 @@ internal class SKTextureBrush : AbstractBrush
#region Methods #region Methods
/// <inheritdoc /> /// <inheritdoc />
protected override Color GetColorAtPoint(in Rectangle rectangle, in RenderTarget renderTarget) protected override Color GetColorAtPoint(Rectangle rectangle, RenderTarget renderTarget)
{ {
return Texture?.GetColorAtRenderTarget(renderTarget) ?? Color.Transparent; return Texture?.GetColorAtRenderTarget(renderTarget) ?? Color.Transparent;
} }

View File

@ -14,6 +14,7 @@
<PackageVersion Include="Avalonia.ReactiveUI" Version="11.0.11" /> <PackageVersion Include="Avalonia.ReactiveUI" Version="11.0.11" />
<PackageVersion Include="Avalonia.Skia.Lottie" Version="11.0.0" /> <PackageVersion Include="Avalonia.Skia.Lottie" Version="11.0.0" />
<PackageVersion Include="Avalonia.Win32" Version="11.0.11" /> <PackageVersion Include="Avalonia.Win32" Version="11.0.11" />
<PackageVersion Include="HPPH.SkiaSharp" Version="1.0.0" />
<PackageVersion Include="Microsoft.Win32.SystemEvents" Version="8.0.0" /> <PackageVersion Include="Microsoft.Win32.SystemEvents" Version="8.0.0" />
<PackageVersion Include="Avalonia.Xaml.Behaviors" Version="11.0.10.9" /> <PackageVersion Include="Avalonia.Xaml.Behaviors" Version="11.0.10.9" />
<PackageVersion Include="AvaloniaEdit.TextMate" Version="11.0.6" /> <PackageVersion Include="AvaloniaEdit.TextMate" Version="11.0.6" />
@ -44,9 +45,9 @@
<PackageVersion Include="NoStringEvaluating" Version="2.5.2" /> <PackageVersion Include="NoStringEvaluating" Version="2.5.2" />
<PackageVersion Include="Octopus.Octodiff" Version="2.0.546" /> <PackageVersion Include="Octopus.Octodiff" Version="2.0.546" />
<PackageVersion Include="PropertyChanged.SourceGenerator" Version="1.1.0" /> <PackageVersion Include="PropertyChanged.SourceGenerator" Version="1.1.0" />
<PackageVersion Include="RGB.NET.Core" Version="2.1.0" /> <PackageVersion Include="RGB.NET.Core" Version="3.0.0-prerelease.1 " />
<PackageVersion Include="RGB.NET.Layout" Version="2.1.0" /> <PackageVersion Include="RGB.NET.Layout" Version="3.0.0-prerelease.1 " />
<PackageVersion Include="RGB.NET.Presets" Version="2.1.0" /> <PackageVersion Include="RGB.NET.Presets" Version="3.0.0-prerelease.1 " />
<PackageVersion Include="RawInput.Sharp" Version="0.1.3" /> <PackageVersion Include="RawInput.Sharp" Version="0.1.3" />
<PackageVersion Include="ReactiveUI" Version="20.1.1" /> <PackageVersion Include="ReactiveUI" Version="20.1.1" />
<PackageVersion Include="ReactiveUI.Validation" Version="4.0.9" /> <PackageVersion Include="ReactiveUI.Validation" Version="4.0.9" />