From 4cfe1f594d923e9e4c4bf16467ea95c839a8aa0a Mon Sep 17 00:00:00 2001 From: Zaafar Ahmed Date: Sat, 26 Jan 2019 13:10:53 -0500 Subject: [PATCH] Reduce cpu usage This PR reduces the CPU usage by setting Veldrid SDL2Window threading to false. We can do this because setting the threading to true only benefits in handling the Keyboard/Mouse events. Our keyboard/mouse events are already handled by the global mouse hook in a threaded way. Observation: CPU usage is reduced from 16% - 20% to 1% - 3% --- ClickableTransparentOverlay/Overlay.cs | 88 ++++++++++++-------------- 1 file changed, 41 insertions(+), 47 deletions(-) diff --git a/ClickableTransparentOverlay/Overlay.cs b/ClickableTransparentOverlay/Overlay.cs index d58fdfc..c009e7b 100644 --- a/ClickableTransparentOverlay/Overlay.cs +++ b/ClickableTransparentOverlay/Overlay.cs @@ -32,8 +32,6 @@ namespace ClickableTransparentOverlay private static bool isVisible; private static bool isClosed; private static bool requireResize; - private static bool startResizing; - private static object resizeLock; /// /// Initializes a new instance of the class. @@ -62,23 +60,19 @@ namespace ClickableTransparentOverlay // Stuff related to (thread safe) resizing of SDL2Window requireResize = false; - startResizing = false; - resizeLock = new object(); futureSize = Vector2.Zero; futurePos = Vector2.Zero; - window = new Sdl2Window("Overlay", x, x, width, height, SDL_WindowFlags.Borderless | SDL_WindowFlags.AlwaysOnTop | SDL_WindowFlags.SkipTaskbar, true); + window = new Sdl2Window("Overlay", x, y, width, height, SDL_WindowFlags.Borderless | SDL_WindowFlags.AlwaysOnTop | SDL_WindowFlags.SkipTaskbar, false); graphicsDevice = VeldridStartup.CreateGraphicsDevice(window, new GraphicsDeviceOptions(true, null, true), GraphicsBackend.Direct3D11); NativeMethods.EnableTransparent(window.Handle, new System.Drawing.Rectangle(window.X, window.Y, window.Width, window.Height)); window.Resized += () => { graphicsDevice.MainSwapchain.Resize((uint)window.Width, (uint)window.Height); imController.WindowResized(window.Width, window.Height); - lock (resizeLock) - { - requireResize = false; - startResizing = false; - } + futureSize = Vector2.Zero; + futurePos = Vector2.Zero; + requireResize = false; }; window.Closed += () => { @@ -107,31 +101,6 @@ namespace ClickableTransparentOverlay Application.Run(new ApplicationContext()); } - /// - /// Free all resources acquired by the overlay - /// - public void Dispose() - { - isVisible = false; - window.Close(); - while (!isClosed) - { - Thread.Sleep(10); - } - - uiThread.Join(); - graphicsDevice.WaitForIdle(); - imController.Dispose(); - commandList.Dispose(); - graphicsDevice.Dispose(); - hookController.Dispose(); - NativeMethods.ShowConsoleWindow(); - resizeLock = null; - this.SubmitUI = null; - Console.WriteLine("All Overlay resources are cleared."); - Application.Exit(); - } - /// /// Resizes the overlay /// @@ -182,23 +151,48 @@ namespace ClickableTransparentOverlay isVisible = false; } + /// + /// Free all resources acquired by the overlay + /// + public void Dispose() + { + isVisible = false; + window.Close(); + while (!isClosed) + { + Thread.Sleep(10); + } + + uiThread.Join(); + graphicsDevice.WaitForIdle(); + imController.Dispose(); + commandList.Dispose(); + graphicsDevice.Dispose(); + hookController.Dispose(); + NativeMethods.ShowConsoleWindow(); + this.SubmitUI = null; + Console.WriteLine("All Overlay resources are cleared."); + Application.Exit(); + } + + /// + /// Infinite While Loop to render the ImGui. + /// private void WhileLoop() { while (window.Exists) { - lock (resizeLock) + if (requireResize) + { + Sdl2Native.SDL_SetWindowPosition(window.SdlWindowHandle, (int)futurePos.X, (int)futurePos.Y); + Sdl2Native.SDL_SetWindowSize(window.SdlWindowHandle, (int)futureSize.X, (int)futureSize.Y); + window.PumpEvents(); + continue; + } + + if (!window.Visible) { - if (requireResize) - { - if (!startResizing) - { - Sdl2Native.SDL_SetWindowPosition(window.SdlWindowHandle, (int)futurePos.X, (int)futurePos.Y); - Sdl2Native.SDL_SetWindowSize(window.SdlWindowHandle, (int)futureSize.X, (int)futureSize.Y); - startResizing = true; - } - - continue; - } + continue; } if (!window.Exists)