Make sure we actually add the data to a queued texture

This commit is contained in:
Flamefire committed 2014-09-24 15:52:38 +02:00
1 parent 466c443a20
commit 5f139467ea
1 file changed
+28 -18
+28 -18
View File
@@ -31,7 +31,7 @@ namespace Vocaluxe.Lib.Draw
private struct STextureQueue private struct STextureQueue
{ {
public readonly WeakReference TextureRef; public readonly object TextureOrRef;
/// <summary> /// <summary>
/// Only valid if Data is byte[] /// Only valid if Data is byte[]
/// </summary> /// </summary>
@@ -42,12 +42,12 @@ namespace Vocaluxe.Lib.Draw
/// <summary> /// <summary>
/// Creates a new Queue entry /// Creates a new Queue entry
/// </summary> /// </summary>
/// <param name="textureRef"></param> /// <param name="textureOrRef">TTextureType or CTextureRef</param>
/// <param name="action"></param> /// <param name="action"></param>
/// <param name="data">Texture data as a bitmap</param> /// <param name="data">Texture data as a bitmap</param>
public STextureQueue(CTextureRef textureRef, EQueueAction action, Bitmap data) public STextureQueue(object textureOrRef, EQueueAction action, Bitmap data)
{ {
TextureRef = new WeakReference(textureRef); TextureOrRef = textureOrRef;
Action = action; Action = action;
Data = data; Data = data;
DataSize = new Size(); DataSize = new Size();
@@ -56,13 +56,13 @@ namespace Vocaluxe.Lib.Draw
/// <summary> /// <summary>
/// Creates a new Queue entry /// Creates a new Queue entry
/// </summary> /// </summary>
/// <param name="textureRef"></param> /// <param name="textureOrRef">TTextureType or CTextureRef</param>
/// <param name="action"></param> /// <param name="action"></param>
/// <param name="dataSize">Size of data</param> /// <param name="dataSize">Size of data</param>
/// <param name="data">Texture data</param> /// <param name="data">Texture data</param>
public STextureQueue(CTextureRef textureRef, EQueueAction action, Size dataSize, byte[] data) public STextureQueue(object textureOrRef, EQueueAction action, Size dataSize, byte[] data)
{ {
TextureRef = new WeakReference(textureRef); TextureOrRef = textureOrRef;
Action = action; Action = action;
DataSize = dataSize; DataSize = dataSize;
Data = data; Data = data;
@@ -499,12 +499,14 @@ namespace Vocaluxe.Lib.Draw
while (_TextureQueue.Count > 0) while (_TextureQueue.Count > 0)
{ {
STextureQueue q = _TextureQueue.Dequeue(); STextureQueue q = _TextureQueue.Dequeue();
CTextureRef textureRef = q.TextureRef.Target as CTextureRef;
if (!q.TextureRef.IsAlive || textureRef == null || !_Textures.ContainsKey(textureRef.ID))
continue;
if (q.Action == EQueueAction.Add) if (q.Action == EQueueAction.Add)
{ {
TTextureType oldTexture = q.TextureOrRef as TTextureType;
Debug.Assert(oldTexture != null, "Queued type is wrong");
Debug.Assert(!oldTexture.IsLoaded);
if (oldTexture.RefCount <= 0)
continue;
TTextureType texture = null; TTextureType texture = null;
// ReSharper disable CanBeReplacedWithTryCastAndCheckForNull // ReSharper disable CanBeReplacedWithTryCastAndCheckForNull
if (q.Data is Bitmap) if (q.Data is Bitmap)
@@ -514,10 +516,15 @@ namespace Vocaluxe.Lib.Draw
else else
throw new ArgumentException("q.Data is of invalid type"); throw new ArgumentException("q.Data is of invalid type");
// ReSharper restore CanBeReplacedWithTryCastAndCheckForNull // ReSharper restore CanBeReplacedWithTryCastAndCheckForNull
TTextureType oldTexture = _Textures[textureRef.ID];
_MergeTextures(oldTexture, texture); _MergeTextures(oldTexture, texture);
} }
else if (q.Action == EQueueAction.Update) else
{
CTextureRef textureRef = q.TextureOrRef as CTextureRef;
Debug.Assert(textureRef != null, "Queued type is wrong");
if (!_Textures.ContainsKey(textureRef.ID))
continue;
if (q.Action == EQueueAction.Update)
{ {
// ReSharper disable CanBeReplacedWithTryCastAndCheckForNull // ReSharper disable CanBeReplacedWithTryCastAndCheckForNull
if (q.Data is Bitmap) if (q.Data is Bitmap)
@@ -533,6 +540,7 @@ namespace Vocaluxe.Lib.Draw
} }
} }
} }
}
/// <summary> /// <summary>
/// Replaces one oldTexture by newTexture summing the refCounts. Also disposes oldTexture /// Replaces one oldTexture by newTexture summing the refCounts. Also disposes oldTexture
@@ -660,7 +668,8 @@ namespace Vocaluxe.Lib.Draw
public CTextureRef EnqueueTexture(Bitmap bmp) public CTextureRef EnqueueTexture(Bitmap bmp)
{ {
CTextureRef textureRef = _GetTextureReference(bmp.GetSize(), _CreateTexture(new Size(-1, -1))); TTextureType texture = _CreateTexture(new Size(-1, -1));
CTextureRef textureRef = _GetTextureReference(bmp.GetSize(), texture);
if (_RequiresResize(bmp.GetSize())) if (_RequiresResize(bmp.GetSize()))
{ {
Task.Factory.StartNew(() => Task.Factory.StartNew(() =>
@@ -669,7 +678,7 @@ namespace Vocaluxe.Lib.Draw
bmp.Dispose(); bmp.Dispose();
lock (_TextureQueue) lock (_TextureQueue)
{ {
_TextureQueue.Enqueue(new STextureQueue(textureRef, EQueueAction.Add, bmp2)); _TextureQueue.Enqueue(new STextureQueue(texture, EQueueAction.Add, bmp2));
} }
}); });
} }
@@ -677,7 +686,7 @@ namespace Vocaluxe.Lib.Draw
{ {
lock (_TextureQueue) lock (_TextureQueue)
{ {
_TextureQueue.Enqueue(new STextureQueue(textureRef, EQueueAction.Add, bmp)); _TextureQueue.Enqueue(new STextureQueue(texture, EQueueAction.Add, bmp));
} }
} }
@@ -709,7 +718,7 @@ namespace Vocaluxe.Lib.Draw
textureRef = _GetTextureReference(invalidSize, texture); textureRef = _GetTextureReference(invalidSize, texture);
// Add the loader to the dictionary in the same lock as the add cache call // Add the loader to the dictionary in the same lock as the add cache call
// Avoids that we get a cache entry in AddTexture but no Task // Avoids that we get a cache entry in AddTexture but no Task
loader = new Task<Size>(() => _LoadAndEnqueueBitmap(filePath, textureRef)); loader = new Task<Size>(() => _LoadAndEnqueueBitmap(filePath, textureRef, texture));
lock (_BitmapsLoading) lock (_BitmapsLoading)
{ {
_BitmapsLoading.Add(filePath, loader); _BitmapsLoading.Add(filePath, loader);
@@ -737,8 +746,9 @@ namespace Vocaluxe.Lib.Draw
/// </summary> /// </summary>
/// <param name="filePath"></param> /// <param name="filePath"></param>
/// <param name="textureRef"></param> /// <param name="textureRef"></param>
/// <param name="texture"></param>
/// <returns></returns> /// <returns></returns>
private Size _LoadAndEnqueueBitmap(string filePath, CTextureRef textureRef) private Size _LoadAndEnqueueBitmap(string filePath, CTextureRef textureRef, TTextureType texture)
{ {
Bitmap bmp = CHelper.LoadBitmap(filePath); Bitmap bmp = CHelper.LoadBitmap(filePath);
if (bmp == null) if (bmp == null)
@@ -779,7 +789,7 @@ namespace Vocaluxe.Lib.Draw
} }
lock (_TextureQueue) lock (_TextureQueue)
{ {
_TextureQueue.Enqueue(new STextureQueue(textureRef, EQueueAction.Add, bmp)); _TextureQueue.Enqueue(new STextureQueue(texture, EQueueAction.Add, bmp));
} }
return origSize; return origSize;
} }