Skip to main content

alpha2_eval/
composition.rs

1//! Composition graphs resolved into the steps that draw their output.
2//!
3//! A composition is compiled once, when the index is built. Nodes reachable from the output are
4//! put in dependency order, bypassed nodes are replaced by their pass-through input, and group
5//! nodes and composition-source media nodes are inlined (the latter shifted by the node's start).
6//! The result equals the flattened graph, so evaluating a frame only samples step settings.
7//!
8//! Inlining copies settings per step. Validation bounds the step count ([`MAX_INLINED_NODES`])
9//! and the planner bounds total plan size ([`MAX_PLAN_BYTES`]).
10
11use alpha2_media::SourceId;
12use alpha2_model::{
13    Animated, AnimatedPoint, ClipBlendMode, ClipEffect, Composition, CompositionId,
14    CompositionReads, EffectValue, Generator, Keyframe, MAX_COMPOSITION_DEPTH, MAX_INLINED_NODES,
15    MatteMode, MediaKind, MediaSource, NodeId, NodeOp, Paint, ShapeMask, VisualProperties,
16};
17use alpha2_time::MediaTime;
18use std::collections::{BTreeMap, HashMap};
19use std::sync::Arc;
20
21use crate::GeneratorPlan;
22
23/// Most bytes all compiled composition plans of one project may hold together, estimated per
24/// [`Step`] (node path plus settings heap). Past this none draws ([`PlanError::TooLarge`]).
25pub const MAX_PLAN_BYTES: u64 = 256 * 1024 * 1024;
26
27/// A composition's output, drawn by steps in dependency order.
28#[derive(Clone, Debug, PartialEq)]
29pub struct CompositionPlan {
30    /// The composition.
31    pub id: CompositionId,
32    /// Steps in dependency order; each step's inputs are earlier steps.
33    pub steps: Vec<Step>,
34    /// The step whose image is the composition's picture.
35    pub output: usize,
36    /// Every `media` node of the compositions the steps come from, drawn or not, by inlined path,
37    /// with its start: what a value bound to data reads through.
38    pub media: Vec<(String, MediaTime)>,
39}
40
41impl CompositionPlan {
42    /// The effect and transition invocations the steps run, in step order.
43    pub fn effects(&self) -> impl Iterator<Item = &ClipEffect> {
44        self.steps.iter().filter_map(|step| match &step.op {
45            StepOp::Effect { effect, .. } | StepOp::Transition { effect, .. } => Some(effect),
46            _ => None,
47        })
48    }
49
50    /// The start of the `media` node `via` beside step `step`, found as the step of that id in the
51    /// same inlined composition.
52    #[must_use]
53    pub fn media_start(&self, step: usize, via: &alpha2_model::NodeId) -> Option<MediaTime> {
54        let path = self.steps.get(step)?.node.as_str();
55        let prefix = path.rfind('/').map_or("", |slash| &path[..=slash]);
56        self.media.iter().find_map(|(node, start)| {
57            (node.strip_prefix(prefix) == Some(via.as_str())).then_some(*start)
58        })
59    }
60
61    /// Whether a step draws the picture of the clip showing the plan.
62    #[must_use]
63    pub fn uses_clip(&self) -> bool {
64        self.steps
65            .iter()
66            .any(|step| matches!(step.op, StepOp::Clip))
67    }
68}
69
70/// One node of a composition, with its inputs resolved to earlier steps.
71#[derive(Clone, Debug, PartialEq)]
72pub struct Step {
73    /// The node path: ids of the group and media nodes inlined through, then its own, joined by `/`.
74    pub node: NodeId,
75    /// What it draws.
76    pub op: StepOp,
77    /// Added to the plan's time to sample this step (starts of the media nodes inlined through).
78    pub offset: MediaTime,
79}
80
81impl Step {
82    /// The time this step's settings are sampled at when the plan is drawn at `time`.
83    #[must_use]
84    pub fn time(&self, time: MediaTime) -> MediaTime {
85        MediaTime(time.ticks().saturating_add(self.offset.ticks()))
86    }
87}
88
89/// The operation of a [`Step`]. Inputs are indices of earlier steps.
90#[derive(Clone, Debug, PartialEq)]
91pub enum StepOp {
92    /// The picture of the clip showing the composition.
93    Clip,
94    /// A host-supplied source at `start` plus the composition time.
95    Media {
96        /// The source.
97        source_id: SourceId,
98        /// Source time at composition time zero.
99        start: MediaTime,
100    },
101    /// A picture the engine draws (a generator node, or a media node naming a generated source).
102    Generator {
103        /// What it draws.
104        generator: Generator,
105        /// Picture time at composition time zero: a media node's start, zero for a generator node.
106        start: MediaTime,
107        /// Planned tree for a shapes or layout generator.
108        plan: Option<Arc<GeneratorPlan>>,
109    },
110    /// A registered effect.
111    Effect {
112        /// The processed image.
113        input: usize,
114        /// Additional named inputs, in name order.
115        inputs: Vec<(String, usize)>,
116        /// The effect.
117        effect: ClipEffect,
118    },
119    /// A registered transition at an explicit progress.
120    Transition {
121        /// The image at progress zero.
122        from: usize,
123        /// The image at progress one.
124        to: usize,
125        /// The transition.
126        effect: ClipEffect,
127        /// Progress in `0..=1`.
128        progress: Animated<f32>,
129    },
130    /// The input placed by a transform.
131    Transform {
132        /// The placed image.
133        input: usize,
134        /// The transform.
135        transform: VisualProperties,
136    },
137    /// Layers composited over transparency, bottom first.
138    Merge {
139        /// The layers.
140        layers: Vec<StepLayer>,
141    },
142    /// The input kept where a second image says.
143    Matte {
144        /// The kept image.
145        input: usize,
146        /// The coverage image.
147        matte: usize,
148        /// Which property of the coverage image counts.
149        mode: MatteMode,
150    },
151    /// The input kept inside shapes.
152    ShapeMask {
153        /// The masked image.
154        input: usize,
155        /// The shapes.
156        masks: Vec<ShapeMask>,
157    },
158}
159
160/// One layer of a [`StepOp::Merge`].
161#[derive(Clone, Debug, PartialEq)]
162pub struct StepLayer {
163    /// The layer's image.
164    pub input: usize,
165    /// Its blend mode.
166    pub blend_mode: ClipBlendMode,
167    /// Its opacity.
168    pub opacity: Animated<f32>,
169}
170
171/// Why a composition's output cannot be drawn.
172#[derive(Clone, Debug, PartialEq, Eq)]
173pub enum PlanError {
174    /// The composition selects no output.
175    NoOutput,
176    /// A node the output needs has an unconnected input.
177    MissingInput {
178        /// The node.
179        node: NodeId,
180        /// `input`, `from`, `to`, `matte`, `layers[i]`, `inputs[i]` or `inputs.<name>`.
181        input: String,
182    },
183    /// A group input read where no group node feeds it.
184    UnboundGroupInput {
185        /// The node.
186        node: NodeId,
187    },
188    /// A clip node in a composition drawn as a source, which shows no clip.
189    ClipWithoutClip {
190        /// The node.
191        node: NodeId,
192    },
193    /// Compositions nest deeper than [`alpha2_model::MAX_COMPOSITION_DEPTH`] (unvalidated only).
194    NestedTooDeep {
195        /// The node.
196        node: NodeId,
197    },
198    /// More than [`alpha2_model::MAX_INLINED_NODES`] steps, or the project's plans exceed
199    /// [`MAX_PLAN_BYTES`].
200    TooLarge,
201}
202
203/// Remaining plan bytes, shared by all plans of one project.
204#[derive(Debug)]
205pub(crate) struct PlanBudget {
206    left: u64,
207    exceeded: bool,
208}
209
210impl PlanBudget {
211    pub(crate) const fn new(bytes: u64) -> Self {
212        Self {
213            left: bytes,
214            exceeded: false,
215        }
216    }
217
218    /// Whether a plan needed more than was left.
219    pub(crate) const fn exceeded(&self) -> bool {
220        self.exceeded
221    }
222}
223
224/// A project's compositions and sources by ID, and what each composition reads.
225pub(crate) struct Lookup<'a> {
226    compositions: HashMap<&'a CompositionId, &'a Composition>,
227    sources: HashMap<&'a SourceId, &'a MediaSource>,
228    reads: CompositionReads<'a>,
229    /// The project canvas in pixels.
230    canvas: (u32, u32),
231}
232
233impl<'a> Lookup<'a> {
234    pub(crate) fn new(
235        compositions: &'a [Composition],
236        sources: &'a [MediaSource],
237        canvas: (u32, u32),
238    ) -> Self {
239        let compositions = compositions
240            .iter()
241            .map(|composition| (&composition.id, composition))
242            .collect();
243        let reads = CompositionReads::new(&compositions);
244        Self {
245            compositions,
246            sources: sources.iter().map(|source| (&source.id, source)).collect(),
247            reads,
248            canvas,
249        }
250    }
251
252    /// What each composition reads.
253    pub(crate) const fn reads(&self) -> &CompositionReads<'a> {
254        &self.reads
255    }
256}
257
258/// Compiles `composition`'s output into steps as a clip's composition, where clip nodes draw the
259/// clip's picture. `compositions` resolves group nodes and `sources` resolves media nodes.
260///
261/// The project must be valid ([`alpha2_model::Project::validate`]).
262///
263/// # Errors
264/// Returns [`PlanError`] when there is no output, a needed input is unconnected, or the plan
265/// nests too deep or grows past [`MAX_PLAN_BYTES`].
266pub fn plan_composition(
267    composition: &Composition,
268    compositions: &[Composition],
269    sources: &[MediaSource],
270    canvas: (u32, u32),
271) -> Result<CompositionPlan, PlanError> {
272    plan(
273        composition,
274        &Lookup::new(compositions, sources, canvas),
275        true,
276        &mut PlanBudget::new(MAX_PLAN_BYTES),
277    )
278}
279
280/// [`plan_composition`] drawing from a shared `budget`.
281pub(crate) fn plan_composition_within(
282    composition: &Composition,
283    lookup: &Lookup<'_>,
284    budget: &mut PlanBudget,
285) -> Result<CompositionPlan, PlanError> {
286    plan(composition, lookup, true, budget)
287}
288
289/// Compiles `composition` as a composition source. Like [`plan_composition`], but clip nodes and
290/// group inputs are errors because nothing feeds them.
291///
292/// # Errors
293/// Returns [`PlanError`] as [`plan_composition`] does, and for a needed clip node or group input.
294pub fn plan_source_composition(
295    composition: &Composition,
296    compositions: &[Composition],
297    sources: &[MediaSource],
298    canvas: (u32, u32),
299) -> Result<CompositionPlan, PlanError> {
300    plan(
301        composition,
302        &Lookup::new(compositions, sources, canvas),
303        false,
304        &mut PlanBudget::new(MAX_PLAN_BYTES),
305    )
306}
307
308/// [`plan_source_composition`] drawing from a shared `budget`.
309pub(crate) fn plan_source_composition_within(
310    composition: &Composition,
311    lookup: &Lookup<'_>,
312    budget: &mut PlanBudget,
313) -> Result<CompositionPlan, PlanError> {
314    plan(composition, lookup, false, budget)
315}
316
317fn plan<'a>(
318    composition: &'a Composition,
319    lookup: &Lookup<'a>,
320    clip_allowed: bool,
321    budget: &mut PlanBudget,
322) -> Result<CompositionPlan, PlanError> {
323    let mut planner = Planner {
324        lookup,
325        steps: Vec::new(),
326        media: Vec::new(),
327        budget,
328    };
329    let scope = Scope {
330        composition,
331        inputs: Vec::new(),
332        controls: HashMap::new(),
333        offset: MediaTime::ZERO,
334        prefix: String::new(),
335        clip_allowed,
336        group_inputs_bound: false,
337        depth: 1,
338    };
339    let output = planner.output(&scope)?;
340    Ok(CompositionPlan {
341        id: composition.id.clone(),
342        steps: planner.steps,
343        output,
344        media: planner.media,
345    })
346}
347
348/// One composition as it is being drawn: inline in another, or on its own.
349struct Scope<'a> {
350    composition: &'a Composition,
351    /// The step each group input reads, if the composition reads it.
352    inputs: Vec<Option<usize>>,
353    /// Values its group node sets, by node and parameter.
354    controls: HashMap<(&'a NodeId, &'a str), &'a Animated<EffectValue>>,
355    /// Added to the plan's time for its steps.
356    offset: MediaTime,
357    /// Prepended to its node ids in the steps.
358    prefix: String,
359    /// Whether a clip node draws the clip's picture here.
360    clip_allowed: bool,
361    /// Whether a group node feeds its group inputs.
362    group_inputs_bound: bool,
363    /// Nesting depth, one for the plan's own composition.
364    depth: usize,
365}
366
367struct Planner<'a, 'b> {
368    lookup: &'b Lookup<'a>,
369    steps: Vec<Step>,
370    media: Vec<(String, MediaTime)>,
371    budget: &'b mut PlanBudget,
372}
373
374impl<'a> Planner<'a, '_> {
375    /// The step drawing `scope`'s output.
376    fn output(&mut self, scope: &Scope<'a>) -> Result<usize, PlanError> {
377        let output = scope
378            .composition
379            .output
380            .as_ref()
381            .ok_or(PlanError::NoOutput)?;
382        for node in &scope.composition.nodes {
383            if let NodeOp::Media { start, .. } = &node.op {
384                self.media
385                    .push((format!("{}{}", scope.prefix, node.id), *start));
386            }
387        }
388        let nodes: HashMap<&NodeId, &'a alpha2_model::CompositionNode> = scope
389            .composition
390            .nodes
391            .iter()
392            .map(|node| (&node.id, node))
393            .collect();
394        self.resolve(scope, &nodes, output)
395    }
396
397    /// The step drawing node `id` of `scope`, planning its dependencies first.
398    ///
399    /// The walk is iterative so a long chain cannot exhaust the stack. Nesting recurses at most
400    /// [`alpha2_model::MAX_COMPOSITION_DEPTH`] deep, and the planner enforces both limits even
401    /// for unvalidated projects.
402    fn resolve(
403        &mut self,
404        scope: &Scope<'a>,
405        nodes: &HashMap<&NodeId, &'a alpha2_model::CompositionNode>,
406        id: &'a NodeId,
407    ) -> Result<usize, PlanError> {
408        let mut done: HashMap<&NodeId, usize> = HashMap::new();
409        // Each entry is a node whose needed inputs are pushed before it is revisited.
410        let mut pending = vec![(id, false)];
411        while let Some((current, expanded)) = pending.pop() {
412            if done.contains_key(current) {
413                continue;
414            }
415            let node = nodes[current];
416            let needed = needed_inputs(self.lookup.reads(), &scope.prefix, node)?;
417            if !expanded {
418                pending.push((current, true));
419                pending.extend(
420                    needed
421                        .iter()
422                        .rev()
423                        .filter(|input| !done.contains_key(**input))
424                        .map(|input| (*input, false)),
425                );
426                continue;
427            }
428            let step = if node.bypass {
429                done[needed[0]]
430            } else {
431                self.node_step(scope, node, &done)?
432            };
433            done.insert(current, step);
434        }
435        Ok(done[id])
436    }
437
438    fn generator_step(&self, generator: &Generator, start: MediaTime) -> StepOp {
439        generator_step(generator.clone(), start, self.lookup.canvas)
440    }
441
442    fn node_step(
443        &mut self,
444        scope: &Scope<'a>,
445        node: &'a alpha2_model::CompositionNode,
446        done: &HashMap<&NodeId, usize>,
447    ) -> Result<usize, PlanError> {
448        let at = |input: &Option<NodeId>| {
449            input
450                .as_ref()
451                .map(|id| done[id])
452                .expect("needed inputs are connected and planned")
453        };
454        let id = || scoped(&scope.prefix, &node.id);
455        let op = match &node.op {
456            NodeOp::Clip {} => {
457                if !scope.clip_allowed {
458                    return Err(PlanError::ClipWithoutClip { node: id() });
459                }
460                StepOp::Clip
461            }
462            NodeOp::GroupInput { index } => {
463                // Validation keeps the index within the composition's ports.
464                return usize::try_from(*index)
465                    .ok()
466                    .and_then(|index| scope.inputs.get(index).copied().flatten())
467                    .filter(|_| scope.group_inputs_bound)
468                    .ok_or_else(|| PlanError::UnboundGroupInput { node: id() });
469            }
470            NodeOp::Group {
471                composition,
472                inputs,
473                controls,
474            } => {
475                // Only the ports its composition reads are needed, and planned.
476                let reads = self.lookup.reads();
477                let inputs = inputs
478                    .iter()
479                    .enumerate()
480                    .map(|(port, input)| reads.reads_port(composition, port).then(|| at(input)))
481                    .collect();
482                return self.group(scope, &id(), composition, inputs, controls);
483            }
484            NodeOp::Media { source_id, start } => {
485                let source = self.lookup.sources.get(source_id).copied();
486                if let Some(composition) = source.and_then(|source| source.composition.as_ref()) {
487                    return self.composition_source(scope, &id(), composition, *start);
488                }
489                let generator = source
490                    .filter(|source| source.kind == MediaKind::Generated)
491                    .and_then(|source| source.generator.as_ref());
492                generator.map_or_else(
493                    || StepOp::Media {
494                        source_id: source_id.clone(),
495                        start: *start,
496                    },
497                    |generator| self.generator_step(generator, *start),
498                )
499            }
500            NodeOp::Generator { generator } => self.generator_step(generator, MediaTime::ZERO),
501            NodeOp::Effect {
502                input,
503                inputs,
504                effect,
505            } => StepOp::Effect {
506                input: at(input),
507                inputs: named_steps(inputs, &at),
508                effect: controlled(scope, &node.id, effect),
509            },
510            NodeOp::Transition {
511                from,
512                to,
513                effect,
514                progress,
515            } => StepOp::Transition {
516                from: at(from),
517                to: at(to),
518                effect: controlled(scope, &node.id, effect),
519                progress: progress.clone(),
520            },
521            NodeOp::Transform { input, transform } => StepOp::Transform {
522                input: at(input),
523                transform: transform.clone(),
524            },
525            NodeOp::Merge { layers } => StepOp::Merge {
526                layers: layers
527                    .iter()
528                    .map(|layer| StepLayer {
529                        input: at(&layer.input),
530                        blend_mode: layer.blend_mode,
531                        opacity: layer.opacity.clone(),
532                    })
533                    .collect(),
534            },
535            NodeOp::Matte { input, matte, mode } => StepOp::Matte {
536                input: at(input),
537                matte: at(matte),
538                mode: *mode,
539            },
540            NodeOp::ShapeMask { input, masks } => StepOp::ShapeMask {
541                input: at(input),
542                masks: masks.clone(),
543            },
544        };
545        self.push(Step {
546            node: id(),
547            op,
548            offset: scope.offset,
549        })
550    }
551
552    /// Adds `step`, enforcing [`MAX_INLINED_NODES`] and the byte budget.
553    fn push(&mut self, step: Step) -> Result<usize, PlanError> {
554        if self.steps.len() >= MAX_INLINED_NODES {
555            return Err(PlanError::TooLarge);
556        }
557        let bytes = step_bytes(&step);
558        if bytes > self.budget.left {
559            self.budget.exceeded = true;
560            return Err(PlanError::TooLarge);
561        }
562        self.budget.left -= bytes;
563        self.steps.push(step);
564        Ok(self.steps.len() - 1)
565    }
566
567    /// Inlines a group node's composition with its inputs and controls applied.
568    fn group(
569        &mut self,
570        scope: &Scope<'a>,
571        node: &NodeId,
572        composition: &CompositionId,
573        inputs: Vec<Option<usize>>,
574        controls: &'a std::collections::BTreeMap<String, Animated<EffectValue>>,
575    ) -> Result<usize, PlanError> {
576        let target = self.composition(composition);
577        let depth = nested(scope, node)?;
578        let controls = target
579            .controls
580            .iter()
581            .filter_map(|control| {
582                controls
583                    .get(&control.name)
584                    .map(|value| ((&control.node, control.param.as_str()), value))
585            })
586            .collect();
587        self.output(&Scope {
588            composition: target,
589            inputs,
590            controls,
591            offset: scope.offset,
592            prefix: format!("{node}/"),
593            clip_allowed: scope.clip_allowed,
594            group_inputs_bound: true,
595            depth,
596        })
597    }
598
599    /// Inlines a media node's composition source, shifted by `start`.
600    fn composition_source(
601        &mut self,
602        scope: &Scope<'a>,
603        node: &NodeId,
604        composition: &CompositionId,
605        start: MediaTime,
606    ) -> Result<usize, PlanError> {
607        let depth = nested(scope, node)?;
608        self.output(&Scope {
609            composition: self.composition(composition),
610            inputs: Vec::new(),
611            controls: HashMap::new(),
612            offset: MediaTime(scope.offset.ticks().saturating_add(start.ticks())),
613            prefix: format!("{node}/"),
614            clip_allowed: false,
615            group_inputs_bound: false,
616            depth,
617        })
618    }
619
620    fn composition(&self, id: &CompositionId) -> &'a Composition {
621        self.lookup
622            .compositions
623            .get(id)
624            .copied()
625            .expect("validation guarantees referenced compositions exist")
626    }
627}
628
629fn nested(scope: &Scope<'_>, node: &NodeId) -> Result<usize, PlanError> {
630    let depth = scope.depth + 1;
631    if depth > MAX_COMPOSITION_DEPTH {
632        return Err(PlanError::NestedTooDeep { node: node.clone() });
633    }
634    Ok(depth)
635}
636
637fn scoped(prefix: &str, id: &NodeId) -> NodeId {
638    if prefix.is_empty() {
639        return id.clone();
640    }
641    NodeId::new(format!("{prefix}{id}")).expect("a prefixed id is not empty")
642}
643
644fn named_steps(
645    inputs: &BTreeMap<String, Option<NodeId>>,
646    at: &dyn Fn(&Option<NodeId>) -> usize,
647) -> Vec<(String, usize)> {
648    inputs
649        .iter()
650        .map(|(name, node)| (name.clone(), at(node)))
651        .collect()
652}
653
654fn controlled(scope: &Scope<'_>, node: &NodeId, effect: &ClipEffect) -> ClipEffect {
655    let mut effect = effect.clone();
656    for ((target, param), value) in &scope.controls {
657        if *target == node {
658            effect.params.insert((*param).to_owned(), (*value).clone());
659        }
660    }
661    effect
662}
663
664/// Bytes `step` holds, as [`MAX_PLAN_BYTES`] counts them.
665fn step_bytes(step: &Step) -> u64 {
666    let heap = match &step.op {
667        StepOp::Clip | StepOp::Matte { .. } => 0,
668        StepOp::Media { source_id, .. } => source_id.as_str().len(),
669        StepOp::Generator { generator, .. } => generator_bytes(generator),
670        StepOp::Effect { effect, inputs, .. } => {
671            effect_bytes(effect)
672                + inputs
673                    .iter()
674                    .map(|(name, _)| name.len() + size_of::<(String, usize)>())
675                    .sum::<usize>()
676        }
677        StepOp::Transition {
678            effect, progress, ..
679        } => effect_bytes(effect) + keyframe_bytes(progress),
680        StepOp::Transform { transform, .. } => transform_bytes(transform),
681        StepOp::Merge { layers } => layers
682            .iter()
683            .map(|layer| size_of::<StepLayer>() + keyframe_bytes(&layer.opacity))
684            .sum(),
685        StepOp::ShapeMask { masks, .. } => masks.iter().map(mask_bytes).sum(),
686    };
687    u64::try_from(size_of::<Step>() + step.node.as_str().len() + heap).unwrap_or(u64::MAX)
688}
689
690/// The heap an animated value's keyframes take; none for a constant.
691fn keyframe_bytes<T>(value: &Animated<T>) -> usize {
692    match value {
693        Animated::Constant { .. } => 0,
694        Animated::Keyframes { keyframes } => keyframes.len() * size_of::<Keyframe<T>>(),
695        Animated::Data(binding) => binding.channel.len(),
696    }
697}
698
699fn effect_bytes(effect: &ClipEffect) -> usize {
700    effect.id.len()
701        + effect
702            .params
703            .iter()
704            .map(|(name, value)| {
705                name.len() + size_of::<Animated<EffectValue>>() + keyframe_bytes(value)
706            })
707            .sum::<usize>()
708        + effect
709            .resources
710            .iter()
711            .map(|(name, id)| name.len() + id.as_str().len())
712            .sum::<usize>()
713}
714
715fn transform_bytes(transform: &VisualProperties) -> usize {
716    point_bytes(&transform.position)
717        + keyframe_bytes(&transform.scale)
718        + keyframe_bytes(&transform.rotation_degrees)
719        + keyframe_bytes(&transform.opacity)
720}
721
722fn mask_bytes(mask: &ShapeMask) -> usize {
723    size_of::<ShapeMask>() + geometry_bytes(&mask.shape) + keyframe_bytes(&mask.feather)
724}
725
726fn point_bytes(point: &AnimatedPoint) -> usize {
727    match point {
728        AnimatedPoint::Constant { .. } | AnimatedPoint::Data(_) => 0,
729        AnimatedPoint::Keyframes { keyframes } => {
730            keyframes.len() * size_of::<alpha2_model::PointKeyframe>()
731        }
732    }
733}
734
735fn path_bytes(path: &Animated<alpha2_model::Path>) -> usize {
736    let one = |path: &alpha2_model::Path| {
737        path.subpaths.len() * size_of::<alpha2_model::Subpath>()
738            + path.vertex_count() * size_of::<alpha2_model::PathVertex>()
739    };
740    match path {
741        Animated::Data(_) => 0,
742        Animated::Constant { value } => one(value),
743        Animated::Keyframes { keyframes } => keyframes
744            .iter()
745            .map(|keyframe| size_of::<Keyframe<alpha2_model::Path>>() + one(&keyframe.value))
746            .sum(),
747    }
748}
749
750fn geometry_bytes(geometry: &alpha2_model::ShapeGeometry) -> usize {
751    use alpha2_model::ShapeGeometry;
752    match geometry {
753        ShapeGeometry::Rectangle(rectangle) => {
754            point_bytes(&rectangle.center)
755                + keyframe_bytes(&rectangle.size)
756                + keyframe_bytes(&rectangle.corner_radius)
757                + keyframe_bytes(&rectangle.rotation_degrees)
758        }
759        ShapeGeometry::Ellipse(ellipse) => {
760            point_bytes(&ellipse.center)
761                + keyframe_bytes(&ellipse.size)
762                + keyframe_bytes(&ellipse.rotation_degrees)
763        }
764        ShapeGeometry::Polystar(star) => {
765            point_bytes(&star.center)
766                + keyframe_bytes(&star.points)
767                + keyframe_bytes(&star.outer_radius)
768                + keyframe_bytes(&star.inner_radius)
769                + keyframe_bytes(&star.outer_roundness)
770                + keyframe_bytes(&star.inner_roundness)
771                + keyframe_bytes(&star.rotation_degrees)
772        }
773        ShapeGeometry::Path(path) => path_bytes(&path.path),
774    }
775}
776
777fn paint_bytes(paint: &Paint) -> usize {
778    match paint {
779        Paint::Solid { .. } => 0,
780        Paint::LinearGradient { stops, .. }
781        | Paint::RadialGradient { stops, .. }
782        | Paint::ConicGradient { stops, .. } => {
783            stops.len() * size_of::<alpha2_model::GradientStop>()
784        }
785    }
786}
787
788fn animated_paint_bytes(paint: &Animated<Paint>) -> usize {
789    match paint {
790        Animated::Data(_) => 0,
791        Animated::Constant { value } => paint_bytes(value),
792        Animated::Keyframes { keyframes } => keyframes
793            .iter()
794            .map(|keyframe| size_of::<Keyframe<Paint>>() + paint_bytes(&keyframe.value))
795            .sum(),
796    }
797}
798
799/// Approximate bytes a content tree holds. Its plan holds about as much again.
800fn shape_items_bytes(items: &[alpha2_model::ShapeItem]) -> usize {
801    use alpha2_model::{ShapeGeometry, ShapeItem};
802    items
803        .iter()
804        .map(|item| {
805            size_of::<ShapeItem>()
806                + match item {
807                    ShapeItem::Group { items, transform } => {
808                        shape_items_bytes(items)
809                            + keyframe_bytes(&transform.anchor)
810                            + point_bytes(&transform.position)
811                            + keyframe_bytes(&transform.scale)
812                            + keyframe_bytes(&transform.rotation_degrees)
813                            + keyframe_bytes(&transform.opacity)
814                    }
815                    ShapeItem::Rectangle(geometry) => {
816                        geometry_bytes(&ShapeGeometry::Rectangle(geometry.clone()))
817                    }
818                    ShapeItem::Ellipse(geometry) => {
819                        geometry_bytes(&ShapeGeometry::Ellipse(geometry.clone()))
820                    }
821                    ShapeItem::Polystar(geometry) => {
822                        geometry_bytes(&ShapeGeometry::Polystar(geometry.clone()))
823                    }
824                    ShapeItem::Path(geometry) => path_bytes(&geometry.path),
825                    ShapeItem::Fill { paint, opacity, .. } => {
826                        animated_paint_bytes(paint) + keyframe_bytes(opacity)
827                    }
828                    ShapeItem::Stroke(stroke) => {
829                        animated_paint_bytes(&stroke.paint)
830                            + keyframe_bytes(&stroke.width)
831                            + keyframe_bytes(&stroke.opacity)
832                            + stroke.dash.as_ref().map_or(0, |dash| {
833                                dash.pattern
834                                    .iter()
835                                    .map(|entry| size_of::<Animated<f32>>() + keyframe_bytes(entry))
836                                    .sum::<usize>()
837                                    + keyframe_bytes(&dash.offset)
838                            })
839                    }
840                    ShapeItem::Trim(trim) => {
841                        keyframe_bytes(&trim.start)
842                            + keyframe_bytes(&trim.end)
843                            + keyframe_bytes(&trim.offset)
844                    }
845                }
846        })
847        .sum()
848}
849
850fn generator_step(generator: Generator, start: MediaTime, canvas: (u32, u32)) -> StepOp {
851    let plan = GeneratorPlan::new(&generator, canvas).map(Arc::new);
852    StepOp::Generator {
853        generator,
854        start,
855        plan,
856    }
857}
858
859fn generator_bytes(generator: &Generator) -> usize {
860    match generator {
861        Generator::Fill { paint } => paint_bytes(paint),
862        // Counted twice: the items and their plan.
863        Generator::Shapes { items } => 2 * shape_items_bytes(items),
864        Generator::Text { text, style } => {
865            text.len() + style.font_family.as_ref().map_or(0, String::len)
866        }
867        // Counted twice: the tree and its plan.
868        Generator::Layout { root } => 2 * crate::layout::layout_bytes(root, &shape_items_bytes),
869    }
870}
871
872/// A node's [`CompositionReads::needed_inputs`] in input order, failing on an unconnected one
873/// (named with `prefix`, like every plan error).
874fn needed_inputs<'a>(
875    reads: &CompositionReads<'_>,
876    prefix: &str,
877    node: &'a alpha2_model::CompositionNode,
878) -> Result<Vec<&'a NodeId>, PlanError> {
879    let needed = reads.needed_inputs(node);
880    if node.bypass && needed.is_empty() {
881        // A merge without layers has nothing to pass through.
882        return Err(PlanError::MissingInput {
883            node: scoped(prefix, &node.id),
884            input: "layers[0]".into(),
885        });
886    }
887    needed
888        .into_iter()
889        .map(|input| {
890            input.node().ok_or_else(|| PlanError::MissingInput {
891                node: scoped(prefix, &node.id),
892                input: input.name(),
893            })
894        })
895        .collect()
896}
897
898/// A clip's compiled composition and how its time maps onto it.
899#[derive(Clone, Debug)]
900pub(crate) struct IndexedComposition {
901    pub(crate) plan: Arc<Result<CompositionPlan, PlanError>>,
902    pub(crate) reference: alpha2_model::ClipComposition,
903}
904
905/// A clip's composition at one timeline timestamp.
906#[derive(Clone, Copy, Debug, PartialEq)]
907pub struct ActiveComposition<'a> {
908    plan: &'a CompositionPlan,
909    time: MediaTime,
910    data: crate::SampleContext<'a>,
911}
912
913impl<'a> ActiveComposition<'a> {
914    /// `plan` at composition time `time`, reading no data.
915    #[must_use]
916    pub const fn new(plan: &'a CompositionPlan, time: MediaTime) -> Self {
917        Self {
918            plan,
919            time,
920            data: crate::SampleContext::NONE,
921        }
922    }
923
924    /// The same composition reading data through `data`.
925    #[must_use]
926    pub const fn with_data(self, data: crate::SampleContext<'a>) -> Self {
927        Self { data, ..self }
928    }
929
930    /// What step `step`'s settings read data through.
931    #[must_use]
932    pub fn step_data(&self, step: usize) -> crate::SampleContext<'a> {
933        self.data.for_step(self.plan, step)
934    }
935
936    /// The compiled composition.
937    #[must_use]
938    pub const fn plan(&self) -> &'a CompositionPlan {
939        self.plan
940    }
941
942    /// The composition time at which settings are sampled.
943    #[must_use]
944    pub const fn time(&self) -> MediaTime {
945        self.time
946    }
947
948    /// Each host-supplied source a step reads, with step index and source time.
949    pub fn media(&self) -> impl Iterator<Item = (usize, &'a SourceId, MediaTime)> + 'a {
950        let time = self.time;
951        self.plan
952            .steps
953            .iter()
954            .enumerate()
955            .filter_map(move |(index, step)| match &step.op {
956                StepOp::Media { source_id, start } => Some((
957                    index,
958                    source_id,
959                    MediaTime(start.ticks().saturating_add(step.time(time).ticks())),
960                )),
961                _ => None,
962            })
963    }
964}
965
966#[cfg(test)]
967#[path = "composition_tests.rs"]
968mod tests;