using System;
using System.Runtime.InteropServices;
using AOT;
using UnityEngine.XR.ARSubsystems;
namespace UnityEngine.XR.ARKit
{
///
/// Represents an asynchronous promise of a high-resolution CPU image.
///
public class HighResolutionCpuImagePromise : Promise
{
///
/// The instance currently awaiting a result, or if no instance currently awaits a result.
///
///
/// IL2CPP cannot currently marshal instance methods, therefore the callback function passed to the
/// native API must be static. In order to call the instance method from within
/// the callback, this class must keep a static reference to the instance that will be resolved by the callback.
/// This design has a limitation that only one promise instance at a time can await a result; however, the
/// native ARKit API for high resolution frame capture has the same limitation, so there is no net negative impact.
/// Refer to ARKit's
/// captureHighResolutionFrameWithCompletion documentation for more information.
///
static HighResolutionCpuImagePromise s_Instance;
///
/// The object whose fields will be assigned by the native API.
///
static XRCpuImage.Cinfo s_Cinfo;
///
/// The function pointer for to be marshalled to native API.
///
static IntPtr s_OnPromiseCompleteFuncPtr = Marshal.GetFunctionPointerForDelegate((HighResolutionCpuImageCallback)OnPromiseComplete);
///
/// Callback passed to the native API to call when the high resolution frame capture is complete.
///
delegate void HighResolutionCpuImageCallback(bool wasSuccessful);
internal HighResolutionCpuImagePromise()
{
if (s_Instance != null)
{
Debug.LogError("A previous request for a high resolution capture hasn't completed yet. Subsequent requests will fail until the request in progress is completed.");
Resolve(default);
return;
}
s_Instance = this;
ARKitCameraSubsystem.TryAcquireHighResolutionCpuImageNative(s_OnPromiseCompleteFuncPtr, out s_Cinfo);
}
[MonoPInvokeCallback(typeof(HighResolutionCpuImageCallback))]
static void OnPromiseComplete(bool wasSuccessful)
{
var result = new Result
{
wasSuccessful = wasSuccessful,
highResolutionCpuImage = wasSuccessful ? new XRCpuImage(ARKitCpuImageApi.instance, s_Cinfo) : default
};
s_Instance.Resolve(result);
s_Instance = null;
}
///
/// Represents the result of an asynchronous request for a high resolution CPU image.
///
public struct Result
{
///
/// If , is initialized with a valid CPU image.
/// If , has a default value.
///
public bool wasSuccessful;
///
/// The high resolution CPU image. You are responsible to this image if
/// is .
///
///
public XRCpuImage highResolutionCpuImage;
}
}
}