Fix thread synchronization problem with Sheet.dirty

This commit is contained in:
Pavlos Touboulidis
2014-05-15 02:23:03 +03:00
parent 266240a3c1
commit 441d602a6d

View File

@@ -21,6 +21,7 @@ namespace OpenRA.Graphics
ITexture texture; ITexture texture;
bool dirty; bool dirty;
byte[] data; byte[] data;
readonly object dirtyLock = new object();
public readonly Size Size; public readonly Size Size;
public byte[] Data { get { return data ?? texture.GetData(); } } public byte[] Data { get { return data ?? texture.GetData(); } }
@@ -68,6 +69,8 @@ namespace OpenRA.Graphics
public ITexture Texture public ITexture Texture
{ {
// This is only called from the main thread but 'dirty'
// is set from other threads too via CommitData().
get get
{ {
if (texture == null) if (texture == null)
@@ -77,10 +80,13 @@ namespace OpenRA.Graphics
} }
if (dirty) if (dirty)
{
lock (dirtyLock)
{ {
texture.SetData(data, Size.Width, Size.Height); texture.SetData(data, Size.Width, Size.Height);
dirty = false; dirty = false;
} }
}
return texture; return texture;
} }
@@ -140,7 +146,10 @@ namespace OpenRA.Graphics
if (data == null) if (data == null)
throw new InvalidOperationException("Texture-wrappers are read-only"); throw new InvalidOperationException("Texture-wrappers are read-only");
lock (dirtyLock)
{
dirty = true; dirty = true;
} }
} }
}
} }