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