From 53647875998bcd3d87e8fd3c7d056da8972ba261 Mon Sep 17 00:00:00 2001 From: Flamefire Date: Thu, 25 Sep 2014 16:37:48 +0200 Subject: [PATCH] Resize enqueued bmp update in a task --- Vocaluxe/Lib/Draw/CDrawBase.cs | 69 ++++++++++++++++++++-------------- 1 file changed, 41 insertions(+), 28 deletions(-) diff --git a/Vocaluxe/Lib/Draw/CDrawBase.cs b/Vocaluxe/Lib/Draw/CDrawBase.cs index ca05c62c..3a8eeed2 100644 --- a/Vocaluxe/Lib/Draw/CDrawBase.cs +++ b/Vocaluxe/Lib/Draw/CDrawBase.cs @@ -669,29 +669,55 @@ namespace Vocaluxe.Lib.Draw } } - public CTextureRef EnqueueTexture(Bitmap bmp) + /// + /// Enqueues a bitmap to add or update the texture + /// + /// TTextureType (Add) or CTextureRef(Update) + /// + /// + /// True if resizing should be done in an extra thread + private void _EnqueueTextureAddOrUpdate(Object texture, Bitmap bmp, EQueueAction action, bool asyncResize) { - TTextureType texture = _CreateTexture(new Size(-1, -1)); - CTextureRef textureRef = _GetTextureReference(bmp.GetSize(), texture); + Debug.Assert(action == EQueueAction.Add || action == EQueueAction.Update); + Debug.Assert(action != EQueueAction.Add || texture is TTextureType); + Debug.Assert(action != EQueueAction.Update || texture is CTextureRef); if (_RequiresResize(bmp.GetSize())) { - Task.Factory.StartNew(() => - { - Bitmap bmp2 = bmp.Resize(_GetNewTextureSize(bmp.Size)); - bmp.Dispose(); - lock (_TextureQueue) - { - _TextureQueue.Enqueue(new STextureQueue(texture, EQueueAction.Add, bmp2)); - } - }); + if (asyncResize) + Task.Factory.StartNew(() => _ResizeTextureAndEnqueue(texture, bmp, action)); + else + _ResizeTextureAndEnqueue(texture, bmp, action); } else { lock (_TextureQueue) { - _TextureQueue.Enqueue(new STextureQueue(texture, EQueueAction.Add, bmp)); + _TextureQueue.Enqueue(new STextureQueue(texture, action, bmp)); } } + } + + /// + /// Helper function to resize a bitmap and enqueue it + /// + /// + /// + /// + private void _ResizeTextureAndEnqueue(object texture, Bitmap bmp, EQueueAction action) + { + Bitmap bmp2 = bmp.Resize(_GetNewTextureSize(bmp.Size)); + bmp.Dispose(); + lock (_TextureQueue) + { + _TextureQueue.Enqueue(new STextureQueue(texture, action, bmp2)); + } + } + + public CTextureRef EnqueueTexture(Bitmap bmp) + { + TTextureType texture = _CreateTexture(new Size(-1, -1)); + CTextureRef textureRef = _GetTextureReference(bmp.GetSize(), texture); + _EnqueueTextureAddOrUpdate(texture, bmp, EQueueAction.Add, true); return textureRef; } @@ -783,26 +809,13 @@ namespace Vocaluxe.Lib.Draw _BitmapsLoading.Remove(filePath); } } - Size size = _GetNewTextureSize(origSize); - if (!size.Equals(origSize)) - { - Bitmap bmp2 = bmp.Resize(size); - bmp.Dispose(); - bmp = bmp2; - } - lock (_TextureQueue) - { - _TextureQueue.Enqueue(new STextureQueue(texture, EQueueAction.Add, bmp)); - } + _EnqueueTextureAddOrUpdate(texture, bmp, EQueueAction.Add, false); return origSize; } public void EnqueueTextureUpdate(CTextureRef textureRef, Bitmap bmp) { - lock (_TextureQueue) - { - _TextureQueue.Enqueue(new STextureQueue(textureRef, EQueueAction.Update, bmp)); - } + _EnqueueTextureAddOrUpdate(textureRef, bmp, EQueueAction.Update, true); } ///