aexlo 0.3.0

Run After Effects plugins without Adobe dependencies.
Documentation
//! `AEGP_PFInterfaceSuite1`: bridges from an effect to the AEGP object model.
//!
//! aexlo has no cameras to hand out, so the camera getter reports a null
//! handle -- the SDK's own answer for "no camera" -- rather than leaving the
//! plugin's out-params uninitialised. The effect and its layer do exist: their
//! handles feed the Effect, Stream and Layer suites (see [`effect`](super::effect)
//! and [`layer`](super::layer)).
//! Everything that can be computed is: effect time maps 1:1 to comp time (the
//! layer starts at comp time 0), and the camera geometry is After Effects'
//! default comp camera (50 mm preset) for the effect's layer.

use crate::sys::{
	A_Err, A_FpLong, A_Matrix4, A_Time, A_long, A_short, A_u_long, AEGP_EffectRefH, AEGP_LayerH,
	AEGP_PFInterfaceSuite1, AEGP_PluginID, PF_Err_BAD_CALLBACK_PARAM, PF_Err_NONE, PF_ProgPtr,
};

use super::{effect, layer};
use crate::PluginInstance;
use crate::core::diagnostics::diag;

unsafe extern "C" fn get_effect_layer_sys(effect_pp_ref: PF_ProgPtr, layerPH: *mut AEGP_LayerH) -> A_Err {
	crate::core::ffi::guard("get_effect_layer_sys", || {
		diag!("AEGP_PFInterfaceSuite1/AEGP_GetEffectLayer",
			"effect_pp_ref" => format!("{:#x}", effect_pp_ref as usize),
			"layerPH" => format!("{:#x}", layerPH as usize),
		);

		let Some(out) = (unsafe { layerPH.as_mut() }) else {
			return PF_Err_BAD_CALLBACK_PARAM;
		};
		if effect_pp_ref.is_null() {
			*out = std::ptr::null_mut();
			return PF_Err_BAD_CALLBACK_PARAM;
		}
		*out = layer::layer_handle(effect_pp_ref);
		PF_Err_NONE
	})
}

unsafe extern "C" fn get_new_effect_for_effect_sys(
	_aegp_plugin_id: AEGP_PluginID,
	effect_pp_ref: PF_ProgPtr,
	effect_refPH: *mut AEGP_EffectRefH,
) -> A_Err {
	crate::core::ffi::guard("get_new_effect_for_effect_sys", || {
		diag!("AEGP_PFInterfaceSuite1/AEGP_GetNewEffectForEffect",
			"aegp_plugin_id" => _aegp_plugin_id as usize,
			"effect_pp_ref" => format!("{:#x}", effect_pp_ref as usize),
			"effect_refPH" => format!("{:#x}", effect_refPH as usize),
		);

		let Some(out) = (unsafe { effect_refPH.as_mut() }) else {
			return PF_Err_BAD_CALLBACK_PARAM;
		};
		if effect_pp_ref.is_null() {
			*out = std::ptr::null_mut();
			return PF_Err_BAD_CALLBACK_PARAM;
		}
		*out = effect::new_effect_handle(effect_pp_ref);
		PF_Err_NONE
	})
}

/// The effect's layer starts at comp time 0 with no stretch, so layer time is
/// comp time.
unsafe extern "C" fn convert_effect_to_comp_time_sys(
	_effect_pp_ref: PF_ProgPtr,
	what_timeL: A_long,
	time_scaleLu: A_u_long,
	comp_timePT: *mut A_Time,
) -> A_Err {
	crate::core::ffi::guard("convert_effect_to_comp_time_sys", || {
		diag!("AEGP_PFInterfaceSuite1/AEGP_ConvertEffectToCompTime",
			"effect_pp_ref" => format!("{:#x}", _effect_pp_ref as usize),
			"what_timeL" => what_timeL,
			"time_scaleLu" => time_scaleLu,
			"comp_timePT" => format!("{:#x}", comp_timePT as usize),
		);

		let Some(out) = (unsafe { comp_timePT.as_mut() }) else {
			return PF_Err_BAD_CALLBACK_PARAM;
		};
		*out = A_Time {
			value: what_timeL,
			scale: time_scaleLu,
		};
		PF_Err_NONE
	})
}

/// No camera layer exists; the SDK documents a null handle for that case.
unsafe extern "C" fn get_effect_camera_sys(
	_effect_pp_ref: PF_ProgPtr,
	_comp_timePT: *const A_Time,
	camera_layerPH: *mut AEGP_LayerH,
) -> A_Err {
	crate::core::ffi::guard("get_effect_camera_sys", || {
		diag!("AEGP_PFInterfaceSuite1/AEGP_GetEffectCamera",
			"effect_pp_ref" => format!("{:#x}", _effect_pp_ref as usize),
			"comp_timePT" => format!("{:#x}", _comp_timePT as usize),
			"camera_layerPH" => format!("{:#x}", camera_layerPH as usize),
		);

		let Some(out) = (unsafe { camera_layerPH.as_mut() }) else {
			return PF_Err_BAD_CALLBACK_PARAM;
		};
		*out = std::ptr::null_mut();
		PF_Err_NONE
	})
}

/// After Effects' default comp camera for a `width` x `height` comp: the 50 mm
/// preset (36 mm film width), centred on the comp and looking down +z, with
/// its image plane at the comp.
///
/// Returns the camera-to-world matrix (row vectors, translation in the last
/// row) and the distance to the image plane (the camera's zoom, in pixels).
pub(crate) fn default_camera(width: f64, height: f64) -> (A_Matrix4, A_FpLong) {
	let zoom = width * 50.0 / 36.0;
	let mut mat = [[0.0; 4]; 4];
	for (i, row) in mat.iter_mut().enumerate() {
		row[i] = 1.0;
	}
	mat[3][0] = width / 2.0;
	mat[3][1] = height / 2.0;
	mat[3][2] = -zoom;
	(A_Matrix4 { mat }, zoom)
}

unsafe extern "C" fn get_effect_camera_matrix(
	effect_pp_ref: PF_ProgPtr,
	_comp_timePT: *const A_Time,
	camera_matrixP: *mut A_Matrix4,
	dist_to_image_planePF: *mut A_FpLong,
	image_plane_widthPL: *mut A_short,
	image_plane_heightPL: *mut A_short,
) -> A_Err {
	crate::core::ffi::guard("get_effect_camera_matrix", || {
		diag!("AEGP_PFInterfaceSuite1/AEGP_GetEffectCameraMatrix",
			"effect_pp_ref" => format!("{:#x}", effect_pp_ref as usize),
			"comp_timePT" => format!("{:#x}", _comp_timePT as usize),
			"camera_matrixP" => format!("{:#x}", camera_matrixP as usize),
			"dist_to_image_planePF" => format!("{:#x}", dist_to_image_planePF as usize),
			"image_plane_widthPL" => format!("{:#x}", image_plane_widthPL as usize),
			"image_plane_heightPL" => format!("{:#x}", image_plane_heightPL as usize),
		);

		let Some(instance) = PluginInstance::get_instance_ptr(effect_pp_ref) else {
			return PF_Err_BAD_CALLBACK_PARAM;
		};
		// The comp is the effect's layer.
		let (width, height) = unsafe { instance.as_ref() }.input_size();
		let (matrix, zoom) = default_camera(width as f64, height as f64);
		unsafe {
			if let Some(out) = camera_matrixP.as_mut() {
				*out = matrix;
			}
			if let Some(out) = dist_to_image_planePF.as_mut() {
				*out = zoom;
			}
			if let Some(out) = image_plane_widthPL.as_mut() {
				*out = width.min(A_short::MAX as u32) as A_short;
			}
			if let Some(out) = image_plane_heightPL.as_mut() {
				*out = height.min(A_short::MAX as u32) as A_short;
			}
		}
		PF_Err_NONE
	})
}

/// Builds the `AEGP_PFInterfaceSuite1` vtable.
///
/// `const` so it can initialize the shared [`SUITE_CONTAINER`](crate::suites::SUITE_CONTAINER)
/// static; the suite is a stateless table of function pointers.
pub(super) const fn create_aegp_pf_interface_suite() -> AEGP_PFInterfaceSuite1 {
	AEGP_PFInterfaceSuite1 {
		AEGP_GetEffectLayer: Some(get_effect_layer_sys),
		AEGP_GetNewEffectForEffect: Some(get_new_effect_for_effect_sys),
		AEGP_ConvertEffectToCompTime: Some(convert_effect_to_comp_time_sys),
		AEGP_GetEffectCamera: Some(get_effect_camera_sys),
		AEGP_GetEffectCameraMatrix: Some(get_effect_camera_matrix),
	}
}

#[cfg(test)]
mod tests {
	use super::*;

	#[test]
	fn layer_is_the_effect_and_time_maps_through() {
		let suite = create_aegp_pf_interface_suite();
		let effect_ref = 0x40 as PF_ProgPtr;
		let mut layer: AEGP_LayerH = std::ptr::null_mut();
		let mut time = A_Time { value: -1, scale: 0 };
		unsafe {
			suite.AEGP_GetEffectLayer.unwrap()(effect_ref, &mut layer);
			suite.AEGP_ConvertEffectToCompTime.unwrap()(std::ptr::null_mut(), 12, 30, &mut time);
		}
		assert_eq!(layer, layer::layer_handle(effect_ref));
		assert_eq!((time.value, time.scale), (12, 30));

		let err = unsafe { suite.AEGP_GetEffectLayer.unwrap()(std::ptr::null_mut(), &mut layer) };
		assert_ne!(err, PF_Err_NONE);
		assert!(layer.is_null());
	}

	#[test]
	fn effect_handle_wraps_the_effect_ref() {
		let suite = create_aegp_pf_interface_suite();
		let effect_ref = 0x40 as PF_ProgPtr;
		let mut handle: AEGP_EffectRefH = std::ptr::null_mut();
		unsafe {
			assert_eq!(
				suite.AEGP_GetNewEffectForEffect.unwrap()(0, effect_ref, &mut handle),
				PF_Err_NONE
			);
			assert_eq!(effect::effect_ref_of(handle), Some(effect_ref));
			let dispose = crate::suites::SUITE_CONTAINER.effect2.AEGP_DisposeEffect.unwrap();
			assert_eq!(dispose(handle), PF_Err_NONE);
		}

		let mut handle: AEGP_EffectRefH = 1 as AEGP_EffectRefH;
		let err = unsafe { suite.AEGP_GetNewEffectForEffect.unwrap()(0, std::ptr::null_mut(), &mut handle) };
		assert_ne!(err, PF_Err_NONE);
		assert!(handle.is_null());
	}

	#[test]
	fn default_camera_centres_on_the_comp() {
		let (m, zoom) = default_camera(1920.0, 1080.0);
		assert!((zoom - 1920.0 * 50.0 / 36.0).abs() < 1e-9);
		assert_eq!(m.mat[3][..3], [960.0, 540.0, -zoom]);
		assert_eq!(m.mat[0][0], 1.0);
	}
}