/// <summary>
/// Experimental API only, subject to change. Connect a Texture IDs to a camera.
///
/// The camera is selected via TangoCameraId. Currently only TANGO_CAMERA_COLOR is supported. The texture
/// handles will be regenerated by the API on startup after which the application can use them, and will be
/// packed RGBA8888 data containing bytes of the image (so a single RGBA8888 will pack 4 neighbouring pixels).
/// If the config flag experimental_image_pixel_format is set to HAL_PIXEL_FORMAT_YCrCb_420_SP, texture_y will
/// pack 1280x720 pixels into a 320x720 RGBA8888 texture. texture_Cb and texture_Cr will contain copies of
/// the 2x2 downsampled interleaved UV planes packed similarly. If experimental_image_pixel_format is set to
/// HAL_PIXEL_FORMAT_YV12 then texture_y will have a stride of 1536 containing 1280 columns of data, packed
/// similarly in a RGBA8888 texture. texture_Cb and texture_Cr will be 2x2 downsampled versions of the same.
/// See YV12 and NV21 formats for details.
///
/// Note: The first scan-line of the color image is reserved for metadata instead of image pixels.
/// </summary>
/// <param name="cameraId">
/// The ID of the camera to connect this texture to. Only TANGO_CAMERA_COLOR and TANGO_CAMERA_FISHEYE are
/// supported.
/// </param>
/// <param name="textures">The texture IDs to use for the Y, Cb, and Cr planes.</param>
/// <param name="onUnityFrameAvailable">Callback method.</param>
internal static void ExperimentalConnectTexture(TangoEnums.TangoCameraId cameraId, YUVTexture textures, TangoService_onUnityFrameAvailable onUnityFrameAvailable)
{
#if UNITY_EDITOR
if (cameraId == TangoEnums.TangoCameraId.TANGO_CAMERA_COLOR)
{
// Resize textures to to simulated width.
textures.ResizeAll(EMULATED_CAMERA_PACKED_WIDTH, EMULATED_CAMERA_PACKED_Y_HEIGHT,
EMULATED_CAMERA_PACKED_WIDTH, EMULATED_CAMERA_PACKED_UV_HEIGHT);
if (!m_emulationIsInitialized)
{
_InitializeResourcesForEmulation();
m_emulationIsInitialized = true;
}
#if !UNITY_EDITOR_WIN
// Rebind Texture2Ds to the underlying OpenGL texture ids of our render textures
// Which is more or less the inverse of what the acutal tango service does but has the same effect.
textures.m_videoOverlayTextureY.UpdateExternalTexture(m_emulatedExpId_Y.GetNativeTexturePtr());
textures.m_videoOverlayTextureCb.UpdateExternalTexture(m_emulatedExpId_CbCr.GetNativeTexturePtr());
textures.m_videoOverlayTextureCr.UpdateExternalTexture(m_emulatedExpId_CbCr.GetNativeTexturePtr());
#else // !UNITY_EDITOR_WIN
// A crash occurs when assigning the pointer of a Unity RenderTexture to a Texture2D (as above)
// in a DirectX environment. Instead, size the Texture2D's correctly and copy render targets
// with ReadPixels() when updating experimental textures.
// Keeping separate paths because ReadPixels() is a significant performance hit.
textures.m_videoOverlayTextureY.Resize(m_emulatedExpId_Y.width, m_emulatedExpId_Y.height);
textures.m_videoOverlayTextureCb.Resize(m_emulatedExpId_CbCr.width, m_emulatedExpId_CbCr.height);
m_emulationTexIdCaptureTextures = textures;
#endif // !UNITY_EDITOR_WIN
}
#else
int returnValue = VideoOverlayAPI.TangoService_Experimental_connectTextureIdUnity(
cameraId,
(uint)textures.m_videoOverlayTextureY.GetNativeTexturePtr().ToInt64(),
(uint)textures.m_videoOverlayTextureCb.GetNativeTexturePtr().ToInt64(),
(uint)textures.m_videoOverlayTextureCr.GetNativeTexturePtr().ToInt64(),
callbackContext,
onUnityFrameAvailable);
if (returnValue != Common.ErrorType.TANGO_SUCCESS)
{
Debug.Log("VideoOverlayProvider.ConnectTexture() Texture was not connected to camera!");
}
#endif
}