thread safe drawlayermanager

pull/5/head
Evan Hemsley 2019-07-28 20:25:52 -07:00
parent 0abbd47da3
commit df6bd0e394
1 changed files with 29 additions and 20 deletions

View File

@ -1,4 +1,5 @@
using System; using System;
using System.Collections.Concurrent;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
@ -6,32 +7,37 @@ namespace Encompass
{ {
internal class DrawLayerManager internal class DrawLayerManager
{ {
private readonly object layerOrderLock = new object();
private readonly SortedList<int, int> layerOrder = new SortedList<int, int>(); private readonly SortedList<int, int> layerOrder = new SortedList<int, int>();
private readonly Dictionary<int, HashSet<Guid>> layerIndexToComponentIDs = new Dictionary<int, HashSet<Guid>>(); private readonly ConcurrentDictionary<int, ConcurrentDictionary<Guid, byte>> layerIndexToComponentIDs = new ConcurrentDictionary<int, ConcurrentDictionary<Guid, byte>>();
private readonly Dictionary<int, HashSet<GeneralRenderer>> layerIndexToGeneralRenderers = new Dictionary<int, HashSet<GeneralRenderer>>(); private readonly ConcurrentDictionary<int, ConcurrentDictionary<GeneralRenderer, byte>> layerIndexToGeneralRenderers = new ConcurrentDictionary<int, ConcurrentDictionary<GeneralRenderer, byte>>();
private readonly Dictionary<Guid, int> componentIDToLayerIndex = new Dictionary<Guid, int>(); private readonly ConcurrentDictionary<Guid, int> componentIDToLayerIndex = new ConcurrentDictionary<Guid, int>();
public IEnumerable<int> LayerOrder { get { return layerOrder.Values; } } public IEnumerable<int> LayerOrder { get { lock(layerOrderLock) { return layerOrder.Values; } } }
public void RegisterGeneralRendererWithLayer(GeneralRenderer renderer, int layer) public void RegisterGeneralRendererWithLayer(GeneralRenderer renderer, int layer)
{ {
if (layerIndexToGeneralRenderers.ContainsKey(layer)) if (layerIndexToGeneralRenderers.ContainsKey(layer))
{ {
var set = layerIndexToGeneralRenderers[layer]; var set = layerIndexToGeneralRenderers[layer];
set.Add(renderer); set[renderer] = 0;
} }
else else
{ {
var set = new HashSet<GeneralRenderer>(); var set = new ConcurrentDictionary<GeneralRenderer, byte>();
layerIndexToGeneralRenderers.Add(layer, set); layerIndexToGeneralRenderers[layer] = set;
set.Add(renderer); set[renderer] = 0;
} }
if (!layerOrder.ContainsKey(layer)) lock (layerOrderLock)
{ {
layerOrder.Add(layer, layer); if (!layerOrder.ContainsKey(layer))
{
layerOrder.Add(layer, layer);
}
} }
} }
@ -39,7 +45,7 @@ namespace Encompass
{ {
if (layerIndexToGeneralRenderers.ContainsKey(layer)) if (layerIndexToGeneralRenderers.ContainsKey(layer))
{ {
layerIndexToGeneralRenderers[layer].Remove(renderer); layerIndexToGeneralRenderers[layer].TryRemove(renderer, out _);
} }
} }
@ -54,20 +60,23 @@ namespace Encompass
if (layerIndexToComponentIDs.ContainsKey(layer)) if (layerIndexToComponentIDs.ContainsKey(layer))
{ {
var set = layerIndexToComponentIDs[layer]; var set = layerIndexToComponentIDs[layer];
set.Add(id); set[id] = 0;
} }
else else
{ {
var set = new HashSet<Guid>(); var set = new ConcurrentDictionary<Guid, byte>();
layerIndexToComponentIDs.Add(layer, set); layerIndexToComponentIDs[layer] = set;
set.Add(id); set[id] = 0;
} }
componentIDToLayerIndex[id] = layer; componentIDToLayerIndex[id] = layer;
if (!layerOrder.ContainsKey(layer)) lock (layerOrderLock)
{ {
layerOrder.Add(layer, layer); if (!layerOrder.ContainsKey(layer))
{
layerOrder.Add(layer, layer);
}
} }
} }
@ -76,7 +85,7 @@ namespace Encompass
if (componentIDToLayerIndex.ContainsKey(id)) if (componentIDToLayerIndex.ContainsKey(id))
{ {
var layer = componentIDToLayerIndex[id]; var layer = componentIDToLayerIndex[id];
layerIndexToComponentIDs[layer].Remove(id); layerIndexToComponentIDs[layer].TryRemove(id, out _);
} }
} }
@ -89,14 +98,14 @@ namespace Encompass
public IEnumerable<Guid> ComponentIDsByLayer(int layer) public IEnumerable<Guid> ComponentIDsByLayer(int layer)
{ {
return layerIndexToComponentIDs.ContainsKey(layer) ? return layerIndexToComponentIDs.ContainsKey(layer) ?
layerIndexToComponentIDs[layer] : layerIndexToComponentIDs[layer].Keys :
Enumerable.Empty<Guid>(); Enumerable.Empty<Guid>();
} }
public IEnumerable<GeneralRenderer> GeneralRenderersByLayer(int layer) public IEnumerable<GeneralRenderer> GeneralRenderersByLayer(int layer)
{ {
return layerIndexToGeneralRenderers.ContainsKey(layer) ? return layerIndexToGeneralRenderers.ContainsKey(layer) ?
layerIndexToGeneralRenderers[layer] : layerIndexToGeneralRenderers[layer].Keys :
Enumerable.Empty<GeneralRenderer>(); Enumerable.Empty<GeneralRenderer>();
} }
} }