Add thread-safety with locks to window manager and rendering code

This commit is contained in:
Stone_Red
2026-06-08 20:18:52 +02:00
parent fd1db1b904
commit 82efb8d967
4 changed files with 222 additions and 156 deletions
+107 -100
View File
@@ -17,130 +17,137 @@ public sealed class CanvasRenderSource : IRenderSource
private static Point lastPointerPosition = new Point(-1, -1);
private static List<int> orderedWindowsCache = new();
private static bool isZOrderDirty = true;
private static readonly object renderLock = new object();
public void Render(IEnumerable<RenderCommand> commands)
{
bool changed = false;
foreach (RenderCommand command in commands)
lock (renderLock)
{
changed = true;
if (command.ElementType == "WindowClose")
bool changed = false;
foreach (RenderCommand command in commands)
{
windowCanvases.Remove(command.WindowId);
windowPositions.Remove(command.WindowId);
windowZIndices.Remove(command.WindowId);
isZOrderDirty = true;
continue;
}
if (command.ElementType == "Window" || command.ElementType == "WindowMove")
{
if (command.Properties.TryGetValue("ZIndex", out object? rawZIndex) && rawZIndex is int z)
changed = true;
if (command.ElementType == "WindowClose")
{
windowZIndices[command.WindowId] = z;
windowCanvases.Remove(command.WindowId);
windowPositions.Remove(command.WindowId);
windowZIndices.Remove(command.WindowId);
isZOrderDirty = true;
continue;
}
}
if (command.ElementType == "Window")
{
Size size = command.Properties.TryGetValue("Size", out object? rawSize) && rawSize is Size windowSize
? windowSize
: new Size(160, 120);
if (!windowCanvases.TryGetValue(command.WindowId, out Canvas? currentCanvas) ||
currentCanvas.Mode.Width != size.Width ||
currentCanvas.Mode.Height != size.Height)
if (command.ElementType == "Window" || command.ElementType == "WindowMove")
{
windowCanvases[command.WindowId] = new Canvas(size.Width, size.Height);
if (command.Properties.TryGetValue("ZIndex", out object? rawZIndex) && rawZIndex is int z)
{
windowZIndices[command.WindowId] = z;
isZOrderDirty = true;
}
}
if (command.ElementType == "Window")
{
Size size = command.Properties.TryGetValue("Size", out object? rawSize) && rawSize is Size windowSize
? windowSize
: new Size(160, 120);
if (!windowCanvases.TryGetValue(command.WindowId, out Canvas? currentCanvas) ||
currentCanvas.Mode.Width != size.Width ||
currentCanvas.Mode.Height != size.Height)
{
windowCanvases[command.WindowId] = new Canvas(size.Width, size.Height);
}
}
if (!windowCanvases.ContainsKey(command.WindowId))
{
windowCanvases[command.WindowId] = new Canvas(160, 120);
}
Canvas windowCanvas = windowCanvases[command.WindowId];
if (command.ElementType == "Window")
{
RenderWindow(windowCanvas, command);
windowPositions[command.WindowId] = command.Position;
continue;
}
if (command.ElementType == "WindowMove")
{
windowPositions[command.WindowId] = command.Position;
continue;
}
if (command.ElementType == "Circle")
{
RenderCircle(windowCanvas, command);
}
if (command.ElementType == "Rectangle")
{
RenderRectangle(windowCanvas, command);
}
if (command.ElementType == "Text")
{
RenderText(windowCanvas, command);
}
if (command.ElementType == "Line")
{
RenderLine(windowCanvas, command);
}
if (command.ElementType == "Button")
{
RenderButton(windowCanvas, command);
}
if (command.ElementType == "CheckBox")
{
RenderCheckBox(windowCanvas, command);
}
}
if (!windowCanvases.ContainsKey(command.WindowId))
if (changed)
{
windowCanvases[command.WindowId] = new Canvas(160, 120);
isDirty = true;
}
Canvas windowCanvas = windowCanvases[command.WindowId];
if (command.ElementType == "Window")
{
RenderWindow(windowCanvas, command);
windowPositions[command.WindowId] = command.Position;
continue;
}
if (command.ElementType == "WindowMove")
{
windowPositions[command.WindowId] = command.Position;
continue;
}
if (command.ElementType == "Circle")
{
RenderCircle(windowCanvas, command);
}
if (command.ElementType == "Rectangle")
{
RenderRectangle(windowCanvas, command);
}
if (command.ElementType == "Text")
{
RenderText(windowCanvas, command);
}
if (command.ElementType == "Line")
{
RenderLine(windowCanvas, command);
}
if (command.ElementType == "Button")
{
RenderButton(windowCanvas, command);
}
if (command.ElementType == "CheckBox")
{
RenderCheckBox(windowCanvas, command);
}
}
if (changed)
{
isDirty = true;
}
}
public static void CompositeAndDisplay(Canvas screenCanvas, Point pointerPosition)
{
if (!isDirty && pointerPosition == lastPointerPosition)
lock (renderLock)
{
return;
}
if (isZOrderDirty)
{
orderedWindowsCache = windowPositions.Keys.OrderBy(id => windowZIndices.TryGetValue(id, out int z) ? z : 0).ToList();
isZOrderDirty = false;
}
screenCanvas.Clear(Color.Black);
foreach (var windowId in orderedWindowsCache)
{
if (windowPositions.TryGetValue(windowId, out Point position) && windowCanvases.TryGetValue(windowId, out Canvas? windowCanvas))
if (!isDirty && pointerPosition == lastPointerPosition)
{
screenCanvas.DrawCanvas(windowCanvas, position.X, position.Y);
return;
}
if (isZOrderDirty)
{
orderedWindowsCache = windowPositions.Keys.OrderBy(id => windowZIndices.TryGetValue(id, out int z) ? z : 0).ToList();
isZOrderDirty = false;
}
screenCanvas.Clear(Color.Black);
foreach (var windowId in orderedWindowsCache)
{
if (windowPositions.TryGetValue(windowId, out Point position) && windowCanvases.TryGetValue(windowId, out Canvas? windowCanvas))
{
screenCanvas.DrawCanvas(windowCanvas, position.X, position.Y);
}
}
screenCanvas.DrawFilledCircle(Color.White, pointerPosition.X, pointerPosition.Y, 5);
screenCanvas.Display();
lastPointerPosition = pointerPosition;
isDirty = false;
}
screenCanvas.DrawFilledCircle(Color.White, pointerPosition.X, pointerPosition.Y, 5);
screenCanvas.Display();
lastPointerPosition = pointerPosition;
isDirty = false;
}
private static void RenderWindow(Canvas canvas, RenderCommand command)
+15 -3
View File
@@ -46,6 +46,7 @@ public sealed class Window(string title, int processId, int id, IRenderSource re
public bool IsDragging => currentInteraction == InteractionMode.Drag;
private readonly object uiElementsLock = new object();
private readonly Dictionary<int, UIElement> uiElements = [];
private int nextUIElementId = 1;
@@ -87,7 +88,10 @@ public sealed class Window(string title, int processId, int id, IRenderSource re
Flush();
}
uiElements.Add(uiElementId, uiElement);
lock (uiElementsLock)
{
uiElements.Add(uiElementId, uiElement);
}
return uiElement;
}
@@ -105,8 +109,16 @@ public sealed class Window(string title, int processId, int id, IRenderSource re
return;
}
bool anyChildChanged;
List<UIElement> elementsCopy;
lock (uiElementsLock)
{
anyChildChanged = uiElements.Values.Any(e => e.AnyPropertyChanged);
elementsCopy = uiElements.Values.ToList();
}
bool windowStateChanged = isFirstRender || Size != lastRenderedSize || IsFocused != lastRenderedIsFocused || Title != lastRenderedTitle;
bool anyChildChanged = uiElements.Values.Any(e => e.AnyPropertyChanged);
bool positionChanged = Position != lastRenderedPosition;
bool zIndexChanged = ZIndex != lastRenderedZIndex;
@@ -133,7 +145,7 @@ public sealed class Window(string title, int processId, int id, IRenderSource re
}
});
foreach (UIElement element in uiElements.Values)
foreach (UIElement element in elementsCopy)
{
commands.Add(new RenderCommand
{
+62 -21
View File
@@ -13,8 +13,9 @@ namespace RemSox.UI.GUI.Windows;
public static class WindowManager
{
private static readonly object windowsLock = new object();
// Process ID to list of windows
private static readonly ConcurrentDictionary<int, List<Window>> windows = new();
private static readonly Dictionary<int, List<Window>> windows = new();
private static int nextWindowId = 1;
private static int nextZIndex = 1;
@@ -76,21 +77,27 @@ public static class WindowManager
ZIndex = nextZIndex++
};
if (!windows.ContainsKey(process.Id))
lock (windowsLock)
{
windows[process.Id] = [];
}
if (!windows.ContainsKey(process.Id))
{
windows[process.Id] = [];
}
windows[process.Id].Add(window);
windows[process.Id].Add(window);
}
return window;
}
public static void CloseWindow(Window window)
{
if (windows.TryGetValue(window.ProcessId, out var processWindows))
lock (windowsLock)
{
processWindows.Remove(window);
if (windows.TryGetValue(window.ProcessId, out var processWindows))
{
processWindows.Remove(window);
}
}
renderSource.Render(new[] { new RenderCommand { WindowId = window.Id, ElementId = window.Id, ElementType = "WindowClose", Position = window.Position, Properties = new Dictionary<string, object?>() } });
@@ -98,17 +105,23 @@ public static class WindowManager
public static List<Window> GetWindowsForProcess(Process process)
{
if (windows.TryGetValue(process.Id, out var processWindows))
lock (windowsLock)
{
return processWindows;
}
if (windows.TryGetValue(process.Id, out var processWindows))
{
return processWindows.ToList();
}
return [];
return [];
}
}
public static void CloseWindowsForProcess(Process process)
{
windows.TryRemove(process.Id, out _);
lock (windowsLock)
{
windows.Remove(process.Id);
}
}
public static void FocusWindow(Window? window)
@@ -137,7 +150,12 @@ public static class WindowManager
public static Window? TryBeginInteract(Point pointerPosition)
{
var allWindows = windows.Values.SelectMany(w => w).OrderByDescending(w => w.ZIndex);
List<Window> allWindows;
lock (windowsLock)
{
allWindows = windows.Values.SelectMany(w => w).OrderByDescending(w => w.ZIndex).ToList();
}
foreach (var window in allWindows)
{
if (window.TryBeginInteract(pointerPosition))
@@ -150,12 +168,15 @@ public static class WindowManager
public static void InvalidateAll()
{
foreach (var processWindows in windows.Values)
List<Window> allWindows;
lock (windowsLock)
{
foreach (var window in processWindows)
{
window.Invalidate();
}
allWindows = windows.Values.SelectMany(w => w).ToList();
}
foreach (var window in allWindows)
{
window.Invalidate();
}
}
@@ -166,13 +187,33 @@ public static class WindowManager
sealed private class MuliRenderSource(List<IRenderSource> sources) : IRenderSource
{
public void AddSource(IRenderSource source) => sources.Add(source);
private readonly object sourcesLock = new object();
public void RemoveSource(IRenderSource source) => sources.Remove(source);
public void AddSource(IRenderSource source)
{
lock (sourcesLock)
{
sources.Add(source);
}
}
public void RemoveSource(IRenderSource source)
{
lock (sourcesLock)
{
sources.Remove(source);
}
}
public void Render(IEnumerable<RenderCommand> commands)
{
foreach (IRenderSource source in sources)
List<IRenderSource> sourcesCopy;
lock (sourcesLock)
{
sourcesCopy = sources.ToList();
}
foreach (IRenderSource source in sourcesCopy)
{
source.Render(commands);
}