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%
master 1.0.2
Zaafar Ahmed 7 years ago committed by GitHub
parent 94af78513c
commit 4cfe1f594d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 88
      ClickableTransparentOverlay/Overlay.cs

@ -32,8 +32,6 @@ namespace ClickableTransparentOverlay
private static bool isVisible; private static bool isVisible;
private static bool isClosed; private static bool isClosed;
private static bool requireResize; private static bool requireResize;
private static bool startResizing;
private static object resizeLock;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="Overlay"/> class. /// Initializes a new instance of the <see cref="Overlay"/> class.
@ -62,23 +60,19 @@ namespace ClickableTransparentOverlay
// Stuff related to (thread safe) resizing of SDL2Window // Stuff related to (thread safe) resizing of SDL2Window
requireResize = false; requireResize = false;
startResizing = false;
resizeLock = new object();
futureSize = Vector2.Zero; futureSize = Vector2.Zero;
futurePos = 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); 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)); NativeMethods.EnableTransparent(window.Handle, new System.Drawing.Rectangle(window.X, window.Y, window.Width, window.Height));
window.Resized += () => window.Resized += () =>
{ {
graphicsDevice.MainSwapchain.Resize((uint)window.Width, (uint)window.Height); graphicsDevice.MainSwapchain.Resize((uint)window.Width, (uint)window.Height);
imController.WindowResized(window.Width, window.Height); imController.WindowResized(window.Width, window.Height);
lock (resizeLock) futureSize = Vector2.Zero;
{ futurePos = Vector2.Zero;
requireResize = false; requireResize = false;
startResizing = false;
}
}; };
window.Closed += () => window.Closed += () =>
{ {
@ -107,31 +101,6 @@ namespace ClickableTransparentOverlay
Application.Run(new ApplicationContext()); Application.Run(new ApplicationContext());
} }
/// <summary>
/// Free all resources acquired by the overlay
/// </summary>
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();
}
/// <summary> /// <summary>
/// Resizes the overlay /// Resizes the overlay
/// </summary> /// </summary>
@ -182,23 +151,48 @@ namespace ClickableTransparentOverlay
isVisible = false; isVisible = false;
} }
/// <summary>
/// Free all resources acquired by the overlay
/// </summary>
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();
}
/// <summary>
/// Infinite While Loop to render the ImGui.
/// </summary>
private void WhileLoop() private void WhileLoop()
{ {
while (window.Exists) 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) continue;
{
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;
}
} }
if (!window.Exists) if (!window.Exists)

Loading…
Cancel
Save