Avoid a globally shared SheetBuilder in SpriteFont.

A globally shared sheet builder leaks memory and resources between mod switches. Instead, we create and inject the sheet builder during mod startup to ensure we still share the builder across all fonts, but can reclaim it when the mod is unloaded.
This commit is contained in:
RoosterDragon
2015-01-14 22:33:58 +00:00
parent 398af8e513
commit c313c52e96
3 changed files with 24 additions and 17 deletions

View File

@@ -37,6 +37,8 @@ namespace OpenRA.Graphics
readonly Queue<IVertexBuffer<Vertex>> tempBuffers = new Queue<IVertexBuffer<Vertex>>();
readonly Stack<Rectangle> scissorState = new Stack<Rectangle>();
SheetBuilder fontSheetBuilder;
Size? lastResolution;
int2? lastScroll;
float? lastZoom;
@@ -94,8 +96,13 @@ namespace OpenRA.Graphics
public void InitializeFonts(Manifest m)
{
using (new Support.PerfTimer("SpriteFonts"))
{
if (fontSheetBuilder != null)
fontSheetBuilder.Dispose();
fontSheetBuilder = new SheetBuilder(SheetType.BGRA);
Fonts = m.Fonts.ToDictionary(x => x.Key,
x => new SpriteFont(Platform.ResolvePath(x.Value.First), x.Value.Second)).AsReadOnly();
x => new SpriteFont(Platform.ResolvePath(x.Value.First), x.Value.Second, fontSheetBuilder)).AsReadOnly();
}
}
public void BeginFrame(int2 scroll, float zoom)
@@ -246,6 +253,8 @@ namespace OpenRA.Graphics
foreach (var buffer in tempBuffers)
buffer.Dispose();
tempBuffers.Clear();
if (fontSheetBuilder != null)
fontSheetBuilder.Dispose();
}
}
}