CubismTaskableModel.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. /**
  2. * Copyright(c) Live2D Inc. All rights reserved.
  3. *
  4. * Use of this source code is governed by the Live2D Open Software license
  5. * that can be found at https://www.live2d.com/eula/live2d-open-software-license-agreement_en.html.
  6. */
  7. using Live2D.Cubism.Core.Unmanaged;
  8. using System.Threading;
  9. namespace Live2D.Cubism.Core
  10. {
  11. /// <summary>
  12. /// 'Atomic' <see cref="CubismModel"/> update task.
  13. /// </summary>
  14. internal sealed class CubismTaskableModel : ICubismTask
  15. {
  16. #region Factory Methods
  17. /// <summary>
  18. /// Creates a <see cref="CubismTaskableModel"/> from a <see cref="CubismMoc"/>.
  19. /// </summary>
  20. /// <param name="moc">Moc source.</param>
  21. /// <returns>Instance.</returns>
  22. public static CubismTaskableModel CreateTaskableModel(CubismMoc moc)
  23. {
  24. return new CubismTaskableModel(moc);
  25. }
  26. #endregion
  27. /// <summary>
  28. /// Handle to unmanaged model.
  29. /// </summary>
  30. public CubismUnmanagedModel UnmanagedModel { get; private set; }
  31. /// <summary>
  32. /// <see cref="CubismMoc"/> the model was instantiated from.
  33. /// </summary>
  34. public CubismMoc Moc { get; private set; }
  35. private CubismDynamicDrawableData[] _dynamicDrawableData;
  36. /// <summary>
  37. /// Buffer to write dynamic data to.
  38. /// </summary>
  39. public CubismDynamicDrawableData[] DynamicDrawableData
  40. {
  41. get
  42. {
  43. CubismDynamicDrawableData[] dynamicDrawableData = null;
  44. if (Monitor.TryEnter(Lock))
  45. {
  46. dynamicDrawableData = _dynamicDrawableData;
  47. Monitor.Exit(Lock);
  48. }
  49. return dynamicDrawableData;
  50. }
  51. private set
  52. {
  53. _dynamicDrawableData = value;
  54. }
  55. }
  56. /// <summary>
  57. /// True if task is currently executing.
  58. /// </summary>
  59. public bool IsExecuting
  60. {
  61. get
  62. {
  63. var isExecuting = false;
  64. if (Monitor.TryEnter(Lock))
  65. {
  66. isExecuting = (State == TaskState.Enqueued || State == TaskState.Executing);
  67. Monitor.Exit(Lock);
  68. }
  69. return isExecuting;
  70. }
  71. }
  72. /// <summary>
  73. /// True if did run to completion at least once.
  74. /// </summary>
  75. public bool DidExecute
  76. {
  77. get
  78. {
  79. var didExecute = false;
  80. if (Monitor.TryEnter(Lock))
  81. {
  82. didExecute = (State == TaskState.Executed);
  83. Monitor.Exit(Lock);
  84. }
  85. return didExecute;
  86. }
  87. }
  88. /// <summary>
  89. /// True if unmanaged model and moc should be released.
  90. /// </summary>
  91. private bool ShouldReleaseUnmanaged { get; set; }
  92. #region Constructor
  93. /// <summary>
  94. /// Initializes instance.
  95. /// </summary>
  96. /// <param name="moc">Moc unmanaged model was instantiated from.</param>
  97. public CubismTaskableModel(CubismMoc moc)
  98. {
  99. Moc = moc;
  100. // Instantiate unmanaged model.
  101. var unmanagedMoc = moc.AcquireUnmanagedMoc();
  102. UnmanagedModel = CubismUnmanagedModel.FromMoc(unmanagedMoc);
  103. Lock = new object();
  104. State = TaskState.Idle;
  105. DynamicDrawableData = CubismDynamicDrawableData.CreateData(UnmanagedModel);
  106. ShouldReleaseUnmanaged = false;
  107. }
  108. #endregion
  109. /// <summary>
  110. /// Tries to read parameters into a buffer.
  111. /// </summary>
  112. /// <param name="parameters">Buffer to write to.</param>
  113. /// <returns><see langword="true"/> on success; <see langword="false"/> otherwise.</returns>
  114. public bool TryReadParameters(CubismParameter[] parameters)
  115. {
  116. var didRead = false;
  117. if (Monitor.TryEnter(Lock))
  118. {
  119. try
  120. {
  121. if (State == TaskState.Executed)
  122. {
  123. parameters.ReadFrom(UnmanagedModel);
  124. didRead = true;
  125. }
  126. }
  127. finally
  128. {
  129. Monitor.Exit(Lock);
  130. }
  131. }
  132. return didRead;
  133. }
  134. /// <summary>
  135. /// Tries to write parameters to a buffer.
  136. /// </summary>
  137. /// <param name="parameters">Buffer to read from.</param>
  138. /// <param name="parts">Buffer to read from.</param>
  139. /// <returns><see langword="true"/> on success; <see langword="false"/> otherwise.</returns>
  140. public bool TryWriteParametersAndParts(CubismParameter[] parameters, CubismPart[] parts)
  141. {
  142. var didWrite = false;
  143. if (Monitor.TryEnter(Lock))
  144. {
  145. try
  146. {
  147. if (State != TaskState.Executing)
  148. {
  149. parameters.WriteTo(UnmanagedModel);
  150. parts.WriteTo(UnmanagedModel);
  151. didWrite = true;
  152. }
  153. }
  154. finally
  155. {
  156. Monitor.Exit(Lock);
  157. }
  158. }
  159. return didWrite;
  160. }
  161. /// <summary>
  162. /// Dispatches the task for (maybe async) execution.
  163. /// </summary>
  164. public void Update()
  165. {
  166. // Validate state.
  167. lock (Lock)
  168. {
  169. if (State == TaskState.Enqueued || State == TaskState.Executing)
  170. {
  171. return;
  172. }
  173. // Update state.
  174. State = TaskState.Enqueued;
  175. }
  176. CubismTaskQueue.Enqueue(this);
  177. }
  178. /// <summary>
  179. /// Forces the task to run now to completion.
  180. /// </summary>
  181. public bool UpdateNow()
  182. {
  183. // Validate state.
  184. lock (Lock)
  185. {
  186. if (State == TaskState.Enqueued || State == TaskState.Executing)
  187. {
  188. return false;
  189. }
  190. // Update state.
  191. State = TaskState.Enqueued;
  192. }
  193. // Run execution directly.
  194. Execute();
  195. return true;
  196. }
  197. /// <summary>
  198. /// Releases unmanaged resource.
  199. /// </summary>
  200. public void ReleaseUnmanaged()
  201. {
  202. ShouldReleaseUnmanaged = true;
  203. // Return if task is ongoing.
  204. lock (Lock)
  205. {
  206. if (State == TaskState.Enqueued || State == TaskState.Executing)
  207. {
  208. return;
  209. }
  210. }
  211. OnReleaseUnmanaged();
  212. ShouldReleaseUnmanaged = false;
  213. }
  214. /// <summary>
  215. /// Runs the task.
  216. /// </summary>
  217. private void Execute()
  218. {
  219. // Validate state.
  220. lock (Lock)
  221. {
  222. State = TaskState.Executing;
  223. }
  224. // Update native backend.
  225. UnmanagedModel.Update();
  226. // Get results.
  227. DynamicDrawableData.ReadFrom(UnmanagedModel);
  228. // Update state.
  229. lock (Lock)
  230. {
  231. State = TaskState.Executed;
  232. // Release native if requested.
  233. if (ShouldReleaseUnmanaged)
  234. {
  235. OnReleaseUnmanaged();
  236. }
  237. }
  238. }
  239. /// <summary>
  240. /// Actually releases native resource(s).
  241. /// </summary>
  242. private void OnReleaseUnmanaged()
  243. {
  244. UnmanagedModel.Release();
  245. Moc.ReleaseUnmanagedMoc();
  246. UnmanagedModel = null;
  247. }
  248. #region Implementation of ICubismTask
  249. void ICubismTask.Execute()
  250. {
  251. Execute();
  252. }
  253. #endregion
  254. #region Threading
  255. /// <summary>
  256. /// Task states.
  257. /// </summary>
  258. private enum TaskState
  259. {
  260. /// <summary>
  261. /// Idle state.
  262. /// </summary>
  263. Idle,
  264. /// <summary>
  265. /// Waiting-for-execution state.
  266. /// </summary>
  267. Enqueued,
  268. /// <summary>
  269. /// Executing state.
  270. /// </summary>
  271. Executing,
  272. /// <summary>
  273. /// Executed state.
  274. /// </summary>
  275. Executed
  276. }
  277. /// <summary>
  278. /// Lock.
  279. /// </summary>
  280. private object Lock { get; set; }
  281. /// <summary>
  282. /// Internal state.
  283. /// </summary>
  284. private TaskState State { get; set; }
  285. #endregion
  286. }
  287. }