GifImageAnimator.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics.CodeAnalysis;
  4. using System.Linq;
  5. using System.Threading;
  6. using HandyControl.Controls;
  7. using HandyControl.Data;
  8. namespace HandyControl.Tools;
  9. internal class ImageAnimator
  10. {
  11. private static List<GifImageInfo> ImageInfoList;
  12. private static readonly ReaderWriterLock RwImgListLock = new();
  13. private static Thread AnimationThread;
  14. private static bool AnyFrameDirty;
  15. [ThreadStatic]
  16. private static int ThreadWriterLockWaitCount;
  17. public static bool CanAnimate(GifImage image)
  18. {
  19. if (image == null)
  20. {
  21. return false;
  22. }
  23. lock (image)
  24. {
  25. var dimensions = image.FrameDimensionsList;
  26. if (dimensions.Select(guid => new GifFrameDimension(guid)).Contains(GifFrameDimension.Time))
  27. {
  28. return image.GetFrameCount(GifFrameDimension.Time) > 1;
  29. }
  30. }
  31. return false;
  32. }
  33. public static void Animate(GifImage image, EventHandler onFrameChangedHandler)
  34. {
  35. if (image == null)
  36. {
  37. return;
  38. }
  39. GifImageInfo imageInfo;
  40. // See comment in the class header about locking the image ref.
  41. lock (image)
  42. {
  43. // could we avoid creating an ImageInfo object if FrameCount == 1 ?
  44. imageInfo = new GifImageInfo(image);
  45. }
  46. // If the image is already animating, stop animating it
  47. StopAnimate(image, onFrameChangedHandler);
  48. // Acquire a writer lock to modify the image info list. If the thread has a reader lock we need to upgrade
  49. // it to a writer lock; acquiring a reader lock in this case would block the thread on itself.
  50. // If the thread already has a writer lock its ref count will be incremented w/o placing the request in the
  51. // writer queue. See ReaderWriterLock.AcquireWriterLock method in the MSDN.
  52. var readerLockHeld = RwImgListLock.IsReaderLockHeld;
  53. var lockDowngradeCookie = new LockCookie();
  54. ThreadWriterLockWaitCount++;
  55. try
  56. {
  57. if (readerLockHeld)
  58. {
  59. lockDowngradeCookie = RwImgListLock.UpgradeToWriterLock(Timeout.Infinite);
  60. }
  61. else
  62. {
  63. RwImgListLock.AcquireWriterLock(Timeout.Infinite);
  64. }
  65. }
  66. finally
  67. {
  68. ThreadWriterLockWaitCount--;
  69. }
  70. try
  71. {
  72. if (imageInfo.Animated)
  73. {
  74. // Construct the image array
  75. //
  76. ImageInfoList ??= new List<GifImageInfo>();
  77. // Add the new image
  78. //
  79. imageInfo.FrameChangedHandler = onFrameChangedHandler;
  80. ImageInfoList.Add(imageInfo);
  81. // Construct a new timer thread if we haven't already
  82. //
  83. if (AnimationThread == null)
  84. {
  85. AnimationThread = new Thread(AnimateImages50Ms)
  86. {
  87. Name = nameof(ImageAnimator),
  88. IsBackground = true
  89. };
  90. AnimationThread.Start();
  91. }
  92. }
  93. }
  94. finally
  95. {
  96. if (readerLockHeld)
  97. {
  98. RwImgListLock.DowngradeFromWriterLock(ref lockDowngradeCookie);
  99. }
  100. else
  101. {
  102. RwImgListLock.ReleaseWriterLock();
  103. }
  104. }
  105. }
  106. public static void StopAnimate(GifImage image, EventHandler onFrameChangedHandler)
  107. {
  108. // Make sure we have a list of images
  109. if (image == null || ImageInfoList == null)
  110. {
  111. return;
  112. }
  113. // Acquire a writer lock to modify the image info list - See comments on Animate() about this locking.
  114. var readerLockHeld = RwImgListLock.IsReaderLockHeld;
  115. var lockDowngradeCookie = new LockCookie();
  116. ThreadWriterLockWaitCount++;
  117. try
  118. {
  119. if (readerLockHeld)
  120. {
  121. lockDowngradeCookie = RwImgListLock.UpgradeToWriterLock(Timeout.Infinite);
  122. }
  123. else
  124. {
  125. RwImgListLock.AcquireWriterLock(Timeout.Infinite);
  126. }
  127. }
  128. finally
  129. {
  130. ThreadWriterLockWaitCount--;
  131. }
  132. try
  133. {
  134. // Find the corresponding reference and remove it
  135. for (var i = 0; i < ImageInfoList.Count; i++)
  136. {
  137. var imageInfo = ImageInfoList[i];
  138. if (Equals(image, imageInfo.Image))
  139. {
  140. if (onFrameChangedHandler == imageInfo.FrameChangedHandler || onFrameChangedHandler != null && onFrameChangedHandler.Equals(imageInfo.FrameChangedHandler))
  141. {
  142. ImageInfoList.Remove(imageInfo);
  143. }
  144. break;
  145. }
  146. }
  147. if (!ImageInfoList.Any())
  148. {
  149. AnimationThread?.Join();
  150. AnimationThread = null;
  151. }
  152. }
  153. finally
  154. {
  155. if (readerLockHeld)
  156. {
  157. RwImgListLock.DowngradeFromWriterLock(ref lockDowngradeCookie);
  158. }
  159. else
  160. {
  161. RwImgListLock.ReleaseWriterLock();
  162. }
  163. }
  164. }
  165. public static void UpdateFrames()
  166. {
  167. if (!AnyFrameDirty || ImageInfoList == null)
  168. {
  169. return;
  170. }
  171. if (ThreadWriterLockWaitCount > 0)
  172. {
  173. // Cannot acquire reader lock at this time, frames update will be missed.
  174. return;
  175. }
  176. RwImgListLock.AcquireReaderLock(Timeout.Infinite);
  177. try
  178. {
  179. foreach (var imageInfo in ImageInfoList)
  180. {
  181. // See comment in the class header about locking the image ref.
  182. lock (imageInfo.Image)
  183. {
  184. imageInfo.UpdateFrame();
  185. }
  186. }
  187. AnyFrameDirty = false;
  188. }
  189. finally
  190. {
  191. RwImgListLock.ReleaseReaderLock();
  192. }
  193. }
  194. [SuppressMessage("Microsoft.Performance", "CA1804:RemoveUnusedLocals")]
  195. private static void AnimateImages50Ms()
  196. {
  197. while (ImageInfoList.Any())
  198. {
  199. // Acquire reader-lock to access imageInfoList, elemens in the list can be modified w/o needing a writer-lock.
  200. // Observe that we don't need to check if the thread is waiting or a writer lock here since the thread this
  201. // method runs in never acquires a writer lock.
  202. RwImgListLock.AcquireReaderLock(Timeout.Infinite);
  203. try
  204. {
  205. foreach (var imageInfo in ImageInfoList)
  206. {
  207. // Frame delay is measured in 1/100ths of a second. This thread
  208. // sleeps for 50 ms = 5/100ths of a second between frame updates,
  209. // so we increase the frame delay count 5/100ths of a second
  210. // at a time.
  211. //
  212. imageInfo.FrameTimer += 5;
  213. if (imageInfo.FrameTimer >= imageInfo.FrameDelay(imageInfo.Frame))
  214. {
  215. imageInfo.FrameTimer = 0;
  216. if (imageInfo.Frame + 1 < imageInfo.FrameCount)
  217. {
  218. imageInfo.Frame++;
  219. }
  220. else
  221. {
  222. imageInfo.Frame = 0;
  223. }
  224. if (imageInfo.FrameDirty)
  225. {
  226. AnyFrameDirty = true;
  227. }
  228. }
  229. }
  230. }
  231. finally
  232. {
  233. RwImgListLock.ReleaseReaderLock();
  234. }
  235. Thread.Sleep(50);
  236. }
  237. }
  238. }