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

Added a debugger, ironed out rendering pipeline

This commit is contained in:
Robert Beekman 2019-10-24 23:53:38 +02:00
parent 4add877156
commit 93cd704c6c
15 changed files with 321 additions and 35 deletions

View File

@ -144,6 +144,7 @@
<Compile Include="Constants.cs" />
<Compile Include="Events\DeviceConfigurationEventArgs.cs" />
<Compile Include="Events\DeviceEventArgs.cs" />
<Compile Include="Events\FrameEventArgs.cs" />
<Compile Include="Exceptions\ArtemisCoreException.cs" />
<Compile Include="Extensions\DirectoryInfoExtensions.cs" />
<Compile Include="Extensions\RgbColorExtensions.cs" />

View File

@ -5,10 +5,6 @@ namespace Artemis.Core.Events
{
public class DeviceEventArgs : EventArgs
{
public DeviceEventArgs()
{
}
public DeviceEventArgs(IRGBDevice device)
{
Device = device;

View File

@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using Artemis.Core.Plugins.Abstract;
using RGB.NET.Core;
namespace Artemis.Core.Events
{
public class FrameEventArgs : EventArgs
{
public FrameEventArgs(List<Module> modules, Bitmap bitmap, double deltaTime, RGBSurface rgbSurface)
{
Modules = modules;
Bitmap = bitmap;
DeltaTime = deltaTime;
RgbSurface = rgbSurface;
}
public List<Module> Modules { get; }
public Bitmap Bitmap { get; }
public double DeltaTime { get; }
public RGBSurface RgbSurface { get; }
}
}

View File

@ -2,27 +2,24 @@
using System.Drawing;
using System.Linq;
using RGB.NET.Core;
using RGB.NET.Groups;
using Color = RGB.NET.Core.Color;
using Rectangle = RGB.NET.Core.Rectangle;
namespace Artemis.Core.RGB.NET
{
public class GraphicsDecorator : AbstractDecorator, IBrushDecorator
public class GraphicsDecorator : AbstractDecorator, IBrushDecorator, IDisposable
{
private readonly DirectBitmap _bitmap;
private DirectBitmap _bitmap;
public GraphicsDecorator(ListLedGroup ledGroup)
public GraphicsDecorator(ILedGroup ledGroup)
{
var leds = ledGroup.GetLeds().ToList();
if (!leds.Any())
{
_bitmap = null;
}
else
{
var width = leds.Max(l => l.AbsoluteLedRectangle.X + l.AbsoluteLedRectangle.Width);
var height = leds.Max(l => l.AbsoluteLedRectangle.Y + l.AbsoluteLedRectangle.Height);
var width = Math.Min(leds.Max(l => l.AbsoluteLedRectangle.X + l.AbsoluteLedRectangle.Width), 2000);
var height = Math.Min(leds.Max(l => l.AbsoluteLedRectangle.Y + l.AbsoluteLedRectangle.Height), 2000);
_bitmap = new DirectBitmap((int) width, (int) height);
}
@ -40,9 +37,25 @@ namespace Artemis.Core.RGB.NET
return new Color(0, 0, 0);
}
public override void OnDetached(IDecoratable decoratable)
{
Dispose();
}
public void Dispose()
{
_bitmap?.Dispose();
_bitmap = null;
}
public Graphics GetGraphics()
{
return _bitmap == null ? null : Graphics.FromImage(_bitmap.Bitmap);
}
public Bitmap GetBitmap()
{
return _bitmap?.Bitmap;
}
}
}

View File

@ -1,7 +1,8 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Artemis.Core.Events;
using Artemis.Core.Exceptions;
using Artemis.Core.Models.Surface;
using Artemis.Core.Plugins.Abstract;
using Artemis.Core.Services.Interfaces;
using Artemis.Core.Services.Storage;
@ -46,11 +47,17 @@ namespace Artemis.Core.Services
throw new ArtemisCoreException("Cannot initialize the core as it is already initialized.");
_logger.Information("Initializing Artemis Core version {version}", typeof(CoreService).Assembly.GetName().Version);
// Initialize the services
await Task.Run(() => _pluginService.CopyBuiltInPlugins());
await Task.Run(() => _pluginService.LoadPlugins());
var surfaceConfig = _surfaceService.ActiveSurfaceConfiguration;
if (surfaceConfig != null)
_logger.Information("Initialized with active surface configuration {surfaceConfig}-{guid}", surfaceConfig.Name, surfaceConfig.Guid);
else
_logger.Information("Initialized without an active surface configuration");
OnInitialized();
}
@ -79,6 +86,8 @@ namespace Artemis.Core.Services
foreach (var module in modules)
module.Render(args.DeltaTime, _rgbService.Surface, g);
}
OnFrameRendered(new FrameEventArgs(modules, _rgbService.GraphicsDecorator.GetBitmap(), args.DeltaTime, _rgbService.Surface));
}
catch (Exception e)
{
@ -89,6 +98,7 @@ namespace Artemis.Core.Services
#region Events
public event EventHandler Initialized;
public event EventHandler<FrameEventArgs> FrameRendered;
private void OnInitialized()
{
@ -97,5 +107,10 @@ namespace Artemis.Core.Services
}
#endregion
protected virtual void OnFrameRendered(FrameEventArgs e)
{
FrameRendered?.Invoke(this, e);
}
}
}

View File

@ -1,4 +1,5 @@
using System;
using Artemis.Core.Events;
namespace Artemis.Core.Services.Interfaces
{
@ -13,5 +14,10 @@ namespace Artemis.Core.Services.Interfaces
/// Occurs the core has finished initializing
/// </summary>
event EventHandler Initialized;
/// <summary>
/// Occurs whenever a frame has finished rendering
/// </summary>
event EventHandler<FrameEventArgs> FrameRendered;
}
}

View File

@ -30,7 +30,7 @@ namespace Artemis.Core.Services
Surface.Exception += SurfaceOnException;
_loadedDevices = new List<IRGBDevice>();
_updateTrigger = new TimerUpdateTrigger {UpdateFrequency = 1.0 / 30};
_updateTrigger = new TimerUpdateTrigger {UpdateFrequency = 1.0 / 25};
Surface.RegisterUpdateTrigger(_updateTrigger);
}
@ -97,10 +97,12 @@ namespace Artemis.Core.Services
public void UpdateGraphicsDecorator()
{
// TODO: Create new one first, then clean up the old one for a smoother transition
// Clean up the old background if present
if (_background != null)
{
_background.RemoveAllDecorators();
_background.Brush?.RemoveAllDecorators();
_background.Detach();
}

View File

@ -140,8 +140,7 @@ namespace Artemis.Core.Services.Storage
MatchDeviceConfiguration(e.Device, surfaceConfiguration);
}
foreach (var deviceConfiguration in ActiveSurfaceConfiguration.DeviceConfigurations)
deviceConfiguration.ApplyToDevice();
UpdateSurfaceConfiguration(ActiveSurfaceConfiguration, true);
}
#endregion
@ -169,7 +168,7 @@ namespace Artemis.Core.Services.Storage
// When all surface configs are loaded, apply the active surface config
var active = SurfaceConfigurations.FirstOrDefault(c => c.IsActive);
if (active != null)
ActiveSurfaceConfiguration = active;
SetActiveSurfaceConfiguration(active);
}
#endregion

View File

@ -3,32 +3,30 @@ using System.Drawing;
using Artemis.Core.Extensions;
using Artemis.Core.Plugins.Abstract;
using Artemis.Core.Plugins.Models;
using Artemis.Core.Services.Interfaces;
using Artemis.Plugins.Modules.General.ViewModels;
using RGB.NET.Core;
using Stylet;
using Color = System.Drawing.Color;
using Rectangle = System.Drawing.Rectangle;
namespace Artemis.Plugins.Modules.General
{
public class GeneralModule : Module
{
private readonly PluginSettings _settings;
private readonly RGBSurface _surface;
private Color _color;
public GeneralModule(PluginInfo pluginInfo, IRgbService rgbService, PluginSettings settings) : base(pluginInfo)
public GeneralModule(PluginInfo pluginInfo, PluginSettings settings) : base(pluginInfo)
{
_settings = settings;
DisplayName = "General";
ExpandsMainDataModel = true;
_surface = rgbService.Surface;
var testSetting = _settings.GetSetting("TestSetting", DateTime.Now);
_color = ColorHelpers.GetRandomRainbowColor();
Colors = new Color[1000];
}
public Color[] Colors { get; set; }
public override void EnablePlugin()
{
}
@ -39,16 +37,59 @@ namespace Artemis.Plugins.Modules.General
public override void Update(double deltaTime)
{
_color = ColorHelpers.ShiftColor(_color, (int) (deltaTime * 200));
for (var i = 0; i < Colors.Length; i++)
{
var color = Colors[i];
Colors[i] = ColorHelpers.ShiftColor(color, (int) (deltaTime * 200));
}
}
public override void Render(double deltaTime, RGBSurface surface, Graphics graphics)
{
// Lets do this in the least performant way possible
foreach (var surfaceLed in _surface.Leds)
// Per-device coloring, slower
// RenderPerDevice(surface, graphics);
// Per-LED coloring, slowest
RenderPerLed(surface, graphics);
}
public void RenderFullSurface(RGBSurface surface, Graphics graphics)
{
}
public void RenderPerDevice(RGBSurface surface, Graphics graphics)
{
var index = 0;
foreach (var device in surface.Devices)
{
var rectangle = surfaceLed.AbsoluteLedRectangle.ToDrawingRectangle();
graphics.FillRectangle(new SolidBrush(_color), rectangle);
var color = Colors[index];
if (color.A == 0)
{
color = ColorHelpers.GetRandomRainbowColor();
Colors[index] = color;
}
var rectangle = new Rectangle((int) device.Location.X, (int) device.Location.Y, (int) device.Size.Width, (int) device.Size.Height);
graphics.FillRectangle(new SolidBrush(color), rectangle);
index++;
}
}
public void RenderPerLed(RGBSurface surface, Graphics graphics)
{
var index = 0;
foreach (var led in surface.Leds)
{
var color = Colors[index];
if (color.A == 0)
{
color = ColorHelpers.GetRandomRainbowColor();
Colors[index] = color;
}
var rectangle = led.AbsoluteLedRectangle.ToDrawingRectangle();
graphics.FillRectangle(new SolidBrush(color), rectangle);
index++;
}
}

View File

@ -165,6 +165,7 @@
<Compile Include="ViewModels\Controls\SurfaceEditor\SurfaceLedViewModel.cs" />
<Compile Include="ViewModels\Controls\SurfaceEditor\SurfaceDeviceViewModel.cs" />
<Compile Include="ViewModels\PanZoomViewModel.cs" />
<Compile Include="ViewModels\Screens\DebugViewModel.cs" />
<Compile Include="ViewModels\Screens\SurfaceEditorViewModel.cs" />
<Compile Include="ViewModels\Interfaces\IEditorViewModel.cs" />
<Compile Include="ViewModels\Controls\Settings\RgbDeviceSettingsViewModel.cs" />
@ -186,6 +187,10 @@
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</Page>
<Page Include="Views\Screens\DebugView.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="Views\Screens\HomeView.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>

View File

@ -51,7 +51,9 @@ namespace Artemis.UI.ViewModels.Controls.SurfaceEditor
public IReadOnlyCollection<SurfaceLedViewModel> Leds => _leds.AsReadOnly();
public Rect DeviceRectangle => new Rect(X, Y, DeviceConfiguration.Device.Size.Width, DeviceConfiguration.Device.Size.Height);
public Rect DeviceRectangle => DeviceConfiguration.Device == null
? new Rect()
: new Rect(X, Y, DeviceConfiguration.Device.Size.Width, DeviceConfiguration.Device.Size.Height);
public void StartMouseDrag(Point mouseStartPosition)
{

View File

@ -0,0 +1,68 @@
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Artemis.Core.Events;
using Artemis.Core.Services.Interfaces;
using Stylet;
namespace Artemis.UI.ViewModels.Screens
{
public class DebugViewModel : Screen
{
private readonly ICoreService _coreService;
private readonly IRgbService _rgbService;
public DebugViewModel(ICoreService coreService, IRgbService rgbService)
{
_coreService = coreService;
_rgbService = rgbService;
_coreService.FrameRendered += CoreServiceOnFrameRendered;
}
public ImageSource CurrentFrame { get; set; }
public double CurrentFps { get; set; }
public void ForceGarbageCollection()
{
GC.Collect();
GC.WaitForPendingFinalizers();
}
private void CoreServiceOnFrameRendered(object sender, FrameEventArgs e)
{
var imageSource = ImageSourceFromBitmap(e.Bitmap);
imageSource.Freeze();
CurrentFps = Math.Round(1.0 / e.DeltaTime, 2);
Execute.OnUIThread(() => CurrentFrame = imageSource);
}
protected override void OnClose()
{
_coreService.FrameRendered -= CoreServiceOnFrameRendered;
base.OnClose();
}
// This is much quicker than saving the bitmap into a memory stream and converting it
private static ImageSource ImageSourceFromBitmap(Bitmap bmp)
{
var handle = bmp.GetHbitmap();
try
{
return Imaging.CreateBitmapSourceFromHBitmap(handle, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
}
finally
{
DeleteObject(handle);
}
}
[DllImport("gdi32.dll", EntryPoint = "DeleteObject")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool DeleteObject([In] IntPtr hObject);
}
}

View File

@ -2,14 +2,20 @@
using Artemis.Core.Services.Interfaces;
using Artemis.UI.ViewModels.Controls.Settings;
using Artemis.UI.ViewModels.Interfaces;
using Ninject;
using Stylet;
namespace Artemis.UI.ViewModels.Screens
{
public class SettingsViewModel : Screen, ISettingsViewModel
{
public SettingsViewModel(IRgbService rgbService)
private readonly IKernel _kernel;
private readonly IWindowManager _windowManager;
public SettingsViewModel(IKernel kernel, IRgbService rgbService, IWindowManager windowManager)
{
_kernel = kernel;
_windowManager = windowManager;
DeviceSettingsViewModels = new BindableCollection<RgbDeviceSettingsViewModel>();
foreach (var device in rgbService.LoadedDevices)
DeviceSettingsViewModels.Add(new RgbDeviceSettingsViewModel(device));
@ -20,6 +26,11 @@ namespace Artemis.UI.ViewModels.Screens
public BindableCollection<RgbDeviceSettingsViewModel> DeviceSettingsViewModels { get; set; }
public string Title => "Settings";
public void ShowDebugger()
{
_windowManager.ShowWindow(_kernel.Get<DebugViewModel>());
}
private void UpdateDevices(object sender, DeviceEventArgs deviceEventArgs)
{
DeviceSettingsViewModels.Add(new RgbDeviceSettingsViewModel(deviceEventArgs.Device));

View File

@ -0,0 +1,98 @@
<metro:MetroWindow x:Class="Artemis.UI.Views.Screens.DebugView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:metro="http://metro.mahapps.com/winfx/xaml/controls"
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
xmlns:s="https://github.com/canton7/Stylet"
xmlns:abstract="clr-namespace:Artemis.Core.Plugins.Abstract;assembly=Artemis.Core"
xmlns:screens="clr-namespace:Artemis.UI.ViewModels.Screens"
mc:Ignorable="d"
GlowBrush="{DynamicResource AccentColorBrush}"
FontFamily="{StaticResource DefaultFont}"
Title="Artemis debugger"
Width="800"
Height="800"
d:DesignHeight="639.411"
d:DesignWidth="1113.251"
d:DataContext="{d:DesignInstance screens:DebugViewModel}"
Icon="/Artemis.UI;component/Resources/logo-512.png">
<metro:MetroWindow.Resources>
<DrawingImage x:Key="BowIcon">
<DrawingImage.Drawing>
<DrawingGroup>
<GeometryDrawing Brush="{DynamicResource IdealForegroundColorBrush}"
Geometry="M1518 3378 c-48 -63 -61 -101 -66 -184 -4 -70 -1 -91 27
-170 l31 -89 -27 -20 c-32 -24 -849 -601 -981 -693 l-93 -64 -87 40
c-48 22 -91 37 -95 32 -5 -4 9 -41 29 -83 l37 -75 -28 -24 c-23 -20
-29 -35 -33 -81 l-4 -56 -82 -19 c-109 -25 -109 -41 4 -91 l85 -38 7
-64 c15 -137 90 -1279 85 -1293 -3 -7 -35 -24 -70 -35 -159 -53 -257
-168 -257 -302 0 -35 2 -38 47 -53 54 -18 185 -21 232 -5 29 10 31
14 31 58 0 26 6 56 14 66 13 18 15 18 46 -8 44 -37 78 -35 119 7 l34
35 -17 41 c-9 23 -12 39 -6 35 6 -4 43 -1 83 6 39 6 219 14 398 18
l327 6 113 57 c158 78 256 166 317 282 24 46 27 62 27 152 0 98 -1
103 -41 184 l-42 83 44 69 c24 37 51 68 59 68 9 0 44 -14 78 -32 l62
-31 -93 -44 c-58 -26 -92 -48 -90 -55 9 -27 353 -68 570 -68 108 0
108 0 108 24 0 34 -105 171 -220 286 -122 122 -238 216 -250 204 -6
-6 -1 -42 16 -98 14 -49 23 -91 19 -94 -3 -3 -36 9 -73 27 l-69 33 24
71 c13 39 23 76 23 82 0 6 28 17 63 24 279 58 399 300 314 632 -32
121 -49 155 -134 255 -37 45 -106 126 -152 180 -73 87 -241 326 -241
343 0 3 15 13 32 21 21 10 35 25 40 45 15 60 -16 103 -81 108 -43 3
-39 22 14 74 l45 43 -25 50 c-35 69 -77 114 -130 139 -63 30 -88 27
-117 -11z m215 -835 c188 -279 250 -417 250 -548 0 -133 -74 -214 -243
-265 l-55 -16 -37 -138 c-21 -76 -39 -140 -40 -141 -6 -5 -814 377 -823
390 -6 7 -19 46 -29 86 -10 41 -25 81 -33 91 -8 9 -57 35 -109 59 -52
23 -93 46 -92 51 2 4 233 169 513 366 l510 358 26 -46 c15 -25 88 -136
162 -247z m-1108 -898 c61 21 88 26 107 19 14 -5 204 -92 421 -194 l395
-185 -27 -35 c-15 -19 -53 -72 -84 -117 l-57 -81 30 -90 c39 -117 40
-179 2 -253 -45 -90 -147 -145 -347 -189 -71 -15 -435 -59 -600 -73 l
-29 -2 -37 540 c-20 297 -40 581 -43 632 l-7 92 98 -46 97 -46 81 28z" />
</DrawingGroup>
</DrawingImage.Drawing>
</DrawingImage>
</metro:MetroWindow.Resources>
<metro:MetroWindow.IconTemplate>
<DataTemplate>
<Grid Width="{TemplateBinding Width}"
Height="{TemplateBinding Height}"
Background="Transparent"
RenderOptions.BitmapScalingMode="HighQuality"
Margin="0,0,-10,0">
<Image Source="{DynamicResource BowIcon}"
Stretch="Uniform"
Margin="6" />
</Grid>
</DataTemplate>
</metro:MetroWindow.IconTemplate>
<StackPanel Margin="10, 10, 10, 10">
<TextBlock>
In this window you can view the inner workings of Artemis.
Please not that having this window open can have a performance impact on your system.
</TextBlock>
<Grid Margin="0,10,0,0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0">
This image shows what is being rendered and dispatched to RGB.NET
</TextBlock>
<TextBlock Grid.Column="1" HorizontalAlignment="Right" Margin="0,0,5,0" FontWeight="Bold">
FPS:
</TextBlock>
<TextBlock Grid.Column="2" HorizontalAlignment="Right" Text="{Binding CurrentFps}"/>
</Grid>
<materialDesign:Card VerticalAlignment="Stretch" Margin="0, 5,0,0">
<Image Source="{Binding CurrentFrame}" />
</materialDesign:Card>
<Button Command="{s:Action ForceGarbageCollection}" Style="{StaticResource MaterialDesignRaisedButton}" HorizontalAlignment="Left" Margin="0, 10, 0, 0">
Force garbage collection
</Button>
</StackPanel>
</metro:MetroWindow>

View File

@ -1,6 +1,7 @@
<UserControl x:Class="Artemis.UI.Views.Screens.SettingsView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:s="https://github.com/canton7/Stylet"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:screens="clr-namespace:Artemis.UI.ViewModels.Screens"
@ -18,9 +19,13 @@
</UserControl.Resources>
<StackPanel Margin="16">
<TextBlock Style="{StaticResource MaterialDesignHeadlineTextBlock}">General</TextBlock>
<Grid>
<StackPanel>
<TextBlock>General settings like start up with Windows etc.</TextBlock>
</Grid>
<Button Command="{s:Action ShowDebugger}" Style="{StaticResource MaterialDesignRaisedButton}" HorizontalAlignment="Left" Margin="0, 10, 0, 0">
Show debugger
</Button>
</StackPanel>
<TextBlock Style="{StaticResource MaterialDesignHeadlineTextBlock}">Devices</TextBlock>