Dispose of graphics resources deterministically.
Textures, FrameBuffers and VertexBuffers allocated by the Sdl2 Renderer were only being released via finalizers. This could lead to OpenGL out of memory errors since resources may not be cleaned up in a timely manner. To avoid this, IDisposable has been implemented and transitively applied to classes that use these resources. As a side-effect some static state is no longer static, particularly in Renderer, in order to facilitate this change and just for nicer design in general. Also dispose some bitmaps.
This commit is contained in:
committed by
RoosterDragon
parent
38b579a081
commit
f0f02dff5c
@@ -9,7 +9,9 @@
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Drawing;
|
||||
using System.Linq;
|
||||
|
||||
namespace OpenRA.Graphics
|
||||
{
|
||||
@@ -27,8 +29,9 @@ namespace OpenRA.Graphics
|
||||
BGRA = 4,
|
||||
}
|
||||
|
||||
public class SheetBuilder
|
||||
public sealed class SheetBuilder : IDisposable
|
||||
{
|
||||
readonly List<Sheet> sheets = new List<Sheet>();
|
||||
Sheet current;
|
||||
TextureChannel channel;
|
||||
SheetType type;
|
||||
@@ -36,19 +39,23 @@ namespace OpenRA.Graphics
|
||||
Point p;
|
||||
Func<Sheet> allocateSheet;
|
||||
|
||||
public static Sheet AllocateSheet()
|
||||
public static Sheet AllocateSheet(int sheetSize)
|
||||
{
|
||||
return new Sheet(new Size(Renderer.SheetSize, Renderer.SheetSize));
|
||||
return new Sheet(new Size(sheetSize, sheetSize));
|
||||
}
|
||||
|
||||
public SheetBuilder(SheetType t)
|
||||
: this(t, AllocateSheet) { }
|
||||
: this(t, Game.Renderer.SheetSize) { }
|
||||
|
||||
public SheetBuilder(SheetType t, int sheetSize)
|
||||
: this(t, () => AllocateSheet(sheetSize)) { }
|
||||
|
||||
public SheetBuilder(SheetType t, Func<Sheet> allocateSheet)
|
||||
{
|
||||
channel = TextureChannel.Red;
|
||||
type = t;
|
||||
current = allocateSheet();
|
||||
sheets.Add(current);
|
||||
this.allocateSheet = allocateSheet;
|
||||
}
|
||||
|
||||
@@ -111,6 +118,7 @@ namespace OpenRA.Graphics
|
||||
{
|
||||
current.ReleaseBuffer();
|
||||
current = allocateSheet();
|
||||
sheets.Add(current);
|
||||
channel = TextureChannel.Red;
|
||||
}
|
||||
else
|
||||
@@ -127,5 +135,12 @@ namespace OpenRA.Graphics
|
||||
}
|
||||
|
||||
public Sheet Current { get { return current; } }
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
foreach (var sheet in sheets)
|
||||
sheet.Dispose();
|
||||
sheets.Clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user