Always access the SheetBuilder from the main thread in MapCache.
Not doing this can occasionally this can result in a cross thread call to the backing texture which leads to bad things. This stops trying to be clever regarding the thread safety invariants exposed by SheetBuilder and does things simply and safely: All updates happen on the main thread.
This commit is contained in:
@@ -11,7 +11,6 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Drawing.Imaging;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
@@ -176,34 +175,31 @@ namespace OpenRA
|
||||
bitmap = Minimap.RenderMapPreview(modData.DefaultRules.TileSets[p.Map.Tileset], p.Map, modData.DefaultRules, true);
|
||||
}
|
||||
|
||||
// Note: this is not generally thread-safe, but it works here because:
|
||||
// (a) This worker is the only thread writing to this sheet
|
||||
// (b) The main thread is the only thread reading this sheet
|
||||
// (c) The sheet is marked dirty after the write is completed,
|
||||
// which causes the main thread to copy this to the texture during
|
||||
// the next render cycle.
|
||||
// (d) Any partially written bytes from the next minimap is in an
|
||||
// unallocated area, and will be committed in the next cycle.
|
||||
try
|
||||
Game.RunAfterTick(() =>
|
||||
{
|
||||
p.SetMinimap(sheetBuilder.Add(bitmap));
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (createdPreview)
|
||||
bitmap.Dispose();
|
||||
}
|
||||
try
|
||||
{
|
||||
p.SetMinimap(sheetBuilder.Add(bitmap));
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (createdPreview)
|
||||
bitmap.Dispose();
|
||||
}
|
||||
});
|
||||
|
||||
// Yuck... But this helps the UI Jank when opening the map selector significantly.
|
||||
Thread.Sleep(Environment.ProcessorCount == 1 ? 25 : 5);
|
||||
}
|
||||
}
|
||||
|
||||
sheetBuilder.Current.ReleaseBuffer();
|
||||
|
||||
// The buffer is not fully reclaimed until changes are written out to the texture.
|
||||
// We will access the texture in order to force changes to be written out, allowing the buffer to be freed.
|
||||
Game.RunAfterTick(() => sheetBuilder.Current.GetTexture());
|
||||
Game.RunAfterTick(() =>
|
||||
{
|
||||
sheetBuilder.Current.ReleaseBuffer();
|
||||
sheetBuilder.Current.GetTexture();
|
||||
});
|
||||
Log.Write("debug", "MapCache.LoadAsyncInternal ended");
|
||||
}
|
||||
|
||||
@@ -224,7 +220,6 @@ namespace OpenRA
|
||||
if (previewLoaderThread != null)
|
||||
previewLoaderThread.Join();
|
||||
|
||||
sheetBuilder.Current.CreateBuffer();
|
||||
previewLoaderThread = new Thread(LoadAsyncInternal)
|
||||
{
|
||||
Name = "Map Preview Loader",
|
||||
|
||||
Reference in New Issue
Block a user