using System; using System.Collections; using System.Collections.Generic; using Unity.Collections; using Unity.Collections.LowLevel.Unsafe; using UnityEngine.InputSystem.Utilities; ////TODO: batch append method ////TODO: switch to NativeArray long length (think we have it in Unity 2018.3) ////REVIEW: can we get rid of kBufferSizeUnknown and force size to always be known? (think this would have to wait until //// the native changes have landed in 2018.3) namespace UnityEngine.InputSystem.LowLevel { /// /// A buffer of raw memory holding a sequence of input events. /// /// /// Note that event buffers are not thread-safe. It is not safe to write events to the buffer /// concurrently from multiple threads. It is, however, safe to traverse the contents of an /// existing buffer from multiple threads as long as it is not mutated at the same time. /// public unsafe struct InputEventBuffer : IEnumerable, IDisposable, ICloneable { public const long BufferSizeUnknown = -1; /// /// Total number of events in the buffer. /// /// Number of events currently in the buffer. public int eventCount => m_EventCount; /// /// Size of the used portion of the buffer in bytes. Use to /// get the total allocated size. /// /// Used size of buffer in bytes. /// /// If the size is not known, returns . /// /// Note that the size does not usually correspond to times sizeof(InputEvent). /// as instances are variable in size. /// public long sizeInBytes => m_SizeInBytes; /// /// Total size of allocated memory in bytes. This value minus is the /// spare capacity of the buffer. Will never be less than . /// /// Size of allocated memory in bytes. /// /// A buffer's capacity determines how much event data can be written to the buffer before it has to be /// reallocated. /// public long capacityInBytes { get { if (!m_Buffer.IsCreated) return 0; return m_Buffer.Length; } } /// /// The raw underlying memory buffer. /// /// Underlying buffer of unmanaged memory. public NativeArray data => m_Buffer; /// /// Pointer to the first event in the buffer. /// /// Pointer to first event in buffer. public InputEventPtr bufferPtr { // When using ConvertExistingDataToNativeArray, the NativeArray isn't getting a "safety handle" (seems like a bug) // and calling GetUnsafeReadOnlyPtr() will result in a NullReferenceException. Get the pointer without checks here. get { return (InputEvent*)NativeArrayUnsafeUtility.GetUnsafeBufferPointerWithoutChecks(m_Buffer); } } /// /// Construct an event buffer using the given memory block containing s. /// /// A buffer containing number of input events. The /// individual events in the buffer are variable-sized (depending on the type of each event). /// The number of events in . Can be zero. /// Total number of bytes of event data in the memory block pointed to by . /// If -1 (default), the size of the actual event data in the buffer is considered unknown and has to be determined by walking /// number of events (due to the variable size of each event). /// The total size of the memory block allocated at . If this /// is larger than , additional events can be appended to the buffer until the capacity /// is exhausted. If this is -1 (default), the capacity is considered unknown and no additional events can be /// appended to the buffer. /// is null and is not zero /// -or- is less than . public InputEventBuffer(InputEvent* eventPtr, int eventCount, int sizeInBytes = -1, int capacityInBytes = -1) : this() { if (eventPtr == null && eventCount != 0) throw new ArgumentException("eventPtr is NULL but eventCount is != 0", nameof(eventCount)); if (capacityInBytes != 0 && capacityInBytes < sizeInBytes) throw new ArgumentException($"capacity({capacityInBytes}) cannot be smaller than size({sizeInBytes})", nameof(capacityInBytes)); if (eventPtr != null) { if (capacityInBytes < 0) capacityInBytes = sizeInBytes; m_Buffer = NativeArrayUnsafeUtility.ConvertExistingDataToNativeArray(eventPtr, capacityInBytes > 0 ? capacityInBytes : 0, Allocator.None); m_SizeInBytes = sizeInBytes >= 0 ? sizeInBytes : BufferSizeUnknown; m_EventCount = eventCount; m_WeOwnTheBuffer = false; } } /// /// Construct an event buffer using the array containing s. /// /// A native array containing number of input events. The /// individual events in the buffer are variable-sized (depending on the type of each event). /// The number of events in . Can be zero. /// Total number of bytes of event data in the . /// If -1 (default), the size of the actual event data in is considered unknown and has to be determined by walking /// number of events (due to the variable size of each event). /// If true, ownership of the NativeArray given by is /// transferred to the InputEventBuffer. Calling will deallocate the array. Also, /// may re-allocate the array. /// has no memory allocated but is not zero. /// is greater than the total length allocated for /// . public InputEventBuffer(NativeArray buffer, int eventCount, int sizeInBytes = -1, bool transferNativeArrayOwnership = false) { if (eventCount > 0 && !buffer.IsCreated) throw new ArgumentException("buffer has no data but eventCount is > 0", nameof(eventCount)); if (sizeInBytes > buffer.Length) throw new ArgumentOutOfRangeException(nameof(sizeInBytes)); m_Buffer = buffer; m_WeOwnTheBuffer = transferNativeArrayOwnership; m_SizeInBytes = sizeInBytes >= 0 ? sizeInBytes : buffer.Length; m_EventCount = eventCount; } /// /// Append a new event to the end of the buffer by copying the event from . /// /// Data of the event to store in the buffer. This will be copied in full as /// per found in the event's header. /// If the buffer needs to be reallocated to accommodate the event, number of /// bytes to grow the buffer by. /// If the buffer needs to be reallocated to accommodate the event, the type of allocation to /// use. /// is null. /// /// If the buffer's current capacity (see ) is smaller than /// of the given event, the buffer will be reallocated. /// public void AppendEvent(InputEvent* eventPtr, int capacityIncrementInBytes = 2048, Allocator allocator = Allocator.Persistent) { if (eventPtr == null) throw new ArgumentNullException(nameof(eventPtr)); // Allocate space. var eventSizeInBytes = eventPtr->sizeInBytes; var destinationPtr = AllocateEvent((int)eventSizeInBytes, capacityIncrementInBytes, allocator); // Copy event. UnsafeUtility.MemCpy(destinationPtr, eventPtr, eventSizeInBytes); } /// /// Make space for an event of bytes and return a pointer to /// the memory for the event. /// /// Number of bytes to make available for the event including the event header (see ). /// If the buffer needs to be reallocated to accommodate the event, number of /// bytes to grow the buffer by. /// If the buffer needs to be reallocated to accommodate the event, the type of allocation to /// use. /// A pointer to a block of memory in . Store the event data here. /// is less than the size needed for the /// header of an . Will automatically be aligned to a multiple of 4. /// /// Only is initialized by this method. No other fields from the event's /// header are touched. /// /// The event will be appended to the buffer after the last event currently in the buffer (if any). /// public InputEvent* AllocateEvent(int sizeInBytes, int capacityIncrementInBytes = 2048, Allocator allocator = Allocator.Persistent) { if (sizeInBytes < InputEvent.kBaseEventSize) throw new ArgumentException( $"sizeInBytes must be >= sizeof(InputEvent) == {InputEvent.kBaseEventSize} (was {sizeInBytes})", nameof(sizeInBytes)); var alignedSizeInBytes = sizeInBytes.AlignToMultipleOf(InputEvent.kAlignment); // See if we need to enlarge our buffer. var necessaryCapacity = m_SizeInBytes + alignedSizeInBytes; var currentCapacity = capacityInBytes; if (currentCapacity < necessaryCapacity) { // Yes, so reallocate. var newCapacity = necessaryCapacity.AlignToMultipleOf(capacityIncrementInBytes); if (newCapacity > int.MaxValue) throw new NotImplementedException("NativeArray long support"); var newBuffer = new NativeArray((int)newCapacity, allocator, NativeArrayOptions.ClearMemory); if (m_Buffer.IsCreated) { UnsafeUtility.MemCpy(newBuffer.GetUnsafePtr(), NativeArrayUnsafeUtility.GetUnsafeBufferPointerWithoutChecks(m_Buffer), this.sizeInBytes); if (m_WeOwnTheBuffer) m_Buffer.Dispose(); } m_Buffer = newBuffer; m_WeOwnTheBuffer = true; } var eventPtr = (InputEvent*)((byte*)NativeArrayUnsafeUtility.GetUnsafeBufferPointerWithoutChecks(m_Buffer) + m_SizeInBytes); eventPtr->sizeInBytes = (uint)sizeInBytes; m_SizeInBytes += alignedSizeInBytes; ++m_EventCount; return eventPtr; } /// /// Whether the given event pointer refers to data within the event buffer. /// /// /// /// /// Note that this method does NOT check whether the given pointer points to an actual /// event in the buffer. It solely performs a pointer out-of-bounds check. /// /// Also note that if the size of the memory buffer is unknown (, /// only a lower-bounds check is performed. /// public bool Contains(InputEvent* eventPtr) { if (eventPtr == null) return false; if (sizeInBytes == 0) return false; var bufferPtr = NativeArrayUnsafeUtility.GetUnsafeBufferPointerWithoutChecks(data); if (eventPtr < bufferPtr) return false; if (sizeInBytes != BufferSizeUnknown && eventPtr >= (byte*)bufferPtr + sizeInBytes) return false; return true; } public void Reset() { m_EventCount = 0; if (m_SizeInBytes != BufferSizeUnknown) m_SizeInBytes = 0; } /// /// Advance the read position to the next event in the buffer, preserving or not preserving the /// current event depending on . /// /// /// /// /// /// /// /// This method MUST ONLY BE CALLED if the current event has been fully processed. If the at /// is smaller than the current event, then this method will OVERWRITE parts or all of the current event. /// internal void AdvanceToNextEvent(ref InputEvent* currentReadPos, ref InputEvent* currentWritePos, ref int numEventsRetainedInBuffer, ref int numRemainingEvents, bool leaveEventInBuffer) { Debug.Assert(currentReadPos >= currentWritePos, "Current write position is beyond read position"); // Get new read position *before* potentially moving the current event so that we don't // end up overwriting the data we need to find the next event in memory. var newReadPos = currentReadPos; if (numRemainingEvents > 1) { // Don't perform safety check in non-debug builds. #if UNITY_EDITOR || DEVELOPMENT_BUILD newReadPos = InputEvent.GetNextInMemoryChecked(currentReadPos, ref this); #else newReadPos = InputEvent.GetNextInMemory(currentReadPos); #endif } // If the current event should be left in the buffer, advance write position. if (leaveEventInBuffer) { Debug.Assert(Contains(currentWritePos), "Current write position should be contained in buffer"); // Move down in buffer if read and write pos have deviated from each other. var numBytes = currentReadPos->sizeInBytes; if (currentReadPos != currentWritePos) UnsafeUtility.MemMove(currentWritePos, currentReadPos, numBytes); currentWritePos = (InputEvent*)((byte*)currentWritePos + numBytes.AlignToMultipleOf(4)); ++numEventsRetainedInBuffer; } currentReadPos = newReadPos; --numRemainingEvents; } public IEnumerator GetEnumerator() { return new Enumerator(this); } IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } public void Dispose() { // Nothing to do if we don't actually own the memory. if (!m_WeOwnTheBuffer) return; Debug.Assert(m_Buffer.IsCreated, "Buffer has not been created"); m_Buffer.Dispose(); m_WeOwnTheBuffer = false; m_SizeInBytes = 0; m_EventCount = 0; } public InputEventBuffer Clone() { var clone = new InputEventBuffer(); if (m_Buffer.IsCreated) { clone.m_Buffer = new NativeArray(m_Buffer.Length, Allocator.Persistent); clone.m_Buffer.CopyFrom(m_Buffer); clone.m_WeOwnTheBuffer = true; } clone.m_SizeInBytes = m_SizeInBytes; clone.m_EventCount = m_EventCount; return clone; } object ICloneable.Clone() { return Clone(); } private NativeArray m_Buffer; private long m_SizeInBytes; private int m_EventCount; private bool m_WeOwnTheBuffer; ////FIXME: what we really want is access to NativeArray's allocator label private struct Enumerator : IEnumerator { private readonly InputEvent* m_Buffer; private readonly int m_EventCount; private InputEvent* m_CurrentEvent; private int m_CurrentIndex; public Enumerator(InputEventBuffer buffer) { m_Buffer = buffer.bufferPtr; m_EventCount = buffer.m_EventCount; m_CurrentEvent = null; m_CurrentIndex = 0; } public bool MoveNext() { if (m_CurrentIndex == m_EventCount) return false; if (m_CurrentEvent == null) { m_CurrentEvent = m_Buffer; return m_CurrentEvent != null; } Debug.Assert(m_CurrentEvent != null, "Current event must not be null"); ++m_CurrentIndex; if (m_CurrentIndex == m_EventCount) return false; m_CurrentEvent = InputEvent.GetNextInMemory(m_CurrentEvent); return true; } public void Reset() { m_CurrentEvent = null; m_CurrentIndex = 0; } public void Dispose() { } public InputEventPtr Current => m_CurrentEvent; object IEnumerator.Current => Current; } } }