[Multimedia.Util] Modified async methods of ImageDecoder and ImageEncoder. 51/165251/1
authorcoderhyme <jhyo.kim@samsung.com>
Wed, 27 Dec 2017 08:42:47 +0000 (17:42 +0900)
committercoderhyme <jhyo.kim@samsung.com>
Wed, 27 Dec 2017 08:42:47 +0000 (17:42 +0900)
It turns out no TaskCompletionSource is needed for these methods.
Plus, created tasks with LongRunning option which makes tasks run on separate threads rather than ones of the thread pool of the default scheduler because a long running task can lead to negative effects in performance.

Change-Id: I6a7d2a2d5128ff30f654d6a41789d4f2c9ceb6c1
Signed-off-by: coderhyme <jhyo.kim@samsung.com>
src/Tizen.Multimedia.Util/ImageUtil/ImageDecoder.cs
src/Tizen.Multimedia.Util/ImageUtil/ImageEncoder.cs

index 43b69ad..4e47ed7 100644 (file)
@@ -20,6 +20,7 @@ using System.Diagnostics;
 using System.IO;
 using System.Linq;
 using System.Runtime.InteropServices;
+using System.Threading;
 using System.Threading.Tasks;
 using static Interop;
 using static Interop.Decode;
@@ -201,37 +202,32 @@ namespace Tizen.Multimedia.Util
             }
         }
 
-        internal Task<IEnumerable<BitmapFrame>> DecodeAsync()
+        private IEnumerable<BitmapFrame> RunDecoding()
         {
-            Initialize(Handle);
-
             IntPtr outBuffer = IntPtr.Zero;
-            SetOutputBuffer(Handle, out outBuffer).ThrowIfFailed("Failed to decode given image");
 
-            var tcs = new TaskCompletionSource<IEnumerable<BitmapFrame>>();
-
-            Task.Run(() =>
+            try
             {
-                try
-                {
-                    int width, height;
-                    ulong size;
+                SetOutputBuffer(Handle, out outBuffer).ThrowIfFailed("Failed to decode given image");
 
-                    DecodeRun(Handle, out width, out height, out size).ThrowIfFailed("Failed to decode");
+                DecodeRun(Handle, out var width, out var height, out var size).
+                    ThrowIfFailed("Failed to decode");
 
-                    tcs.SetResult(new[] { new BitmapFrame(outBuffer, width, height, (int)size) });
-                }
-                catch (Exception e)
-                {
-                    tcs.TrySetException(e);
-                }
-                finally
-                {
-                    LibcSupport.Free(outBuffer);
-                }
-            });
+                yield return new BitmapFrame(outBuffer, width, height, (int)size);
+            }
+            finally
+            {
+                LibcSupport.Free(outBuffer);
+            }
+        }
+
+        internal Task<IEnumerable<BitmapFrame>> DecodeAsync()
+        {
+            Initialize(Handle);
 
-            return tcs.Task;
+            return Task.Factory.StartNew(RunDecoding, CancellationToken.None,
+                TaskCreationOptions.DenyChildAttach | TaskCreationOptions.LongRunning,
+                TaskScheduler.Default);
         }
 
         internal virtual void Initialize(ImageDecoderHandle handle)
index 1270739..e98f678 100644 (file)
@@ -20,6 +20,7 @@ using System.Diagnostics;
 using System.IO;
 using System.Linq;
 using System.Runtime.InteropServices;
+using System.Threading;
 using System.Threading.Tasks;
 using static Interop.ImageUtil;
 using Unmanaged = Interop.ImageUtil.Encode;
@@ -114,38 +115,31 @@ namespace Tizen.Multimedia.Util
                 ThrowIfFailed("Failed to set the color space");
         }
 
-        private Task Run(Stream outStream)
+        private void RunEncoding(object outStream)
         {
-            var tcs = new TaskCompletionSource<bool>();
+            IntPtr outBuffer = IntPtr.Zero;
 
-            Task.Run(() =>
+            try
             {
-                IntPtr outBuffer = IntPtr.Zero;
-
-                try
-                {
-                    Unmanaged.SetOutputBuffer(Handle, out outBuffer).ThrowIfFailed("Failed to initialize encoder");
-
-                    ulong size = 0;
-                    Unmanaged.Run(Handle, out size).ThrowIfFailed("Failed to encode given image");
+                Unmanaged.SetOutputBuffer(Handle, out outBuffer).ThrowIfFailed("Failed to initialize encoder");
 
-                    byte[] buf = new byte[size];
-                    Marshal.Copy(outBuffer, buf, 0, (int)size);
-                    outStream.Write(buf, 0, (int)size);
+                Unmanaged.Run(Handle, out var size).ThrowIfFailed("Failed to encode given image");
 
-                    tcs.TrySetResult(true);
-                }
-                catch (Exception e)
-                {
-                    tcs.TrySetException(e);
-                }
-                finally
-                {
-                    Interop.Libc.Free(outBuffer);
-                }
-            });
+                byte[] buf = new byte[size];
+                Marshal.Copy(outBuffer, buf, 0, (int)size);
+                (outStream as Stream).Write(buf, 0, (int)size);
+            }
+            finally
+            {
+                Interop.Libc.Free(outBuffer);
+            }
+        }
 
-            return tcs.Task;
+        private Task Run(Stream outStream)
+        {
+            return Task.Factory.StartNew(RunEncoding, outStream, CancellationToken.None,
+                TaskCreationOptions.DenyChildAttach | TaskCreationOptions.LongRunning,
+                TaskScheduler.Default);
         }
 
         internal Task EncodeAsync(Action<ImageEncoderHandle> settingInputAction, Stream outStream)