From 5677cdf498c62cd5b898e7092ae7253c972dfd74 Mon Sep 17 00:00:00 2001 From: Robert Date: Fri, 15 Nov 2019 16:01:49 +0100 Subject: [PATCH] Fixed gitingore --- .gitignore | 1 + .../Screens/Settings/Debug/DebugView.xaml | 98 +++++++++++++++++++ .../Screens/Settings/Debug/DebugViewModel.cs | 83 ++++++++++++++++ 3 files changed, 182 insertions(+) create mode 100644 src/Artemis.UI/Screens/Settings/Debug/DebugView.xaml create mode 100644 src/Artemis.UI/Screens/Settings/Debug/DebugViewModel.cs diff --git a/.gitignore b/.gitignore index e56f74231..bbd7f92b0 100644 --- a/.gitignore +++ b/.gitignore @@ -194,3 +194,4 @@ FakesAssemblies/ *.opendb src/Artemis\.Storage/Storage\.db +!src/Artemis.UI/screens/Settings/Debug \ No newline at end of file diff --git a/src/Artemis.UI/Screens/Settings/Debug/DebugView.xaml b/src/Artemis.UI/Screens/Settings/Debug/DebugView.xaml new file mode 100644 index 000000000..64843f8ef --- /dev/null +++ b/src/Artemis.UI/Screens/Settings/Debug/DebugView.xaml @@ -0,0 +1,98 @@ + + + + + + + + + + + + + + + + + + + + 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. + + + + + + + + + + This image shows what is being rendered and dispatched to RGB.NET + + + FPS: + + + + + + + + + + + + \ No newline at end of file diff --git a/src/Artemis.UI/Screens/Settings/Debug/DebugViewModel.cs b/src/Artemis.UI/Screens/Settings/Debug/DebugViewModel.cs new file mode 100644 index 000000000..4c9f1f57f --- /dev/null +++ b/src/Artemis.UI/Screens/Settings/Debug/DebugViewModel.cs @@ -0,0 +1,83 @@ +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.Screens.Settings.Debug +{ + public class DebugViewModel : Screen, IScreenViewModel + { + private readonly ICoreService _coreService; + private readonly IRgbService _rgbService; + + public DebugViewModel(ICoreService coreService, IRgbService rgbService) + { + _coreService = coreService; + _rgbService = rgbService; + } + + public ImageSource CurrentFrame { get; set; } + public double CurrentFps { get; set; } + + public string Title => "Debugger"; + + public void ForceGarbageCollection() + { + GC.Collect(); + GC.WaitForPendingFinalizers(); + } + + private void CoreServiceOnFrameRendered(object sender, FrameRenderedEventArgs e) + { + if (e.Bitmap == null) + return; + + var imageSource = ImageSourceFromBitmap(e.Bitmap); + imageSource.Freeze(); + Execute.OnUIThread(() => { CurrentFrame = imageSource; }); + } + + private void CoreServiceOnFrameRendering(object sender, FrameRenderingEventArgs e) + { + CurrentFps = Math.Round(1.0 / e.DeltaTime, 2); + } + + protected override void OnActivate() + { + _coreService.FrameRendered += CoreServiceOnFrameRendered; + _coreService.FrameRendering += CoreServiceOnFrameRendering; + base.OnActivate(); + } + + protected override void OnDeactivate() + { + _coreService.FrameRendered -= CoreServiceOnFrameRendered; + _coreService.FrameRendering -= CoreServiceOnFrameRendering; + base.OnDeactivate(); + } + + // 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); + } +} \ No newline at end of file