Merge "[Multimedia.Util] Modified async methods of ImageDecoder and ImageEncoder." 5.0.0-preview1-00436
authorcoderhyme <jhyo.kim@samsung.com>
Tue, 2 Jan 2018 22:36:14 +0000 (22:36 +0000)
committerGerrit Code Review <gerrit@review.ap-northeast-2.compute.internal>
Tue, 2 Jan 2018 22:36:14 +0000 (22:36 +0000)
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)