SW 중심대학 OSS GIT 서버 박건태, 이승준, 고기완, 이준호 새로운 배포
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

156 lines
6.0 KiB

4 years ago
  1. using System;
  2. using Unity.Collections;
  3. using Unity.Collections.LowLevel.Unsafe;
  4. namespace UnityEngine.XR.ARSubsystems
  5. {
  6. /// <summary>
  7. /// Holds information related to an asynchronous camera image conversion request. Returned by
  8. /// <see cref="XRCameraImage.ConvertAsync"/>.
  9. /// </summary>
  10. public struct XRAsyncCameraImageConversion : IDisposable, IEquatable<XRAsyncCameraImageConversion>
  11. {
  12. XRCameraSubsystem m_CameraSubsystem;
  13. int m_RequestId;
  14. #if ENABLE_UNITY_COLLECTIONS_CHECKS
  15. AtomicSafetyHandle m_SafetyHandle;
  16. #endif
  17. /// <summary>
  18. /// The <see cref="XRCameraImageConversionParams"/> used during the conversion.
  19. /// </summary>
  20. /// <value>
  21. /// The parameters used during the conversion.
  22. /// </value>
  23. public XRCameraImageConversionParams conversionParams { get; private set; }
  24. /// <summary>
  25. /// The status of the request.
  26. /// </summary>
  27. /// <value>
  28. /// The status of the request.
  29. /// </value>
  30. public AsyncCameraImageConversionStatus status
  31. {
  32. get
  33. {
  34. if (m_CameraSubsystem == null)
  35. {
  36. return AsyncCameraImageConversionStatus.Disposed;
  37. }
  38. return m_CameraSubsystem.GetAsyncRequestStatus(m_RequestId);
  39. }
  40. }
  41. /// <summary>
  42. /// Start the image conversion using this class to interact with the asynchronous conversion and results.
  43. /// </summary>
  44. /// <param name="cameraSubsystem">The camera subsystem performing the image conversion.</param>
  45. /// <param name="nativeHandle">The native handle for the camera image.</param>
  46. /// <param name="conversionParams">The parameters for image conversion.</param>
  47. internal XRAsyncCameraImageConversion(XRCameraSubsystem cameraSubsystem, int nativeHandle,
  48. XRCameraImageConversionParams conversionParams)
  49. {
  50. m_CameraSubsystem = cameraSubsystem;
  51. m_RequestId = m_CameraSubsystem.ConvertAsync(nativeHandle, conversionParams);
  52. this.conversionParams = conversionParams;
  53. #if ENABLE_UNITY_COLLECTIONS_CHECKS
  54. m_SafetyHandle = AtomicSafetyHandle.Create();
  55. #endif
  56. }
  57. /// <summary>
  58. /// Get the raw image data. The returned <c>NativeArray</c> is a direct "view" into the native memory. The
  59. /// memory is only valid until this <see cref="XRAsyncCameraImageConversion"/> is disposed.
  60. /// </summary>
  61. /// <typeparam name="T">The type of data to return. No conversion is performed based on the type; this is
  62. /// merely for access convenience.</typeparam>
  63. /// <returns>
  64. /// A new <c>NativeArray</c> representing the raw image data. This method may fail; use
  65. /// <c>NativeArray.IsCreated</c> to determine the validity of the data.
  66. /// </returns>
  67. /// <exception cref="System.InvalidOperationException">Thrown if the asynchronous conversion
  68. /// <see cref="status"/> is not <see cref="AsyncCameraImageConversionStatus.Ready"/> or if the conversion is
  69. /// invalid.</exception>
  70. public unsafe NativeArray<T> GetData<T>() where T : struct
  71. {
  72. if (status != AsyncCameraImageConversionStatus.Ready)
  73. throw new InvalidOperationException("Async request is not ready.");
  74. IntPtr dataPtr;
  75. int dataLength;
  76. if (m_CameraSubsystem.TryGetAsyncRequestData(m_RequestId, out dataPtr, out dataLength))
  77. {
  78. int stride = UnsafeUtility.SizeOf<T>();
  79. var array = NativeArrayUnsafeUtility.ConvertExistingDataToNativeArray<T>(
  80. (void*)dataPtr, dataLength / stride, Allocator.None);
  81. #if ENABLE_UNITY_COLLECTIONS_CHECKS
  82. NativeArrayUnsafeUtility.SetAtomicSafetyHandle(ref array, m_SafetyHandle);
  83. #endif
  84. return array;
  85. }
  86. throw new InvalidOperationException("The XRAsyncCameraImageConversion is not valid.");
  87. }
  88. /// <summary>
  89. /// Dispose native resources associated with this request, including the raw image data. The <c>NativeArray</c>
  90. /// returned by <see cref="GetData"/> is invalidated immediately after calling <c>Dispose</c>.
  91. /// </summary>
  92. public void Dispose()
  93. {
  94. if (m_CameraSubsystem == null || m_RequestId == 0)
  95. return;
  96. m_CameraSubsystem.DisposeAsyncRequest(m_RequestId);
  97. m_CameraSubsystem = null;
  98. m_RequestId = 0;
  99. #if ENABLE_UNITY_COLLECTIONS_CHECKS
  100. AtomicSafetyHandle.Release(m_SafetyHandle);
  101. #endif
  102. }
  103. public override int GetHashCode()
  104. {
  105. unchecked
  106. {
  107. var hash = conversionParams.GetHashCode();
  108. hash = hash * 486187739 + m_RequestId.GetHashCode();
  109. if (m_CameraSubsystem != null)
  110. hash = hash * 486187739 + m_CameraSubsystem.GetHashCode();
  111. return hash;
  112. }
  113. }
  114. public override bool Equals(object obj)
  115. {
  116. return ((obj is XRAsyncCameraImageConversion) && Equals((XRAsyncCameraImageConversion)obj));
  117. }
  118. public bool Equals(XRAsyncCameraImageConversion other)
  119. {
  120. return
  121. (conversionParams.Equals(other.conversionParams)) &&
  122. (m_RequestId == other.m_RequestId) &&
  123. (m_CameraSubsystem == other.m_CameraSubsystem);
  124. }
  125. public static bool operator ==(XRAsyncCameraImageConversion lhs, XRAsyncCameraImageConversion rhs)
  126. {
  127. return lhs.Equals(rhs);
  128. }
  129. public static bool operator !=(XRAsyncCameraImageConversion lhs, XRAsyncCameraImageConversion rhs)
  130. {
  131. return !lhs.Equals(rhs);
  132. }
  133. public override string ToString()
  134. {
  135. return string.Format("ConversionParams: {0}", conversionParams);
  136. }
  137. }
  138. }