InputEventBuffer.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using Unity.Collections;
  5. using Unity.Collections.LowLevel.Unsafe;
  6. using UnityEngine.InputSystem.Utilities;
  7. ////TODO: batch append method
  8. ////TODO: switch to NativeArray long length (think we have it in Unity 2018.3)
  9. ////REVIEW: can we get rid of kBufferSizeUnknown and force size to always be known? (think this would have to wait until
  10. //// the native changes have landed in 2018.3)
  11. namespace UnityEngine.InputSystem.LowLevel
  12. {
  13. /// <summary>
  14. /// A buffer of raw memory holding a sequence of <see cref="InputEvent">input events</see>.
  15. /// </summary>
  16. /// <remarks>
  17. /// Note that event buffers are not thread-safe. It is not safe to write events to the buffer
  18. /// concurrently from multiple threads. It is, however, safe to traverse the contents of an
  19. /// existing buffer from multiple threads as long as it is not mutated at the same time.
  20. /// </remarks>
  21. public unsafe struct InputEventBuffer : IEnumerable<InputEventPtr>, IDisposable, ICloneable
  22. {
  23. public const long BufferSizeUnknown = -1;
  24. /// <summary>
  25. /// Total number of events in the buffer.
  26. /// </summary>
  27. /// <value>Number of events currently in the buffer.</value>
  28. public int eventCount => m_EventCount;
  29. /// <summary>
  30. /// Size of the used portion of the buffer in bytes. Use <see cref="capacityInBytes"/> to
  31. /// get the total allocated size.
  32. /// </summary>
  33. /// <value>Used size of buffer in bytes.</value>
  34. /// <remarks>
  35. /// If the size is not known, returns <see cref="BufferSizeUnknown"/>.
  36. ///
  37. /// Note that the size does not usually correspond to <see cref="eventCount"/> times <c>sizeof(InputEvent)</c>.
  38. /// as <see cref="InputEvent"/> instances are variable in size.
  39. /// </remarks>
  40. public long sizeInBytes => m_SizeInBytes;
  41. /// <summary>
  42. /// Total size of allocated memory in bytes. This value minus <see cref="sizeInBytes"/> is the
  43. /// spare capacity of the buffer. Will never be less than <see cref="sizeInBytes"/>.
  44. /// </summary>
  45. /// <value>Size of allocated memory in bytes.</value>
  46. /// <remarks>
  47. /// A buffer's capacity determines how much event data can be written to the buffer before it has to be
  48. /// reallocated.
  49. /// </remarks>
  50. public long capacityInBytes
  51. {
  52. get
  53. {
  54. if (!m_Buffer.IsCreated)
  55. return 0;
  56. return m_Buffer.Length;
  57. }
  58. }
  59. /// <summary>
  60. /// The raw underlying memory buffer.
  61. /// </summary>
  62. /// <value>Underlying buffer of unmanaged memory.</value>
  63. public NativeArray<byte> data => m_Buffer;
  64. /// <summary>
  65. /// Pointer to the first event in the buffer.
  66. /// </summary>
  67. /// <value>Pointer to first event in buffer.</value>
  68. public InputEventPtr bufferPtr
  69. {
  70. // When using ConvertExistingDataToNativeArray, the NativeArray isn't getting a "safety handle" (seems like a bug)
  71. // and calling GetUnsafeReadOnlyPtr() will result in a NullReferenceException. Get the pointer without checks here.
  72. get { return (InputEvent*)NativeArrayUnsafeUtility.GetUnsafeBufferPointerWithoutChecks(m_Buffer); }
  73. }
  74. public InputEventBuffer(InputEvent* eventPtr, int eventCount, int sizeInBytes = -1, int capacityInBytes = -1)
  75. : this()
  76. {
  77. if (eventPtr == null && eventCount != 0)
  78. throw new ArgumentException("eventPtr is NULL but eventCount is != 0", nameof(eventCount));
  79. if (capacityInBytes != 0 && capacityInBytes < sizeInBytes)
  80. throw new ArgumentException($"capacity({capacityInBytes}) cannot be smaller than size({sizeInBytes})",
  81. nameof(capacityInBytes));
  82. if (eventPtr != null)
  83. {
  84. if (capacityInBytes < 0)
  85. capacityInBytes = sizeInBytes;
  86. m_Buffer = NativeArrayUnsafeUtility.ConvertExistingDataToNativeArray<byte>(eventPtr,
  87. capacityInBytes > 0 ? capacityInBytes : 0, Allocator.None);
  88. m_SizeInBytes = sizeInBytes >= 0 ? sizeInBytes : BufferSizeUnknown;
  89. m_EventCount = eventCount;
  90. m_WeOwnTheBuffer = false;
  91. }
  92. }
  93. public InputEventBuffer(NativeArray<byte> buffer, int eventCount, int sizeInBytes = -1)
  94. {
  95. if (eventCount > 0 && !buffer.IsCreated)
  96. throw new ArgumentException("buffer has no data but eventCount is > 0", nameof(eventCount));
  97. if (sizeInBytes > buffer.Length)
  98. throw new ArgumentOutOfRangeException(nameof(sizeInBytes));
  99. m_Buffer = buffer;
  100. m_WeOwnTheBuffer = false;
  101. m_SizeInBytes = sizeInBytes >= 0 ? sizeInBytes : buffer.Length;
  102. m_EventCount = eventCount;
  103. }
  104. /// <summary>
  105. /// Append a new event to the end of the buffer.
  106. /// </summary>
  107. /// <param name="eventPtr"></param>
  108. /// <param name="capacityIncrementInBytes"></param>
  109. /// <exception cref="ArgumentNullException"></exception>
  110. /// <exception cref="ArgumentException"></exception>
  111. /// <remarks>
  112. /// If the buffer's current <see cref="capacityInBytes">capacity</see> is smaller than the <see cref="InputEvent.sizeInBytes">
  113. /// size</see> of the given <paramref name="eventPtr">event</paramref>,
  114. /// </remarks>
  115. public void AppendEvent(InputEvent* eventPtr, int capacityIncrementInBytes = 2048)
  116. {
  117. if (eventPtr == null)
  118. throw new ArgumentNullException(nameof(eventPtr));
  119. // Allocate space.
  120. var eventSizeInBytes = eventPtr->sizeInBytes;
  121. var destinationPtr = AllocateEvent((int)eventSizeInBytes, capacityIncrementInBytes);
  122. // Copy event.
  123. UnsafeUtility.MemCpy(destinationPtr, eventPtr, eventSizeInBytes);
  124. }
  125. public InputEvent* AllocateEvent(int sizeInBytes, int capacityIncrementInBytes = 2048)
  126. {
  127. if (sizeInBytes < InputEvent.kBaseEventSize)
  128. throw new ArgumentException(
  129. $"sizeInBytes must be >= sizeof(InputEvent) == {InputEvent.kBaseEventSize} (was {sizeInBytes})",
  130. nameof(sizeInBytes));
  131. var alignedSizeInBytes = sizeInBytes.AlignToMultipleOf(InputEvent.kAlignment);
  132. // See if we need to enlarge our buffer.
  133. var currentCapacity = capacityInBytes;
  134. if (currentCapacity < alignedSizeInBytes)
  135. {
  136. // Yes, so reallocate.
  137. var newCapacity = Math.Max(currentCapacity + capacityIncrementInBytes,
  138. currentCapacity + alignedSizeInBytes);
  139. var newSize = this.sizeInBytes + newCapacity;
  140. if (newSize > int.MaxValue)
  141. throw new NotImplementedException("NativeArray long support");
  142. var newBuffer =
  143. new NativeArray<byte>((int)newSize, Allocator.Persistent, NativeArrayOptions.ClearMemory);
  144. if (m_Buffer.IsCreated)
  145. UnsafeUtility.MemCpy(newBuffer.GetUnsafePtr(), m_Buffer.GetUnsafeReadOnlyPtr(), this.sizeInBytes);
  146. else
  147. m_SizeInBytes = 0;
  148. if (m_WeOwnTheBuffer)
  149. m_Buffer.Dispose();
  150. m_Buffer = newBuffer;
  151. m_WeOwnTheBuffer = true;
  152. }
  153. var eventPtr = (InputEvent*)((byte*)m_Buffer.GetUnsafePtr() + m_SizeInBytes);
  154. eventPtr->sizeInBytes = (uint)sizeInBytes;
  155. m_SizeInBytes += alignedSizeInBytes;
  156. ++m_EventCount;
  157. return eventPtr;
  158. }
  159. /// <summary>
  160. /// Whether the given event pointer refers to data within the event buffer.
  161. /// </summary>
  162. /// <param name="eventPtr"></param>
  163. /// <returns></returns>
  164. /// <remarks>
  165. /// Note that this method does NOT check whether the given pointer points to an actual
  166. /// event in the buffer. It solely performs a pointer out-of-bounds check.
  167. ///
  168. /// Also note that if the size of the memory buffer is unknown (<see cref="BufferSizeUnknown"/>,
  169. /// only a lower-bounds check is performed.
  170. /// </remarks>
  171. public bool Contains(InputEvent* eventPtr)
  172. {
  173. if (eventPtr == null)
  174. return false;
  175. if (sizeInBytes == 0)
  176. return false;
  177. var bufferPtr = NativeArrayUnsafeUtility.GetUnsafeBufferPointerWithoutChecks(data);
  178. if (eventPtr < bufferPtr)
  179. return false;
  180. if (sizeInBytes != BufferSizeUnknown && eventPtr >= (byte*)bufferPtr + sizeInBytes)
  181. return false;
  182. return true;
  183. }
  184. public void Reset()
  185. {
  186. m_EventCount = 0;
  187. if (m_SizeInBytes != BufferSizeUnknown)
  188. m_SizeInBytes = 0;
  189. }
  190. /// <summary>
  191. /// Advance the read position to the next event in the buffer, preserving or not preserving the
  192. /// current event depending on <paramref name="leaveEventInBuffer"/>.
  193. /// </summary>
  194. /// <param name="currentReadPos"></param>
  195. /// <param name="currentWritePos"></param>
  196. /// <param name="numEventsRetainedInBuffer"></param>
  197. /// <param name="numRemainingEvents"></param>
  198. /// <param name="leaveEventInBuffer"></param>
  199. /// <remarks>
  200. /// This method MUST ONLY BE CALLED if the current event has been fully processed. If the at <paramref name="currentWritePos"/>
  201. /// is smaller than the current event, then this method will OVERWRITE parts or all of the current event.
  202. /// </remarks>
  203. internal void AdvanceToNextEvent(ref InputEvent* currentReadPos,
  204. ref InputEvent* currentWritePos, ref int numEventsRetainedInBuffer,
  205. ref int numRemainingEvents, bool leaveEventInBuffer)
  206. {
  207. Debug.Assert(Contains(currentReadPos), "Current read position should be contained in buffer");
  208. Debug.Assert(Contains(currentWritePos), "Current write position should be contained in buffer");
  209. Debug.Assert(currentReadPos >= currentWritePos, "Current write position is beyond read position");
  210. // Get new read position *before* potentially moving the current event so that we don't
  211. // end up overwriting the data we need to find the next event in memory.
  212. var newReadPos = currentReadPos;
  213. if (numRemainingEvents > 1)
  214. newReadPos = InputEvent.GetNextInMemoryChecked(currentReadPos, ref this);
  215. // If the current event should be left in the buffer, advance write position.
  216. if (leaveEventInBuffer)
  217. {
  218. // Move down in buffer if read and write pos have deviated from each other.
  219. var numBytes = currentReadPos->sizeInBytes;
  220. if (currentReadPos != currentWritePos)
  221. UnsafeUtility.MemMove(currentWritePos, currentReadPos, numBytes);
  222. currentWritePos = (InputEvent*)((byte*)currentWritePos + numBytes.AlignToMultipleOf(4));
  223. ++numEventsRetainedInBuffer;
  224. }
  225. currentReadPos = newReadPos;
  226. --numRemainingEvents;
  227. }
  228. public IEnumerator<InputEventPtr> GetEnumerator()
  229. {
  230. return new Enumerator(this);
  231. }
  232. IEnumerator IEnumerable.GetEnumerator()
  233. {
  234. return GetEnumerator();
  235. }
  236. public void Dispose()
  237. {
  238. // Nothing to do if we don't actually own the memory.
  239. if (!m_WeOwnTheBuffer)
  240. return;
  241. Debug.Assert(m_Buffer.IsCreated, "Buffer has not been created");
  242. m_Buffer.Dispose();
  243. m_WeOwnTheBuffer = false;
  244. m_SizeInBytes = 0;
  245. m_EventCount = 0;
  246. }
  247. public InputEventBuffer Clone()
  248. {
  249. var clone = new InputEventBuffer();
  250. if (m_Buffer.IsCreated)
  251. {
  252. clone.m_Buffer = new NativeArray<byte>(m_Buffer.Length, Allocator.Persistent);
  253. clone.m_Buffer.CopyFrom(m_Buffer);
  254. clone.m_WeOwnTheBuffer = true;
  255. }
  256. clone.m_SizeInBytes = m_SizeInBytes;
  257. clone.m_EventCount = m_EventCount;
  258. return clone;
  259. }
  260. object ICloneable.Clone()
  261. {
  262. return Clone();
  263. }
  264. private NativeArray<byte> m_Buffer;
  265. private long m_SizeInBytes;
  266. private int m_EventCount;
  267. private bool m_WeOwnTheBuffer; ////FIXME: what we really want is access to NativeArray's allocator label
  268. private struct Enumerator : IEnumerator<InputEventPtr>
  269. {
  270. private readonly InputEvent* m_Buffer;
  271. private readonly int m_EventCount;
  272. private InputEvent* m_CurrentEvent;
  273. private int m_CurrentIndex;
  274. public Enumerator(InputEventBuffer buffer)
  275. {
  276. m_Buffer = buffer.bufferPtr;
  277. m_EventCount = buffer.m_EventCount;
  278. m_CurrentEvent = null;
  279. m_CurrentIndex = 0;
  280. }
  281. public bool MoveNext()
  282. {
  283. if (m_CurrentIndex == m_EventCount)
  284. return false;
  285. if (m_CurrentEvent == null)
  286. {
  287. m_CurrentEvent = m_Buffer;
  288. return m_CurrentEvent != null;
  289. }
  290. Debug.Assert(m_CurrentEvent != null, "Current event must not be null");
  291. ++m_CurrentIndex;
  292. if (m_CurrentIndex == m_EventCount)
  293. return false;
  294. m_CurrentEvent = InputEvent.GetNextInMemory(m_CurrentEvent);
  295. return true;
  296. }
  297. public void Reset()
  298. {
  299. m_CurrentEvent = null;
  300. m_CurrentIndex = 0;
  301. }
  302. public void Dispose()
  303. {
  304. }
  305. public InputEventPtr Current => m_CurrentEvent;
  306. object IEnumerator.Current => Current;
  307. }
  308. }
  309. }