Skip to content

Commit

Permalink
Reduce cpu usage
Browse files Browse the repository at this point in the history
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%
  • Loading branch information
zaafar authored Jan 26, 2019
1 parent 94af785 commit 4cfe1f5
Showing 1 changed file with 41 additions and 47 deletions.
88 changes: 41 additions & 47 deletions ClickableTransparentOverlay/Overlay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ public class Overlay
private static bool isVisible;
private static bool isClosed;
private static bool requireResize;
private static bool startResizing;
private static object resizeLock;

/// <summary>
/// Initializes a new instance of the <see cref="Overlay"/> class.
Expand Down Expand Up @@ -62,23 +60,19 @@ public Overlay(int x, int y, int width, int height, int fps)

// 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 += () =>
{
Expand Down Expand Up @@ -107,31 +101,6 @@ public void Run()
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>
/// Resizes the overlay
/// </summary>
Expand Down Expand Up @@ -182,23 +151,48 @@ public void Hide()
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()
{
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)
Expand Down

0 comments on commit 4cfe1f5

Please sign in to comment.