static unsafe void OnAsyncConversionComplete( AsyncCameraImageConversionStatus status, XRCameraImageConversionParams conversionParams, IntPtr dataPtr, int dataLength, IntPtr context) { var handle = GCHandle.FromIntPtr(context); var onComplete = (Action <AsyncCameraImageConversionStatus, XRCameraImageConversionParams, NativeArray <byte> >)handle.Target; if (onComplete != null) { var data = NativeArrayUnsafeUtility.ConvertExistingDataToNativeArray <byte>( (void *)dataPtr, dataLength, Allocator.None); #if ENABLE_UNITY_COLLECTIONS_CHECKS var safetyHandle = AtomicSafetyHandle.Create(); NativeArrayUnsafeUtility.SetAtomicSafetyHandle(ref data, safetyHandle); #endif onComplete(status, conversionParams, data); #if ENABLE_UNITY_COLLECTIONS_CHECKS AtomicSafetyHandle.Release(safetyHandle); #endif } handle.Free(); }
/// <summary> /// Convert the <c>XRCameraImage</c> to one of the supported formats using the specified /// <paramref name="conversionParams"/>. The conversion is performed asynchronously. Use the returned /// <see cref="XRAsyncCameraImageConversion"/> to check for the status of the conversion, and retrieve the data /// when complete. /// </summary> /// <remarks> /// It is safe to <c>Dispose</c> the <c>XRCameraImage</c> before the asynchronous operation has completed. /// </remarks> /// <param name="conversionParams">The parameters to use for the conversion.</param> /// <returns>A <see cref="XRAsyncCameraImageConversion"/> which can be used to check the status of the /// conversion operation and get the resulting data.</returns> /// <seealso cref="FormatSupported"/> public XRAsyncCameraImageConversion ConvertAsync(XRCameraImageConversionParams conversionParams) { ValidateNativeHandleAndThrow(); ValidateConversionParamsAndThrow(conversionParams); return(new XRAsyncCameraImageConversion(m_CameraSubsystem, m_NativeHandle, conversionParams)); }
/// <summary> /// Similar to <see cref="ConvertAsync(int, XRCameraImageConversionParams)"/> but takes a delegate to invoke /// when the request is complete, rather than returning a request id. /// </summary> /// <remarks> /// If the first parameter to <paramref name="callback"/> is /// <see cref="AsyncCameraImageConversionStatus.Ready"/> then the <c>dataPtr</c> parameter must be valid for /// the duration of the invocation. The data may be destroyed immediately upon return. The /// <paramref name="context"/> parameter must be passed back to the <paramref name="callback"/>. /// </remarks> /// <param name="nativeHandle">A unique identifier for the camera image to convert.</param> /// <param name="conversionParams">The parameters to use during the conversion.</param> /// <param name="callback">A delegate which must be invoked when the request is complete, whether the /// conversion was successfully or not.</param> /// <param name="context">A native pointer which must be passed back unaltered to <paramref name="callback"/>. /// </param> /// <exception cref="System.NotSupportedException">Thrown if the implementation does not support camera image. /// </exception> internal void ConvertAsync( int nativeHandle, XRCameraImageConversionParams conversionParams, OnImageRequestCompleteDelegate callback, IntPtr context) { m_Provider.ConvertAsync(nativeHandle, conversionParams, callback, context); }
/// <summary> /// Convert the image with handle <paramref name="nativeHandle"/> using the provided /// <see cref="XRCameraImageConversionParams"/>. /// </summary> /// <param name="nativeHandle">A unique identifier for the camera image to convert.</param> /// <param name="conversionParams">The parameters to use during the conversion.</param> /// <param name="destinationBuffer">A buffer to write the converted image to.</param> /// <param name="bufferLength">The number of bytes available in the buffer.</param> /// <returns> /// <c>true</c> if the image was converted and stored in <paramref name="destinationBuffer"/>. /// </returns> /// <exception cref="System.NotSupportedException">Thrown if the implementation does not support camera image. /// </exception> internal bool TryConvert( int nativeHandle, XRCameraImageConversionParams conversionParams, IntPtr destinationBuffer, int bufferLength) { return(m_Provider.TryConvert(nativeHandle, conversionParams, destinationBuffer, bufferLength)); }
/// <summary> /// Method to be implemented by the provider to similar to /// <see cref="ConvertAsync(int, XRCameraImageConversionParams)"/> but takes a delegate to invoke when the /// request is complete, rather than returning a request id. /// </summary> /// <remarks> /// If the first parameter to <paramref name="callback"/> is /// <see cref="AsyncCameraImageConversionStatus.Ready"/> then the <c>dataPtr</c> parameter must be valid /// for the duration of the invocation. The data may be destroyed immediately upon return. The /// <paramref name="context"/> parameter must be passed back to the <paramref name="callback"/>. /// </remarks> /// <param name="nativeHandle">A unique identifier for the camera image to convert.</param> /// <param name="conversionParams">The parameters to use during the conversion.</param> /// <param name="callback">A delegate which must be invoked when the request is complete, whether the /// conversion was successfully or not.</param> /// <param name="context">A native pointer which must be passed back unaltered to /// <paramref name="callback"/>.</param> /// <exception cref="System.NotSupportedException">Thrown if the implementation does not support camera /// image.</exception> public virtual void ConvertAsync( int nativeHandle, XRCameraImageConversionParams conversionParams, OnImageRequestCompleteDelegate callback, IntPtr context) { throw new NotSupportedException("camera image conversion is not supported by this implementation"); }
/// <summary> /// Method to be implemented by the provider to convert the image with handle /// <paramref name="nativeHandle"/> using the provided <paramref cref="conversionParams"/>. /// </summary> /// <param name="nativeHandle">A unique identifier for the camera image to convert.</param> /// <param name="conversionParams">The parameters to use during the conversion.</param> /// <param name="destinationBuffer">A buffer to write the converted image to.</param> /// <param name="bufferLength">The number of bytes available in the buffer.</param> /// <returns> /// <c>true</c> if the image was converted and stored in <paramref name="destinationBuffer"/>. /// </returns> /// <exception cref="System.NotSupportedException">Thrown if the implementation does not support camera /// image.</exception> public virtual bool TryConvert( int nativeHandle, XRCameraImageConversionParams conversionParams, IntPtr destinationBuffer, int bufferLength) { throw new NotSupportedException("camera image conversion is not supported by this implementation"); }
/// <summary> /// Start the image conversion using this class to interact with the asynchronous conversion and results. /// </summary> /// <param name="cameraSubsystem">The camera subsystem performing the image conversion.</param> /// <param name="nativeHandle">The native handle for the camera image.</param> /// <param name="conversionParams">The parameters for image conversion.</param> internal XRAsyncCameraImageConversion(XRCameraSubsystem cameraSubsystem, int nativeHandle, XRCameraImageConversionParams conversionParams) { m_CameraSubsystem = cameraSubsystem; m_RequestId = m_CameraSubsystem.ConvertAsync(nativeHandle, conversionParams); this.conversionParams = conversionParams; #if ENABLE_UNITY_COLLECTIONS_CHECKS m_SafetyHandle = AtomicSafetyHandle.Create(); #endif }
/// <summary> /// <para>Convert the <c>XRCameraImage</c> to one of the supported formats using the specified /// <paramref name="conversionParams"/>. The conversion is performed asynchronously, and /// <paramref name="onComplete"/> is invoked when the conversion is complete, whether successful or not.</para> /// <para>The <c>NativeArray</c> provided in the <paramref name="onComplete"/> delegate is only valid during /// the invocation and is disposed immediately upon return.</para> /// </summary> /// <param name="conversionParams">The parameters to use for the conversion.</param> /// <param name="onComplete">A delegate to invoke when the conversion operation completes. The delegate is /// always invoked.</param> /// <seealso cref="FormatSupported"/> public void ConvertAsync( XRCameraImageConversionParams conversionParams, Action <AsyncCameraImageConversionStatus, XRCameraImageConversionParams, NativeArray <byte> > onComplete) { ValidateNativeHandleAndThrow(); ValidateConversionParamsAndThrow(conversionParams); var handle = GCHandle.Alloc(onComplete); var context = GCHandle.ToIntPtr(handle); m_CameraSubsystem.ConvertAsync(m_NativeHandle, conversionParams, s_OnAsyncConversionComplete, context); }
/// <summary> /// Convert the <c>XRCameraImage</c> to one of the supported formats using the specified /// <paramref name="conversionParams"/>. /// </summary> /// <param name="conversionParams">The parameters for the image conversion.</param> /// <param name="destinationBuffer">A pointer to memory to which to write the converted image.</param> /// <param name="bufferLength">The number of bytes pointed to by <paramref name="destinationBuffer"/>. Must be /// greater than or equal to the value returned by /// <see cref="GetConvertedDataSize(XRCameraImageConversionParams)"/>.</param> /// <exception cref="System.ArgumentException">Thrown if the <paramref name="bufferLength"/> is smaller than /// the data size required by the conversion.</exception> /// <exception cref="System.InvalidOperationException">Thrown if the conversion failed.</exception> /// <seealso cref="FormatSupported"/> public void Convert(XRCameraImageConversionParams conversionParams, IntPtr destinationBuffer, int bufferLength) { ValidateNativeHandleAndThrow(); ValidateConversionParamsAndThrow(conversionParams); int requiredDataSize = GetConvertedDataSize(conversionParams); if (bufferLength < requiredDataSize) { throw new ArgumentException(string.Format( "Conversion requires {0} bytes but only provided {1} bytes.", requiredDataSize, bufferLength), "bufferLength"); } if (!m_CameraSubsystem.TryConvert(m_NativeHandle, conversionParams, destinationBuffer, bufferLength)) { throw new InvalidOperationException("Conversion failed."); } }
/// <summary> /// Ensure the conversion parameters are valid. /// </summary> /// <param name="conversionParams">The conversion parameters to validate.</param> /// <exception cref="System.ArgumentOutOfRangeException">Thrown if the input image rect exceeds the actual /// image dimensions or if the output dimensions exceed the input dimensions.</exception> /// <exception cref="System.ArgumentException">Thrown if the texture format is not suppported</exception> /// <seealso cref="FormatSupported"/> void ValidateConversionParamsAndThrow(XRCameraImageConversionParams conversionParams) { if ((conversionParams.inputRect.x + conversionParams.inputRect.width > width) || (conversionParams.inputRect.y + conversionParams.inputRect.height > height)) { throw new ArgumentOutOfRangeException( "conversionParams.inputRect", "Input rect must be completely within the original image."); } if ((conversionParams.outputDimensions.x > conversionParams.inputRect.width) || (conversionParams.outputDimensions.y > conversionParams.inputRect.height)) { throw new ArgumentOutOfRangeException(string.Format( "Output dimensions must be less than or equal to the inputRect's dimensions: ({0}x{1} > {2}x{3}).", conversionParams.outputDimensions.x, conversionParams.outputDimensions.y, conversionParams.inputRect.width, conversionParams.inputRect.height)); } if (!FormatSupported(conversionParams.outputFormat)) { throw new ArgumentException("TextureFormat not supported.", "conversionParams.format"); } }
/// <summary> /// Create an asynchronous request to convert a camera image, similar to <see cref="TryConvert"/> except the /// conversion should happen on a thread other than the calling (main) thread. /// </summary> /// <param name="nativeHandle">A unique identifier for the camera image to convert.</param> /// <param name="conversionParams">The parameters to use during the conversion.</param> /// <returns>A unique identifier for this request.</returns> /// <exception cref="System.NotSupportedException">Thrown if the implementation does not support camera image. /// </exception> internal int ConvertAsync( int nativeHandle, XRCameraImageConversionParams conversionParams) { return(m_Provider.ConvertAsync(nativeHandle, conversionParams)); }
/// <summary> /// Method to be implemented by the provider to create an asynchronous request to convert a camera image, /// similar to <see cref="TryConvert"/> except the conversion should happen on a thread other than the /// calling (main) thread. /// </summary> /// <param name="nativeHandle">A unique identifier for the camera image to convert.</param> /// <param name="conversionParams">The parameters to use during the conversion.</param> /// <returns>A unique identifier for this request.</returns> /// <exception cref="System.NotSupportedException">Thrown if the implementation does not support camera /// image.</exception> public virtual int ConvertAsync( int nativeHandle, XRCameraImageConversionParams conversionParams) { throw new NotSupportedException("camera image conversion is not supported by this implementation"); }
/// <summary> /// Get the number of bytes required to store a converted image with the given parameters. /// </summary> /// <param name="conversionParams">The desired conversion parameters.</param> /// <returns>The number of bytes required to store the converted image.</returns> /// <exception cref="System.ArgumentException">Thrown if the desired format is not supported.</exception> /// <exception cref="System.ArgumentOutOfRangeException">Thrown if the desired dimensions exceed the native /// image dimensions.</exception> /// <exception cref="System.InvalidOperationException">Thrown if the image is invalid.</exception> /// <seealso cref="FormatSupported"/> public int GetConvertedDataSize(XRCameraImageConversionParams conversionParams) { return(GetConvertedDataSize( conversionParams.outputDimensions, conversionParams.outputFormat)); }