From aace06b6cc95d3937d0eb92aed128e1f63f87943 Mon Sep 17 00:00:00 2001 From: Robert Date: Fri, 13 Aug 2021 22:04:26 +0200 Subject: [PATCH] Core - Added better way to flush LEDs UI - Fixed a freeze when opening the device properties window of a device Core - Updated RGB.NET sampler call for API changes --- src/Artemis.Core/RGB.NET/SKTexture.cs | 4 +- src/Artemis.Core/Services/CoreService.cs | 8 ++- .../Services/Interfaces/IRgbService.cs | 5 ++ src/Artemis.Core/Services/RgbService.cs | 72 +++++++++++-------- .../Tabs/DevicePropertiesTabViewModel.cs | 2 +- 5 files changed, 57 insertions(+), 34 deletions(-) diff --git a/src/Artemis.Core/RGB.NET/SKTexture.cs b/src/Artemis.Core/RGB.NET/SKTexture.cs index 27a9f6d5b..6bb77132a 100644 --- a/src/Artemis.Core/RGB.NET/SKTexture.cs +++ b/src/Artemis.Core/RGB.NET/SKTexture.cs @@ -107,7 +107,7 @@ namespace Artemis.Core GetRegionData(skRectI.Left, skRectI.Top, skRectI.Width, skRectI.Height, buffer); Span pixelData = stackalloc byte[DATA_PER_PIXEL]; - Sampler.SampleColor(new SamplerInfo(skRectI.Width, skRectI.Height, buffer), pixelData); + Sampler.Sample(new SamplerInfo(skRectI.Width, skRectI.Height, buffer), pixelData); return GetColor(pixelData); } @@ -119,7 +119,7 @@ namespace Artemis.Core GetRegionData(skRectI.Left, skRectI.Top, skRectI.Width, skRectI.Height, buffer); Span pixelData = stackalloc byte[DATA_PER_PIXEL]; - Sampler.SampleColor(new SamplerInfo(skRectI.Width, skRectI.Height, buffer), pixelData); + Sampler.Sample(new SamplerInfo(skRectI.Width, skRectI.Height, buffer), pixelData); ArrayPool.Shared.Return(rent); diff --git a/src/Artemis.Core/Services/CoreService.cs b/src/Artemis.Core/Services/CoreService.cs index a9fc7a45e..9dee9fedb 100644 --- a/src/Artemis.Core/Services/CoreService.cs +++ b/src/Artemis.Core/Services/CoreService.cs @@ -13,7 +13,6 @@ using RGB.NET.Core; using Serilog; using Serilog.Events; using SkiaSharp; -using Module = Artemis.Core.Modules.Module; namespace Artemis.Core.Services { @@ -107,6 +106,13 @@ namespace Artemis.Core.Services if (_rgbService.IsRenderPaused) return; + if (_rgbService.FlushLeds) + { + _rgbService.FlushLeds = false; + _rgbService.Surface.Update(true); + return; + } + try { _frameStopWatch.Restart(); diff --git a/src/Artemis.Core/Services/Interfaces/IRgbService.cs b/src/Artemis.Core/Services/Interfaces/IRgbService.cs index 10ec9fa1b..5420edb8f 100644 --- a/src/Artemis.Core/Services/Interfaces/IRgbService.cs +++ b/src/Artemis.Core/Services/Interfaces/IRgbService.cs @@ -41,6 +41,11 @@ namespace Artemis.Core.Services /// bool RenderOpen { get; } + /// + /// Gets or sets a boolean indicating whether to flush the RGB.NET LEDs during next update + /// + bool FlushLeds { get; set; } + /// /// Opens the render pipeline /// diff --git a/src/Artemis.Core/Services/RgbService.cs b/src/Artemis.Core/Services/RgbService.cs index feff46dc2..f43b1ccc1 100644 --- a/src/Artemis.Core/Services/RgbService.cs +++ b/src/Artemis.Core/Services/RgbService.cs @@ -53,7 +53,7 @@ namespace Artemis.Core.Services UpdateTrigger = new TimerUpdateTrigger {UpdateFrequency = 1.0 / _targetFrameRateSetting.Value}; Surface.RegisterUpdateTrigger(UpdateTrigger); - + Utilities.ShutdownRequested += UtilitiesOnShutdownRequested; } @@ -137,8 +137,14 @@ namespace Artemis.Core.Services public bool IsRenderPaused { get; set; } public bool RenderOpen { get; private set; } + /// + public bool FlushLeds { get; set; } + public void AddDeviceProvider(IRGBDeviceProvider deviceProvider) { + if (RenderOpen) + throw new ArtemisCoreException("Cannot add a device provider while rendering"); + lock (_devices) { try @@ -199,6 +205,9 @@ namespace Artemis.Core.Services public void RemoveDeviceProvider(IRGBDeviceProvider deviceProvider) { + if (RenderOpen) + throw new ArtemisCoreException("Cannot update the remove device provider while rendering"); + lock (_devices) { try @@ -267,36 +276,39 @@ namespace Artemis.Core.Services if (RenderOpen) throw new ArtemisCoreException("Cannot update the texture while rendering"); - IManagedGraphicsContext? graphicsContext = Constants.ManagedGraphicsContext = _newGraphicsContext; - if (!ReferenceEquals(graphicsContext, _newGraphicsContext)) - graphicsContext = _newGraphicsContext; - - if (graphicsContext != null) - _logger.Debug("Creating SKTexture with graphics context {graphicsContext}", graphicsContext.GetType().Name); - else - _logger.Debug("Creating SKTexture with software-based graphics context"); - - float evenWidth = Surface.Boundary.Size.Width; - if (evenWidth % 2 != 0) - evenWidth++; - float evenHeight = Surface.Boundary.Size.Height; - if (evenHeight % 2 != 0) - evenHeight++; - - float renderScale = (float) _renderScaleSetting.Value; - int width = Math.Max(1, MathF.Min(evenWidth * renderScale, 4096).RoundToInt()); - int height = Math.Max(1, MathF.Min(evenHeight * renderScale, 4096).RoundToInt()); - - _texture?.Dispose(); - _texture = new SKTexture(graphicsContext, width, height, renderScale); - _textureBrush.Texture = _texture; - - - if (!ReferenceEquals(_newGraphicsContext, Constants.ManagedGraphicsContext = _newGraphicsContext)) + lock (_devices) { - Constants.ManagedGraphicsContext?.Dispose(); - Constants.ManagedGraphicsContext = _newGraphicsContext; - _newGraphicsContext = null; + IManagedGraphicsContext? graphicsContext = Constants.ManagedGraphicsContext = _newGraphicsContext; + if (!ReferenceEquals(graphicsContext, _newGraphicsContext)) + graphicsContext = _newGraphicsContext; + + if (graphicsContext != null) + _logger.Debug("Creating SKTexture with graphics context {graphicsContext}", graphicsContext.GetType().Name); + else + _logger.Debug("Creating SKTexture with software-based graphics context"); + + float evenWidth = Surface.Boundary.Size.Width; + if (evenWidth % 2 != 0) + evenWidth++; + float evenHeight = Surface.Boundary.Size.Height; + if (evenHeight % 2 != 0) + evenHeight++; + + float renderScale = (float) _renderScaleSetting.Value; + int width = Math.Max(1, MathF.Min(evenWidth * renderScale, 4096).RoundToInt()); + int height = Math.Max(1, MathF.Min(evenHeight * renderScale, 4096).RoundToInt()); + + _texture?.Dispose(); + _texture = new SKTexture(graphicsContext, width, height, renderScale); + _textureBrush.Texture = _texture; + + + if (!ReferenceEquals(_newGraphicsContext, Constants.ManagedGraphicsContext = _newGraphicsContext)) + { + Constants.ManagedGraphicsContext?.Dispose(); + Constants.ManagedGraphicsContext = _newGraphicsContext; + _newGraphicsContext = null; + } } } diff --git a/src/Artemis.UI/Screens/Settings/Device/Tabs/DevicePropertiesTabViewModel.cs b/src/Artemis.UI/Screens/Settings/Device/Tabs/DevicePropertiesTabViewModel.cs index 244b7e448..6d2800bfa 100644 --- a/src/Artemis.UI/Screens/Settings/Device/Tabs/DevicePropertiesTabViewModel.cs +++ b/src/Artemis.UI/Screens/Settings/Device/Tabs/DevicePropertiesTabViewModel.cs @@ -140,7 +140,7 @@ namespace Artemis.UI.Screens.Settings.Device.Tabs Device.GreenScale = GreenScale / 100f; Device.BlueScale = BlueScale / 100f; - _rgbService.Surface.Update(true); + _rgbService.FlushLeds = true; } public void BrowseCustomLayout(object sender, MouseEventArgs e)