WIP - Added a BitmapBrush to replace the GraphicsDecorator

This commit is contained in:
SpoinkyNL committed 2019-12-11 00:04:23 +01:00
1 parent fe21bc83da
commit a2871541b9
15 files changed
+399 -116

No files matched your search

+2 -1
View File
@@ -160,7 +160,7 @@
<Compile Include="Extensions\DirectoryInfoExtensions.cs" />
<Compile Include="Extensions\DoubleExtensions.cs" />
<Compile Include="Extensions\FloatExtensions.cs" />
<Compile Include="Extensions\RgbColorExtensions.cs" />
<Compile Include="Extensions\SKColorExtensions.cs" />
<Compile Include="Extensions\RgbDeviceExtensions.cs" />
<Compile Include="Extensions\RgbRectangleExtensions.cs" />
<Compile Include="Extensions\TypeExtensions.cs" />
@@ -194,6 +194,7 @@
<Compile Include="Models\Profile\Profile.cs" />
<Compile Include="Ninject\CoreModule.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="RGB.NET\BitmapBrush.cs" />
<Compile Include="RGB.NET\GraphicsDecorator.cs" />
<Compile Include="Services\DeviceService.cs" />
<Compile Include="Services\Interfaces\ILayerService.cs" />
@@ -1,5 +1,4 @@
using System;
using System.Drawing;
using Artemis.Core.RGB.NET;
using RGB.NET.Core;
@@ -7,13 +6,13 @@ namespace Artemis.Core.Events
{
public class FrameRenderedEventArgs : EventArgs
{
public FrameRenderedEventArgs(GraphicsDecorator graphicsDecorator, RGBSurface rgbSurface)
public FrameRenderedEventArgs(BitmapBrush bitmapBrush, RGBSurface rgbSurface)
{
GraphicsDecorator = graphicsDecorator;
BitmapBrush = bitmapBrush;
RgbSurface = rgbSurface;
}
public GraphicsDecorator GraphicsDecorator { get; }
public BitmapBrush BitmapBrush { get; }
public RGBSurface RgbSurface { get; }
}
}
@@ -1,14 +0,0 @@
using RGB.NET.Core;
using Color = System.Windows.Media.Color;
namespace Artemis.Core.Extensions
{
public static class RgbColorExtensions
{
public static Color ToMediaColor(this global::RGB.NET.Core.Color color)
{
var (_, r, g, b) = color.GetRGBBytes();
return Color.FromRgb(r, g, b);
}
}
}
@@ -0,0 +1,14 @@
using RGB.NET.Core;
using SkiaSharp;
namespace Artemis.Core.Extensions
{
// ReSharper disable once InconsistentNaming - I didn't come up with SKColor
public static class SKColorExtensions
{
public static Color ToRgbColor(this SKColor color)
{
return new Color(color.Alpha, color.Red, color.Green, color.Blue);
}
}
}
+109
View File
@@ -0,0 +1,109 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using Artemis.Core.Extensions;
using RGB.NET.Core;
using SkiaSharp;
namespace Artemis.Core.RGB.NET
{
public class BitmapBrush : AbstractDecoratable<IBrushDecorator>, IBrush, IDisposable
{
#region Constructors
public BitmapBrush(Scale scale)
{
Scale = scale;
}
#endregion
#region Properties & Fields
/// <inheritdoc />
public bool IsEnabled { get; set; } = true;
/// <inheritdoc />
public BrushCalculationMode BrushCalculationMode { get; set; } = BrushCalculationMode.Absolute;
/// <inheritdoc />
public double Brightness { get; set; }
/// <inheritdoc />
public double Opacity { get; set; }
/// <inheritdoc />
public IList<IColorCorrection> ColorCorrections { get; } = new List<IColorCorrection>();
/// <inheritdoc />
public Rectangle RenderedRectangle { get; private set; }
/// <inheritdoc />
public Dictionary<BrushRenderTarget, Color> RenderedTargets { get; } = new Dictionary<BrushRenderTarget, Color>();
public Scale Scale { get; set; }
public SKBitmap Bitmap { get; private set; }
#endregion
#region Methods
/// <inheritdoc />
public virtual void PerformRender(Rectangle rectangle, IEnumerable<BrushRenderTarget> renderTargets)
{
if (RenderedRectangle != rectangle || RenderedScale != Scale)
Bitmap = null;
if (renderTargets.Any())
{
var test = RGBSurface.Instance.SurfaceRectangle;
var width = renderTargets.Max(l => l.Led.AbsoluteLedRectangle.Location.X + l.Led.AbsoluteLedRectangle.Size.Width);
var height = renderTargets.Max(l => l.Led.AbsoluteLedRectangle.Location.Y + l.Led.AbsoluteLedRectangle.Size.Height);
}
RenderedRectangle = rectangle;
RenderedScale = Scale;
RenderedTargets.Clear();
if (Bitmap == null)
CreateBitmap(RenderedRectangle);
foreach (var renderTarget in renderTargets)
{
var scaledLocation = renderTarget.Point * Scale;
if (scaledLocation.X < Bitmap.Width && scaledLocation.Y < Bitmap.Height)
{
RenderedTargets[renderTarget] = Bitmap.GetPixel(RoundToInt(scaledLocation.X), RoundToInt(scaledLocation.Y)).ToRgbColor();
}
}
}
public Scale RenderedScale { get; private set; }
private void CreateBitmap(Rectangle rectangle)
{
var width = Math.Min((rectangle.Location.X + rectangle.Size.Width) * Scale.Horizontal, 4096);
var height = Math.Min((rectangle.Location.Y + rectangle.Size.Height) * Scale.Vertical, 4096);
Bitmap = new SKBitmap(new SKImageInfo(width.RoundToInt(), height.RoundToInt()));
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private int RoundToInt(double number)
{
return (int) Math.Round(number, MidpointRounding.AwayFromZero);
}
/// <inheritdoc />
public virtual void PerformFinalize()
{
}
public void Dispose()
{
Bitmap?.Dispose();
}
#endregion
}
}
@@ -22,7 +22,7 @@ namespace Artemis.Core.RGB.NET
var height = Math.Min(leds.Max(l => l.AbsoluteLedRectangle.Location.Y + l.AbsoluteLedRectangle.Size.Height) * scale, 4096);
Bitmap = new SKBitmap(new SKImageInfo(width.RoundToInt(), height.RoundToInt()));
}
public SKBitmap Bitmap { get; private set; }
public Color ManipulateColor(Rectangle rectangle, BrushRenderTarget renderTarget, Color color)
+8 -5
View File
@@ -89,14 +89,17 @@ namespace Artemis.Core.Services
module.Update(args.DeltaTime);
}
// If there is no ready graphics decorator, skip the frame
lock (_rgbService.GraphicsDecorator)
// If there is no ready bitmap brush, skip the frame
if (_rgbService.BitmapBrush == null)
return;
lock (_rgbService.BitmapBrush)
{
if (_rgbService.GraphicsDecorator?.Bitmap == null)
if (_rgbService.BitmapBrush.Bitmap == null)
return;
// Render all active modules
using (var canvas = new SKCanvas(_rgbService.GraphicsDecorator.Bitmap))
using (var canvas = new SKCanvas(_rgbService.BitmapBrush.Bitmap))
{
canvas.Clear(new SKColor(0, 0, 0));
lock (_modules)
@@ -117,7 +120,7 @@ namespace Artemis.Core.Services
private void SurfaceOnUpdated(UpdatedEventArgs args)
{
OnFrameRendered(new FrameRenderedEventArgs(_rgbService.GraphicsDecorator, _rgbService.Surface));
OnFrameRendered(new FrameRenderedEventArgs(_rgbService.BitmapBrush, _rgbService.Surface));
}
protected virtual void OnFrameRendering(FrameRenderingEventArgs e)
@@ -9,7 +9,7 @@ namespace Artemis.Core.Services.Interfaces
public interface IRgbService : IArtemisService
{
RGBSurface Surface { get; set; }
GraphicsDecorator GraphicsDecorator { get; }
BitmapBrush BitmapBrush { get; }
IReadOnlyCollection<IRGBDevice> LoadedDevices { get; }
void AddDeviceProvider(IRGBDeviceProvider deviceProvider);
@@ -25,6 +25,6 @@ namespace Artemis.Core.Services.Interfaces
/// </summary>
event EventHandler<DeviceEventArgs> DeviceReloaded;
void UpdateGraphicsDecorator();
void UpdateSurfaceLedGroup();
}
}
+22 -18
View File
@@ -21,7 +21,7 @@ namespace Artemis.Core.Services
private readonly PluginSetting<double> _renderScaleSetting;
private readonly PluginSetting<int> _targetFrameRateSetting;
private readonly TimerUpdateTrigger _updateTrigger;
private ListLedGroup _background;
private ListLedGroup _surfaceLedGroup;
internal RgbService(ILogger logger, ISettingsService settingsService)
{
@@ -30,7 +30,6 @@ namespace Artemis.Core.Services
_targetFrameRateSetting = settingsService.GetSetting("Core.TargetFrameRate", 25);
Surface = RGBSurface.Instance;
GraphicsDecorator = new GraphicsDecorator(new ListLedGroup(), 1);
// Let's throw these for now
Surface.Exception += SurfaceOnException;
@@ -44,7 +43,7 @@ namespace Artemis.Core.Services
/// <inheritdoc />
public RGBSurface Surface { get; set; }
public GraphicsDecorator GraphicsDecorator { get; private set; }
public BitmapBrush BitmapBrush { get; private set; }
public IReadOnlyCollection<IRGBDevice> LoadedDevices => _loadedDevices.AsReadOnly();
@@ -57,7 +56,7 @@ namespace Artemis.Core.Services
_logger.Warning("RgbDevice provider {deviceProvider} has no devices", deviceProvider.GetType().Name);
return;
}
foreach (var surfaceDevice in deviceProvider.Devices)
{
if (!_loadedDevices.Contains(surfaceDevice))
@@ -80,7 +79,7 @@ namespace Artemis.Core.Services
private void RenderScaleSettingOnSettingChanged(object sender, EventArgs e)
{
UpdateGraphicsDecorator();
UpdateSurfaceLedGroup();
}
private void TargetFrameRateSettingOnSettingChanged(object sender, EventArgs e)
@@ -99,23 +98,28 @@ namespace Artemis.Core.Services
public event EventHandler<DeviceEventArgs> DeviceLoaded;
public event EventHandler<DeviceEventArgs> DeviceReloaded;
public void UpdateGraphicsDecorator()
public void UpdateSurfaceLedGroup()
{
lock (GraphicsDecorator)
if (_surfaceLedGroup == null)
{
// Clean up the old background if present
if (_background != null)
{
_background.Brush?.RemoveAllDecorators();
_background.Detach();
}
// Apply the application wide brush and decorator
BitmapBrush = new BitmapBrush(new Scale(_renderScaleSetting.Value));
_surfaceLedGroup = new ListLedGroup(Surface.Leds) { Brush = BitmapBrush };
return;
}
lock (_surfaceLedGroup)
{
// Clean up the old background
_surfaceLedGroup.Detach();
// Apply the application wide brush and decorator
_background = new ListLedGroup(Surface.Leds) {Brush = new SolidColorBrush(new Color(255, 255, 255, 255))};
GraphicsDecorator = new GraphicsDecorator(_background, _renderScaleSetting.Value);
_background.Brush.RemoveAllDecorators();
_background.Brush.AddDecorator(GraphicsDecorator);
BitmapBrush.Scale = new Scale(_renderScaleSetting.Value);
_surfaceLedGroup = new ListLedGroup(Surface.Leds) { Brush = BitmapBrush };
}
lock (BitmapBrush)
{
}
}
@@ -93,7 +93,7 @@ namespace Artemis.Core.Services.Storage
}
// Update the RGB service's graphics decorator to work with the new surface entity
_rgbService.UpdateGraphicsDecorator();
_rgbService.UpdateSurfaceLedGroup();
OnActiveSurfaceConfigurationChanged(new SurfaceConfigurationEventArgs(ActiveSurface));
}
@@ -111,7 +111,7 @@ namespace Artemis.Core.Services.Storage
}
_surfaceRepository.Save(surface.SurfaceEntity);
_rgbService.UpdateGraphicsDecorator();
_rgbService.UpdateSurfaceLedGroup();
OnSurfaceConfigurationUpdated(new SurfaceConfigurationEventArgs(surface));
}