1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
//! Loading a plugin: library and entry point resolution, setup commands,
//! dispatching commands, and teardown.
use super::*;
/// Signature of `PluginDataEntryFunction2`: the AE SDK's cross-platform replacement
/// for a binary PiPL resource. Plugins export this under a fixed symbol name and,
/// when called, report their real entry point name (and other PiPL metadata) back
/// through the `PF_PluginDataCB2` callback instead of the host parsing a resource.
type PluginDataEntryFn = unsafe extern "C" fn(
crate::sys::PF_PluginDataPtr,
crate::sys::PF_PluginDataCB2,
*const crate::sys::SPBasicSuite,
*const std::os::raw::c_char,
*const std::os::raw::c_char,
) -> crate::sys::PF_Err;
#[derive(Default)]
struct PluginDataInfo {
entry_point_name: Option<String>,
}
unsafe extern "C" fn receive_plugin_data(
in_ptr: crate::sys::PF_PluginDataPtr,
_in_name: *const crate::sys::A_u_char,
_in_match_name: *const crate::sys::A_u_char,
_in_category: *const crate::sys::A_u_char,
in_entry_point_name: *const crate::sys::A_u_char,
_in_kind: crate::sys::A_long,
_in_api_version_major: crate::sys::A_long,
_in_api_version_minor: crate::sys::A_long,
_in_reserved_info: crate::sys::A_long,
_in_support_url: *const crate::sys::A_u_char,
) -> crate::sys::A_Err {
crate::core::ffi::guard("receive_plugin_data", || {
if in_ptr.is_null() || in_entry_point_name.is_null() {
return PF_Err_INVALID_CALLBACK;
}
// SAFETY: Both pointers are non-null (checked above): `in_ptr` is the `PluginDataInfo` aexlo passed to `PluginDataEntryFunction2`, and the name is the plugin's NUL-terminated string.
let info = unsafe { &mut *(in_ptr as *mut PluginDataInfo) };
// SAFETY: As above, for the entry point name.
let name = unsafe { CStr::from_ptr(in_entry_point_name as *const std::os::raw::c_char) };
info.entry_point_name = Some(name.to_string_lossy().into_owned());
PF_Err_NONE
})
}
/// Plugin constructors. Taking a [`Host`] guarantees the process-wide
/// [`AppHost`](crate::AppHost) is fixed before any plugin code runs.
///
/// The plugin's code runs inside this process with no sandbox: see
/// [the crate docs](crate#trust-and-process-model).
impl Host {
/// Load the plugin at `path` with default options and run it through
/// `PF_Cmd_GLOBAL_SETUP`, `PF_Cmd_PARAMS_SETUP` and `PF_Cmd_SEQUENCE_SETUP`.
///
/// Shorthand for `host.loader(path).load()`; use [`Self::loader`] to set
/// strict mode or the frame size before setup runs.
///
/// # Errors
/// As [`Loader::load`].
pub fn load(self, path: impl AsRef<Path>) -> Result<PluginInstance> {
self.loader(path).load()
}
/// Start loading the plugin at `path`; finish with [`Loader::load`].
///
/// `path` is the plugin artifact exactly as it exists on disk: a bare
/// `.aex`/`.dll` file on Windows, or a `.plugin` bundle directory on macOS.
/// Callers never need to branch on platform -- if `path` is a directory it's
/// treated as a bundle and the actual binary under `Contents/MacOS/` is
/// resolved automatically; if it's a file, it's loaded as-is.
pub fn loader(self, path: impl AsRef<Path>) -> Loader {
Loader::new(Source::Path(path.as_ref().to_path_buf()))
}
/// Drive an entry point that already lives in this process with default
/// options. Shorthand for `host.entry_loader(entry).load()`.
///
/// # Errors
/// As [`Loader::load`].
///
/// # Safety
/// As [`Self::entry_loader`].
pub unsafe fn from_entry(self, entry: PluginEntryPoint) -> Result<PluginInstance> {
// SAFETY: The caller upholds `entry_loader`'s contract.
unsafe { self.entry_loader(entry) }.load()
}
/// [`Self::from_entry`] for an entry point given by address. Shorthand for
/// `host.entry_loader_raw(entry_addr).load()`.
///
/// # Errors
/// As [`Loader::load`].
///
/// # Safety
/// As [`Self::entry_loader_raw`].
pub unsafe fn from_entry_raw(self, entry_addr: usize) -> Result<PluginInstance> {
// SAFETY: The caller upholds `entry_loader_raw`'s contract.
unsafe { self.entry_loader_raw(entry_addr) }.load()
}
/// Start driving an entry point that already lives in this process,
/// skipping the whole `dlopen` → cdylib → bundle path.
///
/// The intended caller is a plugin's own `#[test]`/example: the
/// `after-effects` `define_effect!` macro exports `EffectMain` as a plain
/// `#[no_mangle] extern "C"` function, so it can be handed here directly for
/// a fast, in-process preview that also happens to be debuggable (breakpoints
/// and real backtraces, unlike a `dlopen`ed cdylib).
///
/// Use this when the plugin crate is built against the *same*
/// `after-effects-sys` version as aexlo ([`crate::sys`]), so
/// `EffectMain as PluginEntryPoint` coerces. Otherwise reach for
/// [`Self::entry_loader_raw`].
///
/// # Safety
/// `entry` must be a valid AE effect entry point that is ABI-compatible with
/// [`PluginEntryPoint`] and stays callable for the lifetime of the instance.
pub unsafe fn entry_loader(self, entry: PluginEntryPoint) -> Loader {
Loader::new(Source::Entry(entry))
}
/// Like [`Self::entry_loader`], but takes the entry point's raw address and
/// transmutes it to the expected ABI.
///
/// This exists for the common case where the plugin crate pulls a *different*
/// `after-effects-sys` version than aexlo: the two `PluginEntryPoint` types
/// are then nominally distinct and won't coerce, even though the C ABI is
/// identical. Passing the address (`EffectMain as usize`) sidesteps the type
/// mismatch, making ABI compatibility the caller's explicit promise.
///
/// # Safety
/// `entry_addr` must be the address of a function that is ABI-compatible with
/// [`PluginEntryPoint`] and stays callable for the lifetime of the instance.
pub unsafe fn entry_loader_raw(self, entry_addr: usize) -> Loader {
// SAFETY: the caller promises `entry_addr` is such a function.
let entry: PluginEntryPoint = unsafe { std::mem::transmute(entry_addr) };
Loader::new(Source::Entry(entry))
}
}
/// Where a [`Loader`] gets the plugin's entry point from.
enum Source {
/// A plugin binary or bundle on disk, `dlopen`ed by [`Loader::load`].
Path(PathBuf),
/// An entry point already resident in this process.
Entry(PluginEntryPoint),
}
/// Options for loading a plugin, applied before its first command runs.
///
/// Made by [`Host::loader`] (a plugin on disk) or [`Host::entry_loader`] (an
/// in-process entry point):
///
/// ```no_run
/// use aexlo::{Depth8, Host, Layer, Strict};
///
/// # fn main() -> aexlo::Result<()> {
/// let fx = Host::get()
/// .loader("MyEffect.aex")
/// .strict(Strict::all())
/// .input(Layer::<Depth8>::black(1920, 1080))
/// .load()?;
/// # Ok(())
/// # }
/// ```
#[must_use = "a Loader does nothing until `load` is called"]
pub struct Loader {
source: Source,
strict: crate::Strict,
observer: Option<(Arc<dyn crate::Observer>, crate::ObserveLevel)>,
size: Option<(u32, u32)>,
input: Option<AnyLayer>,
output_size: Option<(u32, u32)>,
}
impl Loader {
fn new(source: Source) -> Self {
Self {
source,
strict: crate::Strict::default(),
observer: None,
size: None,
input: None,
output_size: None,
}
}
/// Turn strict mode on from the very first command, so what the plugin
/// allocates during setup is tracked too (see [`PluginInstance::finish`]).
pub fn strict(mut self, strict: crate::Strict) -> Self {
self.strict = strict;
self
}
/// Install an observer before the first command, so it sees setup too:
/// `GLOBAL_SETUP`, `PARAMS_SETUP`, `SEQUENCE_SETUP` and what they call
/// and allocate (see [`PluginInstance::set_observer`]).
pub fn observer(mut self, observer: Arc<dyn crate::Observer>, level: crate::ObserveLevel) -> Self {
self.observer = Some((observer, level));
self
}
/// Set the frame size every setup command sees: an opaque black 8 bpc
/// input and an output of `width` x `height`. [`Self::input`] and
/// [`Self::output_size`] override either side.
pub fn size(mut self, width: u32, height: u32) -> Self {
self.size = Some((width, height));
self
}
/// Set the input layer every setup command sees. Its depth is the
/// project depth. The output takes its size unless [`Self::output_size`]
/// or [`Self::size`] sets one.
pub fn input(mut self, input: impl Into<AnyLayer>) -> Self {
self.input = Some(input.into());
self
}
/// Set the output size every setup command sees.
pub fn output_size(mut self, width: u32, height: u32) -> Self {
self.output_size = Some((width, height));
self
}
/// Load the plugin and run it through `PF_Cmd_GLOBAL_SETUP`,
/// `PF_Cmd_PARAMS_SETUP` and `PF_Cmd_SEQUENCE_SETUP`.
///
/// # Errors
/// - [`AexloError::InvalidSize`] if a size is zero or does not fit in `i32`.
/// - [`AexloError::PluginNotFound`] / [`AexloError::AmbiguousBundle`] if the
/// binary can't be located, [`AexloError::PluginLoad`] if it can't be
/// opened or exports no entry point.
/// - [`AexloError::PluginExecutionFailed`] if the plugin rejects a setup
/// command.
pub fn load(self) -> Result<PluginInstance> {
let input = match (self.input, self.size) {
(Some(input), _) => Some(input),
(None, Some((width, height))) => {
check_size(width, height)?;
Some(AnyLayer::filled(PixelDepthKind::U8, width, height, OPAQUE_BLACK))
}
(None, None) => None,
};
let output_size = self
.output_size
.or(self.size)
.or_else(|| input.as_ref().map(|layer| (layer.width(), layer.height())));
if let Some(input) = &input {
check_size(input.width(), input.height())?;
}
if let Some((width, height)) = output_size {
check_size(width, height)?;
}
let path = match &self.source {
Source::Path(path) => path.as_path(),
Source::Entry(_) => Path::new("<in-process>"),
};
let mut instance = PluginInstance::new(path);
if let Some(input) = input {
match input {
AnyLayer::U8(layer) => instance.set_input_layer(layer),
AnyLayer::U16(layer) => instance.set_input_layer(layer),
AnyLayer::F32(layer) => instance.set_input_layer(layer),
}
}
if let Some((width, height)) = output_size {
instance.set_render_size(width, height);
}
instance.set_strict(self.strict);
if let Some((observer, level)) = self.observer {
instance.set_observer(Some(observer), level);
}
match self.source {
Source::Path(_) => instance.load()?,
Source::Entry(entry) => {
instance.entry_point = Some(entry);
instance.entry_point_name = Some(DEFAULT_ENTRY_POINT_NAME.to_string());
// No `raw_library`: the entry point is already resident in this process.
}
}
instance.finalize()?;
Ok(instance)
}
}
/// [`AexloError::InvalidSize`] unless `width` x `height` is a size After
/// Effects can represent.
fn check_size(width: u32, height: u32) -> Result<()> {
if width == 0 || height == 0 || width > i32::MAX as u32 || height > i32::MAX as u32 {
return Err(AexloError::InvalidSize { width, height });
}
Ok(())
}
impl Drop for PluginInstance {
/// Tears the plugin down in the same order as After Effects so that the
/// state it allocated is released.
///
/// Failures are only logged, because panicking in `drop` would abort the
/// process and the plugin is about to be unloaded anyway.
fn drop(&mut self) {
self.teardown();
}
}
impl PluginInstance {
/// Run the post-load setup shared by every constructor: `GLOBAL_SETUP`,
/// `PARAMS_SETUP`, then `SEQUENCE_SETUP`. Independent of where the entry point
/// came from (`dlopen`ed or handed in directly).
pub(super) fn finalize(&mut self) -> Result<()> {
self.setup_global()?;
self.setup_params()?;
self.setup_arbitrary_params();
self.setup_sequence()?;
Ok(())
}
/// Create a new PluginInstance with default values.
pub(super) fn new(path: &Path) -> Self {
let interact_callbacks = crate::host::interact::create_interact_callbacks();
let utility_callbacks = crate::host::utility::create_utility_callbacks();
let pica = Self::build_pica_suite();
let input_layer = HostLayer::new(AnyLayer::new(Self::build_layer(
WIDTH,
HEIGHT,
Pixel::<Depth8>::green(),
)));
let output_layer = HostLayer::new(AnyLayer::filled(PixelDepthKind::U8, WIDTH, HEIGHT, OPAQUE_BLACK));
{
let mut instance_placeholder = PluginInstance {
aegp_owner: crate::suites::register::new_owner(),
raw_library: None,
entry_point: None,
entry_point_name: None,
binary_file_path: path.to_path_buf(),
resolved_binary_path: None,
utility_callbacks,
pica,
host_info: InDataHost::new(interact_callbacks),
effect_state: EffectState::default(),
// Match the output world / default layers so the frame size the plugin
// sees is consistent across in_data, the checkout rects, and the worlds.
render_context: {
let mut ctx = RenderContext::default();
ctx.set_size(WIDTH as i32, HEIGHT as i32);
ctx
},
out_data: crate::core::helpers::OutDataBuilder::new().build(),
global_out_flags: 0,
global_out_flags2: 0,
global_version: 0,
params: Vec::new(),
param_labels: Vec::new(),
params_dirty: false,
params_ptr_cache: Vec::new(),
input_layer,
output_layer,
world: crate::core::helpers::LayerDefBuilder::new()
.with_size(WIDTH as i32, HEIGHT as i32)
.build(),
// Placeholder; wired to `input_layer`'s pixels in `wire_self_pointers`.
input_world: crate::core::helpers::LayerDefBuilder::new()
.with_size(WIDTH as i32, HEIGHT as i32)
.build(),
smart_render_data: SmartRenderData::new(),
gpu_context: None,
gpu_upload_staging: Vec::new(),
gpu_readback_staging: Vec::new(),
gpu_input_uploaded: false,
gpu_data: null_mut(),
gpu_device_ready: false,
options_button_name: None,
ui_contexts: HashMap::new(),
custom_ui_ready: std::collections::HashSet::new(),
custom_controls: HashMap::new(),
supported_pixel_formats: Vec::new(),
mask_paths: Vec::new(),
text_layer: None,
linked_layers: HashMap::new(),
layer_checkouts: HashMap::new(),
parallel_iterate: true,
owned_arb_values: Vec::new(),
arb_streams_live: false,
observer: None,
strict: crate::Strict::default(),
tracker: Default::default(),
output_guard: None,
torn_down: false,
};
instance_placeholder.wire_self_pointers();
instance_placeholder.push_input_layer_param();
instance_placeholder
}
}
/// Build the `SPBasicSuite` vtable handed to the plugin for acquiring host suites.
fn build_pica_suite() -> Box<crate::sys::SPBasicSuite> {
Box::new(crate::sys::SPBasicSuite {
AcquireSuite: Some(crate::suites::rusty_acquire_suite),
ReleaseSuite: Some(crate::suites::rusty_release_suite),
IsEqual: None,
AllocateBlock: None,
FreeBlock: None,
ReallocateBlock: None,
Undefined: None,
})
}
fn wire_self_pointers(&mut self) {
self.sync_output_world();
self.input_world = self.input_layer.as_sys();
}
/// Call the plugin with `PF_Cmd_GLOBAL_SETUP` command.
fn setup_global(&mut self) -> Result<()> {
self.call_plugin(RawCommand::GlobalSetup, null_mut())?;
self.global_out_flags = self.out_data.out_flags;
// The capabilities declared here are what count. `out_data` is shared
// by every later command, and plugins overwrite its flags in some of
// them (the SDK samples set `out_flags2` in GPU device setup), so it
// must not be consulted afterwards.
self.global_out_flags2 = self.out_data.out_flags2;
self.global_version = self.out_data.my_version;
Ok(())
}
/// Call the plugin with `PF_Cmd_PARAMS_SETUP` command.
fn setup_params(&mut self) -> Result<()> {
self.arb_streams_live = false;
self.call_plugin(RawCommand::ParamsSetup, null_mut())
}
/// Give every arbitrary-data parameter a value, as After Effects does when
/// the effect is applied: the parameter starts at its default, so the
/// plugin copies `dephault` through `PF_Cmd_ARBITRARY_CALLBACK`
/// (`PF_Arbitrary_COPY_FUNC`). Without a default, or when the copy fails,
/// it allocates one through `PF_Arbitrary_NEW_FUNC`. Plugins dereference the
/// value handle they check out, so leaving it null crashes them; and some
/// (TextGrad) make `NEW_FUNC` an empty placeholder, so the default must win.
fn setup_arbitrary_params(&mut self) {
for index in 0..self.params.len() {
if self.params[index].param_type != PF_Param_ARBITRARY_DATA as PF_ParamType {
continue;
}
// SAFETY: the parameter was verified to be arbitrary data above.
let arb = unsafe { self.params[index].u.arb_d };
if !arb.value.is_null() {
continue;
}
let mut value: PF_ArbitraryH = null_mut();
if !arb.dephault.is_null() {
// SAFETY: `PF_ArbParamsExtra` is a plain C struct (integers, raw pointers, unions of those), for which all-zero bytes are a valid value.
let mut extra: PF_ArbParamsExtra = unsafe { std::mem::zeroed() };
extra.which_function = PF_Arbitrary_COPY_FUNC as _;
extra.id = arb.id;
extra.u.copy_func_params.refconPV = arb.refconPV;
extra.u.copy_func_params.src_arbH = arb.dephault;
extra.u.copy_func_params.dst_arbPH = &mut value;
if let Err(err) = self.call_plugin(RawCommand::ArbitraryCallback, &mut extra as *mut _ as *mut c_void) {
log::warn!("arbitrary param #{index}: copying the default failed: {err:?}");
value = null_mut();
}
}
if value.is_null() {
// SAFETY: `PF_ArbParamsExtra` is a plain C struct (integers, raw pointers, unions of those), for which all-zero bytes are a valid value.
let mut extra: PF_ArbParamsExtra = unsafe { std::mem::zeroed() };
extra.which_function = PF_Arbitrary_NEW_FUNC as _;
extra.id = arb.id;
extra.u.new_func_params.refconPV = arb.refconPV;
extra.u.new_func_params.arbPH = &mut value;
if let Err(err) = self.call_plugin(RawCommand::ArbitraryCallback, &mut extra as *mut _ as *mut c_void) {
log::warn!("arbitrary param #{index}: creating a value failed: {err:?}");
value = null_mut();
}
}
if value.is_null() {
// Legitimate for arbs that only carry a custom UI.
log::debug!("arbitrary param #{index}: the plugin created no value");
continue;
}
self.params[index].u.arb_d.value = value;
self.owned_arb_values.push(index);
}
self.arb_streams_live = true;
}
/// Hand the arbitrary-data values created by [`Self::setup_arbitrary_params`]
/// back to the plugin (`PF_Arbitrary_DISPOSE_FUNC`).
fn dispose_arbitrary_params(&mut self) {
for index in std::mem::take(&mut self.owned_arb_values) {
// SAFETY: only arbitrary-data parameters are recorded as owned.
let arb = unsafe { self.params[index].u.arb_d };
// SAFETY: `PF_ArbParamsExtra` is a plain C struct (integers, raw pointers, unions of those), for which all-zero bytes are a valid value.
let mut extra: PF_ArbParamsExtra = unsafe { std::mem::zeroed() };
extra.which_function = PF_Arbitrary_DISPOSE_FUNC as _;
extra.id = arb.id;
extra.u.dispose_func_params.refconPV = arb.refconPV;
extra.u.dispose_func_params.arbH = arb.value;
if let Err(err) = self.call_plugin(RawCommand::ArbitraryCallback, &mut extra as *mut _ as *mut c_void) {
log::warn!("arbitrary param #{index}: disposing the value failed: {err:?}");
}
self.params[index].u.arb_d.value = null_mut();
}
}
/// Call the plugin with `PF_Cmd_SEQUENCE_SETUP` command.
///
/// This gives the plugin a chance to allocate its per-instance
/// `sequence_data` (which [`Self::call_plugin`] then propagates back into
/// `in_data` for subsequent commands). Some effects also perform work here
/// that later render commands depend on -- e.g. plugins built on the
/// aescripts licensing library run their license check during
/// `SEQUENCE_SETUP`/`SEQUENCE_RESETUP`, and render a watermark if it never
/// runs -- so we always issue it before rendering.
///
/// A fresh setup expects a null incoming `sequence_data`; make sure any
/// stale pointer is cleared so the plugin allocates from scratch rather than
/// treating garbage as a flattened blob to resurrect.
fn setup_sequence(&mut self) -> Result<()> {
self.effect_state.reset_sequence();
self.out_data.sequence_data = null_mut();
self.call_plugin(RawCommand::SequenceSetup, null_mut())
}
/// Ask the plugin what its real entry point symbol is via the modern
/// `PluginDataEntryFunction2` protocol -- the same self-description mechanism
/// After Effects itself uses instead of parsing a binary PiPL resource.
fn query_declared_entry_point(lib: &Library, pica: &crate::sys::SPBasicSuite) -> Option<String> {
// SAFETY: The SDK fixes `PluginDataEntryFunction2`'s signature as `PluginDataEntryFn`.
let entry_fn = unsafe { lib.symbol::<PluginDataEntryFn>(PLUGIN_DATA_ENTRY_SYMBOL) }.ok()?;
let mut info = PluginDataInfo::default();
let host_name = CString::new(HOST_NAME).ok()?;
let host_version = CString::new(HOST_VERSION).ok()?;
// SAFETY: `entry_fn` has the SDK's signature; every argument is a live local or the instance's PICA suite.
let result = unsafe {
entry_fn(
&mut info as *mut PluginDataInfo as crate::sys::PF_PluginDataPtr,
Some(receive_plugin_data),
pica as *const crate::sys::SPBasicSuite,
host_name.as_ptr(),
host_version.as_ptr(),
)
};
if result != PF_Err_NONE {
log::debug!("{} reported error code {}.", PLUGIN_DATA_ENTRY_SYMBOL, result);
return None;
}
info.entry_point_name
}
/// Resolve the plugin's entry point. `PluginDataEntryFunction2` wins, then the
/// platform symbol recorded in the PiPL resource, then [`FALLBACK_ENTRY_POINT_CANDIDATES`].
fn resolve_entry_point(
lib: &Library,
pica: &crate::sys::SPBasicSuite,
binary: &Path,
) -> Result<(PluginEntryPoint, String)> {
let declared = Self::query_declared_entry_point(lib, pica);
let from_pipl = match crate::Pipl::read(binary) {
Ok(pipl) => pipl.and_then(|pipl| pipl.entry_point()),
Err(err) => {
log::debug!("PiPL not read: {err}");
None
}
};
if let Some(name) = &from_pipl {
log::debug!("PiPL entry point '{name}'.");
}
let mut names = Vec::new();
for name in declared.into_iter().chain(from_pipl) {
if !names.iter().any(|existing: &String| existing == &name) {
names.push(name);
}
}
for name in FALLBACK_ENTRY_POINT_CANDIDATES {
if !names.iter().any(|existing| existing == name) {
names.push((*name).to_string());
}
}
let mut last_error = None;
for candidate in &names {
// SAFETY: Every AE effect entry point has the SDK's fixed `EffectMain` signature, `PluginEntryPoint`.
match unsafe { lib.symbol::<PluginEntryPoint>(candidate) } {
Ok(symbol) => {
log::info!("Resolved entry point '{candidate}'.");
return Ok((symbol, candidate.clone()));
}
Err(err) => {
log::debug!("Entry point symbol '{}' not resolved: {}", candidate, err);
last_error = Some(err);
}
}
}
Err(AexloError::PluginLoad {
path: binary.to_path_buf(),
source: Box::new(last_error.expect("FALLBACK_ENTRY_POINT_CANDIDATES is non-empty")),
})
}
/// Resolve `artifact_path` -- the plugin as it exists on disk -- to the concrete
/// dynamic library that should be `dlopen`'d.
///
/// Callers hand us whatever they'd double-click to install the plugin: a bare
/// `.aex`/`.dll` file on Windows, or a `.plugin` bundle directory on macOS. Rather
/// than branching on the compiled/runtime OS (which breaks the moment a flat test
/// `.dylib` is loaded on macOS, or a bundle is inspected from another host), we
/// dispatch on the shape of `artifact_path` itself: a directory is a bundle to dig
/// into, a file is already the binary to load.
fn resolve_binary_path(artifact_path: &Path) -> Result<PathBuf> {
if artifact_path.is_dir() {
return Self::resolve_bundle_binary(artifact_path);
}
if artifact_path.is_file() {
return Ok(artifact_path.to_path_buf());
}
Err(AexloError::PluginNotFound {
path: artifact_path.to_path_buf(),
})
}
/// Find the executable inside a macOS `.plugin` bundle's `Contents/MacOS/`.
///
/// AE plugin bundles are required to contain exactly one binary there, so we
/// use that instead of assuming the binary is named after the bundle.
fn resolve_bundle_binary(bundle_path: &Path) -> Result<PathBuf> {
let macos_dir = bundle_path.join("Contents").join("MacOS");
let mut binaries = std::fs::read_dir(&macos_dir)
.map_err(|_| AexloError::PluginNotFound {
path: macos_dir.clone(),
})?
.filter_map(|entry| entry.ok())
.map(|entry| entry.path())
.filter(|path| path.is_file());
match (binaries.next(), binaries.next()) {
(Some(binary), None) => Ok(binary),
(None, _) => Err(AexloError::PluginNotFound { path: macos_dir }),
(Some(_), Some(_)) => Err(AexloError::AmbiguousBundle {
path: bundle_path.to_path_buf(),
}),
}
}
/// Resolve `self.path` to a binary, `dlopen` it, and resolve its entry point,
/// storing the results on `self`.
fn load(&mut self) -> Result<()> {
let module_path = Self::resolve_binary_path(&self.binary_file_path)?;
let module_path_str = module_path.display().to_string();
log::info!("Loading plugin from '{module_path_str}'.");
let lib = Library::open(&module_path).map_err(|e| AexloError::PluginLoad {
path: module_path.clone(),
source: Box::new(e),
})?;
let (entry_point, resolved_name) = Self::resolve_entry_point(&lib, self.pica.as_ref(), &module_path)?;
self.entry_point = Some(entry_point);
self.entry_point_name = Some(resolved_name.clone());
self.raw_library = Some(lib);
// Remembered for the get_platform_data callback (plugin path queries).
self.resolved_binary_path = Some(module_path.clone());
log::info!("Resolved entry point symbol: {resolved_name}.");
log::info!("Loaded plugin '{module_path_str}' successfully.");
Ok(())
}
/// Invoke the resolved entry point with `self.cmd`, updating `self` before and
/// after the call so the next invocation sees a consistent state.
///
/// Before calling: composes a fresh `PF_InData` from the host info, effect
/// state and render context, with `effect_ref` pointing at `self` (so suite
/// callbacks can recover the instance via [`Self::get_instance_ptr`]), and
/// rebuilds the cached param pointer list if `params` was mutated.
///
/// After calling: adopts a non-null `out_data.global_data`/`sequence_data`
/// into the effect state, so plugin-allocated state persists across commands.
///
/// `extra_data` is the command-specific extra struct (e.g. `PF_PreRenderExtra`
/// for `SmartPreRender`), or null for commands that don't take one.
///
/// # Errors
/// Returns [`AexloError::ContainerNotLoaded`] if no entry point has been
/// resolved yet (see [`Self::load`]), before any other state is touched.
/// Returns [`AexloError::PluginExecutionFailed`] if the plugin returns a
/// non-`PF_Err_NONE` code.
pub(super) fn call_plugin(&mut self, command: RawCommand, extra_data: *mut ::std::os::raw::c_void) -> Result<()> {
let entry_point = self.entry_point.ok_or(AexloError::PluginNotLoaded)?;
if !matches!(command, RawCommand::SequenceSetdown | RawCommand::GlobalSetdown) {
crate::suites::register::dispatch_idle(self.aegp_owner);
}
let _aegp_scope = crate::suites::register::enter_owner(self.aegp_owner);
let effect_ref = self as *mut _ as PF_ProgPtr;
let _current_effect = crate::suites::enter_effect(effect_ref);
let entry_point_name = self.entry_point_name.as_deref().unwrap_or(DEFAULT_ENTRY_POINT_NAME);
// debug, not info: this runs for every render command, so at info level a
// preview loop drowns the log.
log::debug!("Executing command: {command:?}");
if self.params_dirty {
self.params_ptr_cache = self.params.iter_mut().map(|p| p as *mut _).collect();
self.params_dirty = false;
}
let mut in_data = crate::core::in_data::compose(
&self.host_info,
&self.effect_state,
&self.render_context,
CallBindings {
effect_ref,
utils: self.utility_callbacks.as_mut() as *mut _,
pica: self.pica.as_mut() as *mut _,
num_params: self.params.len() as i32,
},
);
let observer = self.observer.clone();
let started = observer.as_ref().map(|installed| {
installed.observer.command(&CommandEvent {
command: command as i32,
name: crate::observe::command_name(command as i32),
phase: CommandPhase::Begin,
duration: None,
error: None,
});
Instant::now()
});
let calls_guard = crate::observe::enter(observer.as_ref());
let name = crate::observe::command_name(command as i32);
let tracking = self.tracking();
let resumed = tracking
.as_ref()
.and_then(|_| self.tracker.lock().ok().map(|mut t| t.begin(name)));
// SAFETY: `call_entry`'s contract: the entry point comes from this plugin, and every pointer is this instance's live `in_data`/`out_data`/params/output.
let result = crate::suites::iterate::with_parallel_iterate(self.parallel_iterate, || unsafe {
call_entry(
entry_point,
command,
&mut in_data,
&mut self.out_data,
self.params_ptr_cache.as_mut_ptr(),
&mut self.world,
extra_data,
)
});
if let Some(resumed) = resumed
&& let Ok(mut tracker) = self.tracker.lock()
{
tracker.end(name, resumed);
}
drop(tracking);
drop(calls_guard);
if let (Some(installed), Some(started)) = (&observer, started) {
let code = result as i64;
installed.observer.command(&CommandEvent {
command: command as i32,
name: crate::observe::command_name(command as i32),
phase: CommandPhase::End,
duration: Some(started.elapsed()),
error: (code != PF_Err_NONE as i64).then_some(code as i32),
});
}
self.effect_state.absorb(&self.out_data);
//* ---- Check for errors ---------------------- *//
#[allow(non_upper_case_globals)]
match result {
PF_Err_NONE => {
log::debug!(
"Executed command '{command:?}' successfully ({entry_point_name} exited with code {result})."
);
}
code => {
// The format! only runs on the failure path, never per frame.
return Err(AexloError::PluginExecutionFailed {
command: format!("{command:?}"),
code: code.into(),
});
}
}
//* -------------------------------------------- *//
Ok(())
}
/// Set the plugin down once, in After Effects' order.
pub(super) fn teardown(&mut self) {
// The entry point is missing only when `load` failed to resolve it,
// in which case the plugin was never set up.
if self.entry_point.is_none() || self.torn_down {
crate::suites::register::remove_owner(self.aegp_owner);
return;
}
self.torn_down = true;
// Release pre-render data left by an interrupted render while the
// plugin's code (which owns the delete callback) is still loaded.
self.end_output_guard();
self.dispose_pre_render();
if let Err(err) = self.gpu_device_setdown() {
log::warn!("PF_Cmd_GPU_DEVICE_SETDOWN failed during drop: {err:?}");
}
if let Err(err) = self.call_plugin(RawCommand::SequenceSetdown, null_mut()) {
log::warn!("PF_Cmd_SEQUENCE_SETDOWN failed during drop: {err:?}");
}
self.dispose_arbitrary_params();
if let Err(err) = self.call_plugin(RawCommand::GlobalSetdown, null_mut()) {
log::warn!("PF_Cmd_GLOBAL_SETDOWN failed during drop: {err:?}");
}
crate::suites::register::remove_owner(self.aegp_owner);
if let Ok(mut tracker) = self.tracker.lock() {
tracker.torn_down();
}
}
}